gprolog icon indicating copy to clipboard operation
gprolog copied to clipboard

formatting floats (adaptive 16 or 17 decimal precision)

Open Jean-Luc-Picard-2021 opened this issue 6 months ago • 0 comments

Currently GNU Prolog shows me:

/* GNU Prolog 1.5.0 */

?- X is 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1.
X = 0.79999999999999993

?- 0.79999999999999993 == 0.7999999999999999.
Yes

?- X is 23/10.
X = 2.2999999999999998

?- 2.2999999999999998 == 2.3.
Yes

An algorithm which would first probe 16 decimal precision, could produce 2.3 and 0.7999999999999999, which looks to me the more common result among Prolog systems,

and is shorter and still valid result. You can try SWI-Prolog:

/* SWI-Prolog 9.3.8 */

?- X is 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1.
X = 0.7999999999999999.

?- X is 23/10.
X = 2.3.

Jean-Luc-Picard-2021 avatar Jul 28 '24 14:07 Jean-Luc-Picard-2021