trealla icon indicating copy to clipboard operation
trealla copied to clipboard

Missing rounding in float/1 for bigint argument

Open Jean-Luc-Picard-2021 opened this issue 2 years ago • 11 comments

I get these results (note the 119042423827613008 is wrong):

/* Trealla Prolog 2.2.10 */
?- X is 51^10, Z is float(X).
   X = 119042423827613008, Z = 1.19042423827613e+17.
?- Z is float(51^10).
   Z = 1.19042423827613e+17.

But one would rather expect that float/1 performs HALF_EVEN rounding. The results should then be (1.1904242382761301E17 instead of 1.19042423827613e+17):

?- X is 51^10, Z is float(X).
X = 119042423827613001, Z = 1.1904242382761301E17.
?- Z is float(51^10).
Z = 1.1904242382761301E17.

That the (^)/2 result is wrong, is rather irrelevant, it is also:

?- Z is float(119042423827613008).
Z = 1.1904242382761301E17.

Jean-Luc-Picard-2021 avatar Sep 28 '22 09:09 Jean-Luc-Picard-2021

It's a printing error.

On Wed, Sep 28, 2022 at 7:33 PM Jean-Luc-Picard-2021 < @.***> wrote:

I get these results (note the 119042423827613008 is wrong):

/* Trealla Prolog 2.2.10 */ ?- X is 51^10, Z is float(X). X = 119042423827613008, Z = 1.19042423827613e+17. ?- Z is float(51^10). Z = 1.19042423827613e+17.

But one would rather expect that float/1 performs HALF_EVEN rounding. The results should then be (1.1904242382761301E17 instead of 1.19042423827613e+17):

?- X is 51^10, Z is float(X). X = 119042423827613001, Z = 1.1904242382761301E17. ?- Z is float(51^10). Z = 1.1904242382761301E17.

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/48, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEX5ECOEJBRKJUEKHODWAQGHRANCNFSM6AAAAAAQXSU4CE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

infradig avatar Sep 28 '22 11:09 infradig

I mean print rounding error. using 17 digits instead of 18.

On Wed, Sep 28, 2022 at 7:33 PM Jean-Luc-Picard-2021 < @.***> wrote:

I get these results (note the 119042423827613008 is wrong):

/* Trealla Prolog 2.2.10 */ ?- X is 51^10, Z is float(X). X = 119042423827613008, Z = 1.19042423827613e+17. ?- Z is float(51^10). Z = 1.19042423827613e+17.

But one would rather expect that float/1 performs HALF_EVEN rounding. The results should then be (1.1904242382761301E17 instead of 1.19042423827613e+17):

?- X is 51^10, Z is float(X). X = 119042423827613001, Z = 1.1904242382761301E17. ?- Z is float(51^10). Z = 1.1904242382761301E17.

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/48, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEX5ECOEJBRKJUEKHODWAQGHRANCNFSM6AAAAAAQXSU4CE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

infradig avatar Sep 28 '22 11:09 infradig

Yes it seems so:

?- Z is float(51^10), T is Z-1.1904242382761301E17.
   Z = 1.19042423827613e+17, T = 0.0

Jean-Luc-Picard-2021 avatar Sep 28 '22 11:09 Jean-Luc-Picard-2021

Yeah, can fix it by going to 18 digits of precision, but then you get things like 3.00000000000001 or 2.9999999999999997 (Scryer) for what should be integers.

On Wed, Sep 28, 2022 at 9:57 PM Jean-Luc-Picard-2021 < @.***> wrote:

Yes it seems so:

?- Z is float(51^10), T is Z-1.1904242382761301E17. Z = 1.19042423827613e+17, T = 0.0

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/48#issuecomment-1260796849, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSERIF6X6N2I4HVDNKN3WAQXC7ANCNFSM6AAAAAAQXSU4CE . You are receiving this because you commented.Message ID: @.***>

infradig avatar Sep 28 '22 13:09 infradig

Can you show a Prolog query where this happens? Also its not necessarily a display problem. For example many Math.pow(double,double) implementations cannot

do the problem correctly. They cannot or do not round to the theoretical optimum, they have inherently a higher error than only one Unit in the last place (ULP). So a certain error

is to expect. You see this here:

/* Dogelog Player, JavaScript */
?- X is 51**10.
X = 1.19042423827613E17.

?- X is float(51^10).
X = 119042423827613010.0.

In the above the (**)/2 is Math.pow(double,double) and the (^)/2 is exact bigint exponentiation. I took the pair (51,10) from a list of pairs where (**)/2 and (^)/2 often disagree

based on Math.pow(double,double) typically being weak.

Jean-Luc-Picard-2021 avatar Sep 28 '22 13:09 Jean-Luc-Picard-2021

When printing with DBL_DECIMAL_DIG-1 (17 i think) digits of precision

?- N9 is 370370367037037036703703703670 / 123456789012345678901234567890. N9 = 3.0. ?- X is 51^10, Z is float(X). X = 119042423827613001, Z = 1.19042423827613e+17.

when printing with DBL_DECIMAL_DIG (18 i think) digits of precision

?- N9 is 370370367037037036703703703670 / 123456789012345678901234567890. N9 = 3.0000000000000004. ?- X is 51^10, Z is float(X). X = 119042423827613001, Z = 1.1904242382761301e+17.

$ scryer-prolog -f ?- N9 is 370370367037037036703703703670 / 123456789012345678901234567890. N9 = 2.9999999999999996. ?- X is 51^10, Z is float(X). X = 119042423827613001, Z = 1.19042423827613e17.

On Wed, Sep 28, 2022 at 11:22 PM Jean-Luc-Picard-2021 < @.***> wrote:

Can you make show a query where this happens? I don't know how to reproduce 3.00000000000001 or 2.9999999999999997 which should be 3. After all 3 is an exact float.

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/48#issuecomment-1260907889, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSESP3H75CVQUQHAAGA3WARBARANCNFSM6AAAAAAQXSU4CE . You are receiving this because you commented.Message ID: @.***>

infradig avatar Sep 28 '22 21:09 infradig

$ swipl Welcome to SWI-Prolog (threaded, 64 bits, version 8.4.3) ?- N9 is 370370367037037036703703703670 / 123456789012345678901234567890. N9 = 3. ?- X is 51^10, Z is float(X). X = 119042423827613001, Z = 1.1904242382761301e+17.

On Wed, Sep 28, 2022 at 11:22 PM Jean-Luc-Picard-2021 < @.***> wrote:

Can you make show a query where this happens? I don't know how to reproduce 3.00000000000001 or 2.9999999999999997 which should be 3. After all 3 is an exact float.

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/48#issuecomment-1260907889, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSESP3H75CVQUQHAAGA3WARBARANCNFSM6AAAAAAQXSU4CE . You are receiving this because you commented.Message ID: @.***>

infradig avatar Sep 28 '22 21:09 infradig

I have opened a separate ticket for printing. Because this can get extremly confusing, if printing is not precise enough. The ticket is here:

not precise enough float printing https://github.com/trealla-prolog/trealla/issues/49

Jean-Luc-Picard-2021 avatar Sep 28 '22 21:09 Jean-Luc-Picard-2021

And i suppose X is 1/10 with 18 digits you get

$ tpl ?- X is 1/10. X = 0.10000000000000001.

(as does GNUProlog).

Versus a better looking 0.1 (SWI, Scryer, BProlog).

On Wed, Sep 28, 2022 at 11:22 PM Jean-Luc-Picard-2021 < @.***> wrote:

Can you make show a query where this happens? I don't know how to reproduce 3.00000000000001 or 2.9999999999999997 which should be 3. After all 3 is an exact float.

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/48#issuecomment-1260907889, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSESP3H75CVQUQHAAGA3WARBARANCNFSM6AAAAAAQXSU4CE . You are receiving this because you commented.Message ID: @.***>

infradig avatar Sep 28 '22 21:09 infradig

printing needs to satisfy. If this is not satisfied, its very hard to debug other errors, it will "mask" other errors, like erros or variations in arithmetic such

as (/)/2, (**)/2, (^)/2 and float/2:

read(write(X))) == X

in the 3.0 example its clear. In SWI-Prolog the result is different. Try subtracting 3 from the division? The 1/10 example, I don't know, maybe better discuss it here:

not precise enough float printing https://github.com/trealla-prolog/trealla/issues/49

Jean-Luc-Picard-2021 avatar Sep 28 '22 22:09 Jean-Luc-Picard-2021

Here you see SWI-Prolog computes another number, the issue is currently discussed in SWI-Prolog forum. They use mqr_get_d instead mqr_get_d_nearest or

somesuch is their problem. Also their a varying approachs to realize (/)/2, either with divmod(A,B,...) or simply (float)A/(float)B. Their system might also differ in this respect:

/* Trella Prolog */
?- N9 is 370370367037037036703703703670 / 123456789012345678901234567890 - 3.
   N9 = 4.440892098500626e-16.

/* SWI-Prolog */
?- N9 is 370370367037037036703703703670 / 123456789012345678901234567890 - 3.
N9 = 0.

Jean-Luc-Picard-2021 avatar Sep 28 '22 22:09 Jean-Luc-Picard-2021

The (^)/2 error is gone, I now find:

Trealla Prolog (c) Infradig 2020-2022, v2.7.15
?- X is 51^10.
   X = 119042423827613001

And the rest is a printing artefact, as already observed:

?- X is 51^10, Z is float(X), Z =:= 1.19042423827613008E17.
   X = 119042423827613001, Z = 1.19042423827613e+17.

Closing this ticket.

Jean-Luc-Picard-2021 avatar Jan 04 '23 08:01 Jean-Luc-Picard-2021