[erlang-questions] erlang float comparison
Richard O'Keefe
ok@REDACTED
Wed May 16 00:43:40 CEST 2012
On 15/05/2012, at 9:04 PM, Angel J. Alvarez Miguel wrote:
>
> The problem can be stated as:
>
> Result = case Myfloat > 1.0 of
> true -> 1.0;
> false MyFloat
> end
> ...
> ...
> math:acos(Result).
>
> after that we came across some values that fail to pass the test > 1.0 but are seen
> as greater to the math:acos function rendering a bad arithmetic expression error
That is pretty astonishing, actually.
The obvious question is *what are these values*?
I've just written a test loop searching for such
values and so far haven't found any.
Is there any difference between emulated and native code?
Why aren't you worried about Myfloat < -1.0?
By the way, this seems a rather strange way to write
Result = if Myfloat > 1.0 -> 1.0 ; true -> Myfloat end
Do you know what trig library your copy of Erlang was linked
with? Here's the relevant code from fdlibm:
hx = __HI(x);
ix = hx & 0x7fffffff;
if (ix >= 0x3ff00000) { /* |x| >= 1 */
if (((ix - 0x3ff00000) | __LO(x)) == 0) { /* |x|==1 */
if (hx > 0)
return 0.0; /* acos(1) = 0 */
else
return pi + 2.0 * pio2_lo; /* acos(-1)= pi */
}
return (x - x) / (x - x); /* acos(|x|>1) is NaN */
}
There's no way _that_ could claim an IEEE double was greater
than 1.0 if it wasn't.
More information about the erlang-questions
mailing list