New 'pi' constant

  • With OCaml 4.07.0+rc1 the value ‘max_float’ can be obtained with:
max_float ;;
Pervasives.max_float ;;
Float.max_float ;;
Stdlib.max_float ;;
Stdlib.Pervasives.max_float ;;
Stdlib__float.max_float ;;

Waouw! Other possibilities?

  • Note: the constant ‘pi’.

In the OCaml 4.07 manual:
https://caml.inria.fr/pub/distrib/ocaml-4.07/ocaml-4.07-refman.html#sec8

we have:

# let pi = 4.0 *. atan 1.0;;

Would not it be appropriate to use the new pre-defined ‘pi’ constant?

e.g.

#let pi = Float.pi ;;

#let pi = Stdlib.Float.pi ;; (* funky *)
#let pi = Stdlib__float.pi ;; (* funkier *)
2 Likes

Using Float.pi that early in the manual is problematic because modules are only introduced one chapter later. Replacing π with e might be a good idea to avoid giving the impression that π is not defined in the standard library.

1 Like

There are plenty of ways to get pi too:

let pi = 2.0 *. asin 1.0;;
let pi = 2.0 *. acos 0.0;;
let pi = 4.0 *. atan 1.0;;
let pi = 2.0 *. Complex.(arg i);;
let pi = 2.0 *. Complex.((log i).im);;

I usually use this:

let pi = acos (-1.);;

Why not?

But, could not ‘e’ also be defined in Float?

Adding e might be an option; but I think that it is far less straigthforward than π.
Contrarily to π, e is really easy to access with exp 1..
Moreover, e is rarely used as an isolated constant by itelf. The closest example than I can think of right now is Stirling formula n! ~ √(2πn) (n/e) ^ (-n) but this can also be written as √(2πn) exp ( n log n - n). To the point that I have personally used the Euler-Mascheroni γ more often than e (as an isolated constant).

2 Likes

Indeed, Euler y (or √π, or √2π …) are much more common.
e.g.:

let euler = 0x0.93C467E37DB0C7A4D1BE3F810152CB
let sqrt_pi = 0x1.C5BF891B4EF6AA79C3B0520D5DB938
let sqrt_2pi = 0x2.81B263FEC4E0B2CAF9483F5CE459DC