rekam Posted December 2, 2003 Posted December 2, 2003 Hello ! I have a very strange problem. Look at this : 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 shouldn't be just 0.04 ? Really, I don't understand. And the worst : when b < 0.95, c is correct :confused: :confused: Well, has anybody already had such a problem ? Thanks! Quote
BlueOysterCult Posted December 2, 2003 Posted December 2, 2003 Double has something to do with a 64 bit floating point Pi as Double is 3.1415926535 I think maybe use Single? Quote
rekam Posted December 2, 2003 Author Posted December 2, 2003 It's strange, but it's the same problem...I think I'll cope that with Math.Round or something...even if it's not, let's say, "professional" :D Quote
Mehyar Posted December 3, 2003 Posted December 3, 2003 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 .... Quote Dream as if you'll live forever, live as if you'll die today
Administrators PlausiblyDamp Posted December 3, 2003 Administrators Posted December 3, 2003 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
donnacha Posted December 19, 2003 Posted December 19, 2003 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 Quote Hamlet
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.