Skip to main content

Timeline for Why do you need float/double?

Current License: CC BY-SA 3.0

9 events
when toggle format what by license comment
Jun 27, 2011 at 22:06 comment added ChrisF @vartec - You still have to know that though.
Jun 27, 2011 at 21:56 comment added vartec in many countries rounding as done via binary representation is already officialy accepted by tax authorities.
Jun 27, 2011 at 21:37 comment added Aaronaught @acidzombie: The correct type to use for money is a fixed-point decimal with high (at least 4 decimal points) precision. No ifs, ands, or buts. Storing money values as cents is not sufficient, because in practice it only gives you two points of precision.
Jun 27, 2011 at 20:51 comment added ChrisF @Barry - I know. I was trying to illustrate the type of problem you get. Also a value like 0.175 does exist if the tax rate is 17.5% and you need to calculate the tax on an item that costs 10 cents.
Jun 27, 2011 at 20:48 comment added Barry Brown You should never multiply 10 (an integer) by .175 (a real/floating number) because you shouldn't mix exact numbers with inexact numbers; the result will be inexact. In other words, in a system of exact numbers, a value like .175 would never exist, and so this is a non-sensical calculation. A better solution is to multiply 10000 by 175 and manually insert a decimal point where appropriate.
Jun 27, 2011 at 20:37 history edited ChrisF CC BY-SA 3.0
clarification
Jun 27, 2011 at 20:36 comment added ChrisF @acidzombie24 - I didn't mean a specific type, but what ever type your language uses to represent money values. Also if you have 10 cents and multiply by 0.175 you have 1.75 cents - how do you deal with that with integer arithmetic? Is it 1 cent or 2 cents? Get it wrong and your customer could end up owning the tax man a lot of money.
Jun 27, 2011 at 20:35 comment added user2528 What is the money type? (language or reference link) and why is that the 'correct' type? Is it because its... 128bits or more something? My other why would using my 'tricks' be incorrect? You have a whole number by cent. If you multiple it by .175 you'll get a whole number and use it for whatever you want. Thinking about your example i think float would be able to hold my value with enough precision but i wont have to worry about 0.3f==0.3d being false. -edit- and +1
Jun 27, 2011 at 20:25 history answered ChrisF CC BY-SA 3.0