Aproximate expr evaluation predicate is/2 result to specific number of decimal places
Closed this issue · 2 comments
R0mb0 commented
System info:
- System: macOs 12.0.1 intel
- Gprolog Version: 1.5.0 (64 bit)
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
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
Thanks for your clarification!