ciao-lang / ciao

Ciao is a modern Prolog implementation that builds up from a logic-based simple kernel designed to be portable, extensible, and modular.
https://ciao-lang.org
GNU Lesser General Public License v3.0
268 stars 20 forks source link

Rounding glitch from bigint to float #77

Open Jean-Luc-Picard-2021 opened 1 year ago

Jean-Luc-Picard-2021 commented 1 year ago

SWI-Prolog has released a new release 8.5.20 with better float rounding. Comparing we found:

/* SWI-Prolog 8.5.20 Ok */
?- X is float(166153499473114502559719956244594689).
X = 1.6615349947311452e+35.

/* Ciao Prolog Nok */
?- X is float(166153499473114502559719956244594689).
X = 1.661534994731145e35 ?

Our guess is that this conversion routine doesn't work for HALF_EVEN mode:

flt64_t bn_to_float(bignum_t *bn) {
while (i > 1) {
f = f*norm2 + u;
https://github.com/ciao-lang/ciao/blob/master/core/engine/eng_bignum.c#L1043
jfmc commented 1 year ago

Python

>>> float(166153499473114502559719956244594689)
1.6615349947311452e+35
>>> 

Javascript

> 166153499473114502559719956244594689
1.6615349947311452e+35