[erlang-questions] bignum sqrt broken in erlang?

Mikael Pettersson mikpe@REDACTED
Tue Aug 25 15:35:49 CEST 2009


On Tue, 25 Aug 2009 15:11:03 +0200, Angel Alvarez wrote:
> let ab=N
> let M=(a+1)(b+1);
> 
> M=ab+a+b+1  that is N +a +b +1  so M  > N
> 
> Ok but in erlang...
> 
> Erlang R13B01 (erts-5.7.2) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]
> 
> Eshell V5.7.2  (abort with ^G)
> 1> N=71641520761751435455133616475667090434063332228247871795429.
> 71641520761751435455133616475667090434063332228247871795429
> 
> 2> R0=trunc(math:sqrt(N)).
> 267659337146589062070609641472
> 
> 3> R1=R0+1.
> 267659337146589062070609641473
> 
> 4> M=R1*R1.
> 71641520761751431352030800763682813030352458120945601609729
> 
> 5> M -N > 0.
> false
> 
> where is the problem? That's erlang or the gmp library?

Not gmp because the Erlang runtime does all bignum arithmetic
on its own.

I suspect that math:sqrt/1 causes a conversion from bignum to flonum,
which loses precision.


More information about the erlang-questions mailing list