Inexact Conversion Result

Started by Robert, September 11, 2025, 02:23:37 PM

Previous topic - Next topic

MrBcx

#2
USING$ to the rescue:


DIM a![10]

FOR INTEGER i = 0 TO 9
    a![i] = i + 0.1
    PRINT USING$("#.#", a![i])
NEXT

I know Robert knows this, so the following is for anyone else who might be interested in this thread.
The precision issue that we see is related to how floating-point numbers are represented in binary.
For example, the value 1.1 cannot be exactly represented in IEEE 754 binary floating-point.   Our
compilers convert that to the closest representable double, which is 1.1000000238418579.   
No amount of formatting can recover the "intended" 1.1 because that information is already lost,
so we artificially compensate with functions like USING$()

Robert

Here's another "Watch out for !" example of inexactitude.

DIM a![10]

FOR INTEGER i = 0 TO 9
  a![i] = i + 0.1
PRINT a![i]
PRINT STR$(a![i])
?
NEXT



Result:


 0.1
 0.100000001490116

 1.1
 1.10000002384186

 2.1
 2.09999990463257

 3.1
 3.09999990463257

 4.1
 4.09999990463257

 5.1
 5.09999990463257

 6.1
 6.09999990463257

 7.1
 7.09999990463257

 8.1
 8.10000038146973

 9.1
 9.10000038146973