Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value of Pi and Math module #5173

Closed
vicuna opened this issue Nov 4, 2010 · 2 comments
Closed

Value of Pi and Math module #5173

vicuna opened this issue Nov 4, 2010 · 2 comments

Comments

@vicuna
Copy link

vicuna commented Nov 4, 2010

Original bug ID: 5173
Reporter: warwick
Status: closed (set by @xavierleroy on 2010-11-11T17:21:50Z)
Resolution: not a bug
Priority: normal
Severity: feature
Version: 3.12.0
Category: ~DO NOT USE (was: OCaml general)
Related to: #4170
Monitored by: @Chris00

Bug description

Sorry ... I guess this has been discussed already, but I think the inclusion of a value for Pi would make OCaml easier to learn and use (rather than having to put: let pi = 4. *. atan 1. in your code).

It'd be a problem adding pi to the namespace in Pervasives, so it'd be good to have a Math module. This would give the opportunity to move floating point functions from Pervasives to Math, over time; and also the chance to add some new maths functions or values if required - without cluttering Pervasives.

For example:

Math.round, Math.deg, Math.rad, Math.e, Math.asinh, Math.acosh, Math.atanh, ...

@vicuna
Copy link
Author

vicuna commented Nov 4, 2010

Comment author: warwick

I just looked at the value of pi more closely (using toplevel OCaml 3.12.0 on Mac) and I didn't realise that the floating point loses some precision, so if you put either of
let pi = 4. *. atan 1.
let pi = 3.1415926535897932384626433832795

You end up with
val pi : float = 3.14159265358979312

(ie ending with ...312 rather than ...323 or ...324 rounded)

@vicuna
Copy link
Author

vicuna commented Nov 11, 2010

Comment author: @xavierleroy

Hello,

You're right that, in retrospect, it would have been preferable to have a separate module for float operations in the standard library. Experience shows, however, that adding a new module in the stdlib with a nice, popular name like Math always introduces incompatibilies in existing Caml code, something that we cannot afford at this point.

Note, however, that OCaml Batteries (http://thelema.github.com/batteries-included/hdoc/) has a "BatFloat" module very much like what you propose, and it defines "BatFloat.pi" among other useful things. If you have ideas and code for additional floating-point stuff, I'm sure the Batteries people would be receptive.

Concerning the loss of precision: the float you get out of "4 *. atan 1.0" is, indeed, the 64-bit IEEE float closest to the true value of pi. That's the best one can do within the limitations of this float format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant