|
-
Jun 27th, 2006, 04:57 AM
#1
Thread Starter
Junior Member
.Net and Office calculate wrong
Hi,
There is a big problem in how all Microsoft products handles substractions. When you subtract two values the outcome is wrong.
This problem is found in VB6, .NET2001 and VS2005 as well in Excel.
Try this in Excel:
=79,8-84,2 and =79,7-84,1 both should give -4,4 but they give -4,40000000000001 and -4,39999999999999 (in Excel you must show all decimals otherwise it will round and it looks well)
In .NET this gives a problem when you compare the values with each other, they are NOT equal and they should be!.
This happens in many different cases! so be aware!!
Fred
-
Jun 27th, 2006, 05:00 AM
#2
Re: .Net and Office calculate wrong
Sounds like the evils of floating point math.
I cannot speak for the EXCEL part, but are you using SINGLE or DOUBLE in the VB6 or .Net attempts?
-
Jun 27th, 2006, 05:03 AM
#3
Thread Starter
Junior Member
Re: .Net and Office calculate wrong
Im using doubles (with singles the error is bigger). The strange part of this is when the outcome is 3 or 4 the problem does not exists......
-
Jun 27th, 2006, 05:07 AM
#4
Re: .Net and Office calculate wrong
When you do math on floating point datatypes the outcome is not accurate.
You must use CURRENCY or DECIMAL datatypes if you want accuracy. If you do not need the decimal point then use INTEGER datatypes.
It has to do with how the floating point datatype is stored - and how math is done on it.
We never use SINGLE or DOUBLE.
-
Jun 27th, 2006, 03:24 PM
#5
Re: .Net and Office calculate wrong
I don't know what =79,7-84,1 means but can you post your .NET code? (c0d)
-
Jun 27th, 2006, 03:33 PM
#6
Re: .Net and Office calculate wrong
That is a comma format instead of decimal point format - regional settings...
-
Jun 27th, 2006, 03:37 PM
#7
Re: .Net and Office calculate wrong
Ohh, that European thing always confused me.
-
Jun 27th, 2006, 03:49 PM
#8
Re: .Net and Office calculate wrong
There was an excellent thread on this in the VB6 forum....about three years ago. Somebody posted a thorough technical explanation of why this happens. If you wanted to dig for it, you mght find it....or get totally bored.
My usual boring signature: Nothing
 
-
Jun 30th, 2006, 11:09 AM
#9
Frenzied Member
Re: .Net and Office calculate wrong
Essentially, zero cannot be absolutely represented using the notation that M$ (and many others) use. Sometimes it's zero, othertimes it's very very very close to zero.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|