Does OCaml float in a box or without a box?

I know that floating point numbers are usually placed in OCaml, but my difficulty is that the word: usually. When are they not in the box? And, if they are not boxed, how are they represented, so does the runtime recognize them as different from ints or pointers?

I found http://caml.inria.fr/pub/old_caml_site/ocaml/numerical.html , which lists certain moments when the floats do not fit in the box, but it is 11 years old, t know if it is still updated, and he does not explain HOW they are presented when they are not boxed.

I'm new to OCaml, so sorry if this is a stupid noob question. Thanks!

+7
floating-point ocaml
source share
3 answers

Floats are unpacked when in an array and in a record, all of whose fields are floating. For these cases, there is a special tag that marks the collection as containing unpacked floats.

This is described in section 19.3 of the OCaml manual.

+7
source share

Floats are unpacked in two situations:

  • When there is only float in the array (even in polymorphic functions) or in a record (this time only when all fields are defined as float at compile time)

  • During sequences of floating-point operations, the compiler notices that it is not useful to put a float just before unpacking it for the next operation using it, so the float remains open. IN:

    let x = let y = a +. b in y *. c 

    The y value will not be included. This optimization is also performed on int32, int64 and nativeint, so they have very good performance in heavy computing.

+4
source share

In addition to Jeffrey's answer, note that polymorphic fields (which are assigned a float) are not counted as a float in the criterion "all fields must be float".

I.e:

 type r = { x: float; y: float; } let a = { x = 1.0; y = 2.0; } (* unboxed *) type 'ar = { x: 'a; y: 'a; } let a = { x = 1.0; y = 2.0; } (* boxed *) 
+2
source share

All Articles