Bug? Simple Sum Decimal Bug

infow

Member
Licensed User
Longtime User
Hi,
I made a simple sum in a loop:


B4X:
Total = Total + biblio.IsNull(Cursor1.GetDouble("VALUE"),0)

Log(Cursor1.GetDouble("ACERTO") & " " & TotalAcerto)

Results:
0.0 0
194.45 194.45
-297.56 -103.11000000000001
103.11 -1.4210854715202004E-14
0.0 -1.4210854715202004E-14

After sum the negative -297.56 it gives a crazy number with lots of decimals.

Help Please,
 

infow

Member
Licensed User
Longtime User
The sum of 194.45+(-297.56), its not supposed generate .11000000000001 but only .11
Really strange behavior. I will try round it up.
 
Last edited:

infow

Member
Licensed User
Longtime User
The main problem its that we have to convert to string to again convert to double every sum, its that correct?
 

keirS

Well-Known Member
Licensed User
Longtime User
The sum of 194.45+(-297.56), its not supposed generate .11000000000001 but only .11
Really strange behavior. I will try round it up.

Using AGrahams BigNumbers library will solve your issues as you can set the precision for the numbers. It's a must if you are performing calculations involving currency (money) or anything which requires precision.

B4X:
Dim bg1 As BigDecimal
    Dim bg2 As BigDecimal
    bg1.Initialize("1.40")
    bg2.Initialize("165")
    bg2.Multiply(bg1)
    Log(bg2.ToString)
  
    Log(1.40 * 165)

The example gives the result of 231.00 for the BigDecimal calculation and 230.99999999999997 for 1.40 * 165.

By the way this isn't a bug it's how IEEE floating point arithmetic works.
 
Last edited:
Top