[erlang-questions] math:pow(X, Y).

Richard O'Keefe ok@REDACTED
Tue Mar 20 23:06:39 CET 2012


On 20/03/2012, at 11:10 PM, 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 about cases where
> you've exceeded what can be represented in double precision floating
> point, in computing an exponential.

Raising to a power is not one of the operations defined by the IEEE
standard.  (Getting that right to half an ULP is a _lot_ harder than
getting + and / right to have an ULP.)  So the IEEE standard has
nothing whatever to say about math:pow/2.

One may argue by analogy that
 - EITHER it should return an appropriately signed Infinity
 - OR it should raise an exception (the Overflow exception).
whichever is consistent with the behaviour of * .

1> 1.0e200 * 1.0e200.
** exception error: bad argument in an arithmetic expression
     in operator  */2
        called as 1.0e200 * 1.0e200
2> math:pow(1.0e200, 2).
** exception error: bad argument in an arithmetic expression
     in function  math:pow/2
        called as math:pow(1.0e200,2)

The behaviour of math:pow/2 *is* consistent with the behaviour of
simple multiplication, so no IEEE worries there.  What _is_
questionable is that all the arguments are perfectly OK; the
problem is *not* a bad argument but a floating point overflow,
and so it should be labelled.

Now full conformance to IEEE would mean that exception/infinity
behaviour would be switchable at run time.  Presumably each Erlang
thread would have to have its own copy of the floating point state.
At this point it starts getting complicated, and for typical Erlang
applications, not _usefully_ so.



More information about the erlang-questions mailing list