Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have two different parameter money and spend in c# code
money positive, spend negative value.

Two parameter type double in code and in mysql table.

I wrote ın code behınd select sum(result)as total from tablename where id=xxxxx

This return true valueee if your value money>spend

For ex 48 money spend -2 result 46 but

If ı try money 46 spend -46 money=spend result must be 0 zero but this select returns 7.105427357601E-15 whyy?

Please help me.

Thanks in Advance.
Posted
Updated 6-Jun-11 1:39am
v2

Don't use double for currency. Currency is always an integer number of pennies (or cents or whatever the smallest division in your currency of choice is), and unless you're running an investment bank or live in Zimbabwe, a long int is easily big enough (~4×1018) and has better precision than a double anyway.

Comparing floating point differences to zero is a well known area where precision issues introduced by the architecture spoil mathematically logical outcomes.
 
Share this answer
 
Comments
wellcometoend 6-Jun-11 9:39am    
ı live in turkey not zimbabwe ı know float int what ıs the meaning

ı solve this problem decimal(10,2)
Sergey Alexandrovich Kryukov 6-Jun-11 16:53pm    
Good point about Zimbabwe as well as comparison issue. (I voted 4.) This is the inherent problem of all computer technology and in fact the mathematical notion of a real number which cannot be accurately presented in any computing device in principle, because each potentially holds infinite information. It also depends how you define "money". If it is the operational day in bank and balance, this can be some integer type, but when you're calculating efficiency of investment or world-wide currency values, it's naturally a floating-point.
--SA
It should be 0, but claims to be 7.105E-15. They aren't far from each other.
That makes me think of a representation problem that Type double has:
It stores numbers as sum of different powers of 2. Let's call it "dual". We usually think in sums of different powers of 10 ("decimal").

Converting between the two representations can introduce errors. 1/10 (decimal) doesn't have an exact equivalent in dual as long as you don't use an infinite number of digits.

But IIRC, problems should strictly stick to non-integral numbers.

Does your problem really arise with the mentioned 46 as an example?
Then please ignore this post completely.

Or is it converted somewhere to 0.046?
Then another data type could help. In C#, that would be decimal. Maybe, there is something similar in MySQL.
 
Share this answer
 
Comments
BobJanova 6-Jun-11 8:58am    
I don't think there is a direct database equivalent of decimal, which is why I didn't suggest it, but if I am wrong about that, your answer is correct.

And yes, floating point should be able to represent small integers accurately, but if the '46' is the result of summing over several numbers which themselves are non-integer, it won't necessarily get it back,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900