[erlang-questions] erlang float comparison

Richard O'Keefe <>
Thu May 17 00:52:17 CEST 2012


On 17/05/2012, at 1:20 AM, Angel J. Alvarez Miguel wrote:
> Also, asking my workmate about this, results also in some vague memories about acos 
> failing on a number around 1.0-e8 still we can detail exactly what the number was.. d'oh

This should be a very easy case for acos.  If epsilon is a small number,
then acos(epsilon) approx= pi/2 - epsilon.  On the other hand, since pi
is not machine-representable, this might well *look* like a hard case.

One of the features I love about Ada is that the trig functions have
an optional argument saying how much a full circle is, so that you
can express angles like pi/2 precisely as "0.25 of a full circle.
Solaris has a full set of {sin,cos,tan,asin,acos,atan,atan2,sincos}pi()
functions with an implicit multiplication or division as appropriate by
an infinitely precise pi, which is nearly as good.

When it comes to inverse trig functions, I am leery of anything but
two-argument arc-tangent.  I beg you to examine every use of
math:acos/1, math:asin/1, and math:atan/1 and very carefully verify
that it is OK in that particular case to get only half the circle
back and that the half you get is in fact the half you want and to
DOCUMENT in the code what you expect and to test that you were right.

I think it's fair to say that floating point is not only weirder than
we imagine, it's weirder than most of us *can* imagine.




More information about the erlang-questions mailing list