[erlang-questions] math:pow(X, Y).
Matthias Lang
matthias@REDACTED
Tue Mar 20 13:47:47 CET 2012
On Tuesday, March 20, Michael Turner wrote:
> "As these are the C library, the bugs are the same."
> I'm not sure what the IEEE floating point spec says
The 'math' module doesn't mention IEEE. There was a mention of it in a
thread about NaN a few weeks ago:
http://erlang.org/pipermail/erlang-questions/2012-February/064713.html
But anyway, I'm interested in understanding, and, to do that, just follow
the clues:
Clue #1: The man page says it's "just the C library"
Clue #2: The C library on your machine probably has documentation for
what pow() does. On mine, it says:
RETURN VALUE
On success, these functions return the value of x to the power of y.
If x is a finite value less than 0, and y is a finite noninteger, a
domain error occurs, and a NaN is returned.
If the result overflows, a range error occurs, and the functions return
HUGE_VAL, HUGE_VALF, or HUGE_VALL, respectively, with the mathematiâ
cally correct sign.
If result underflows, and is not representable, a range error occurs,
and 0.0 is returned.
Clue #3: Take a look at the Erlang source. It's in erl_math.c, which
is just 233 lines long, so nothing hard. Looking at it, it
funnels all functions through math_call_2() or math_call_1(),
and that checks that the output is finite (in the macro
ERTS_FP_ERROR_THOROUGH).
Finished. If the underlying C function returns NaN or INF, then Erlang
is going to throw a badarith, and that's true for all the functions in
the math module.
There's one dangling thing: I expect to see badarith, but the pretty printed
error suggests a badarg:
8> math:pow(20,900).
** exception error: bad argument in an arithmetic expression
in function math:pow/2
called as math:pow(20,900)
The non-pretty one doesn't:
9> catch math:pow(20,900).
{'EXIT',{badarith,[{math,pow,[20,900],[]},
The pretty error comes from stdlib/src/lib.erl. Maybe "error evaluating
arithmetic expression" would cover things better.
Matt
More information about the erlang-questions
mailing list