Stange substraction operation

rekam

Active member
Joined
Oct 7, 2003
Messages
43
Location
Switzerland
Hello !
I have a very strange problem. Look at this :

Code:
Dim a, b, c as double
a = 1
b = 0.96

c = a - b
msgbox(c)

Why does the msgbox display 0.0399999999999999999 ?????? The result of this simple operation shouldnt be just 0.04 ?

Really, I dont understand. And the worst : when b < 0.95, c is correct :confused: :confused:

Well, has anybody already had such a problem ?
Thanks!
 
Its strange, but its the same problem...I think Ill cope that with Math.Round or something...even if its not, lets say, "professional" :D
 
I tried your example and my msgbox displayed 0.04, although if you debug the debugger will show 0.0400000000036.

I agree its weird, but I do not know why your msgbox didnt display 0.04 ....
 
If you want exact numerics use the Decimal data type. Double and Single are both floating point numbers and as such are prone to rounding errors.
 
Yeah, Decimal.Add/Subtract/Multiply combined with Decimal.Todouble should help.

e.g.
Dim a,b,c as double

b = 1234.555
c = 0234.554
a = Decimal.ToDouble(Decimal.Subtract(b,c))

should give a double of 1000.001
 
Back
Top