didoudiaz / gprolog

GNU Prolog
Other
106 stars 13 forks source link

Aproximate expr evaluation predicate is/2 result to specific number of decimal places #25

Closed R0mb0 closed 2 years ago

R0mb0 commented 2 years ago

System info:

Reproduction:

?- A is 0.77 / 10.

A = 0.076999999999999999

yes

?- A is 0.78 / 10.

A = 0.078

(1 ms) yes

?- A is 0.79 / 10.

A = 0.079000000000000001

yes

Question:

Is there a way to select the desired number of decimal places in the resulting expr ?

didoudiaz commented 2 years ago

Sprry, but currently, there is no way to control how the top-level displays floating numbers. Don't know if it helps but you can add a call to format/2 in your goal to display your float with a given number of decimals, e.g.:

| ?- X is 0.77/10, format('~3f',[X]).
0.077

X = 0.076999999999999999

If you don't want to see the X = 0.076999999999999999 shown by the top-level you can use a variable name beginning with underscore (such variables are not shown):

| ?- _X is 0.77/10, format('~3f',[_X]).
0.077
R0mb0 commented 2 years ago

Thanks for your clarification!