From: neuneudr on 18 Sep 2009 09:28 On Sep 15, 5:57 pm, "Frank Cisco" <tdyjkdftyujdt...(a)dtyjdtyjdtyjdty.com> wrote: > cheers all! why on earth is float used at all then if it's so inaccurate? While there are good reasons, that were stated here, it's probably safe to say that floating-point --both float and double-- are way overused by people that don't really understand how they work nor when they should be used. A typical mistake being to represent a monetary amount that has two significant number after the dot (eg $123.45) using floating-point numbers (one solution being to think not in term of 'dollar' but in term of 'cents' and hence storing directly 12345). People often fail to realize that using integer math could ease their pain (as long as the range of values fit in an integer or a long or a BigXXX). Like you can go to great lenght and write complicated compensated sum algorithm to compute variance... Or you can preprocess your data, use integer math, and have the result to the exact, provable, precision you want, without needing to fear errors accumulating.
From: markspace on 18 Sep 2009 10:53 neuneudr(a)yahoo.fr wrote: > use integer math, and have the > result to the exact, provable, precision you want, without > needing to fear errors accumulating. Eww. Sorry, there might have been good reasons to do this at one time... like 1980... but there's no way I would spend the time or headache trying to replace floating point math with integer math today. If anything I'd say this sort of operation requires far more expertise than just using the existing floating point and double primitives.
From: Thomas Pornin on 18 Sep 2009 17:24 According to markspace <nospam(a)nowhere.com>: > Sorry, there might have been good reasons to do this at one time... like > 1980... but there's no way I would spend the time or headache trying to > replace floating point math with integer math today. For software related to money, integer calculation is mandatory. Bankers really hate it when fractions of cents disappear during processing. For conversions between currencies and trading, there are precise rules to follow, finely tuned to avoid stability issues; the Java FP types are not good for that. On the other hand, the Java "long" type, with its guaranteed 64 bits and fully specified behaviour, is quite appreciated. --Thomas Pornin
From: Martin Gregorie on 18 Sep 2009 17:33 On Fri, 18 Sep 2009 07:53:36 -0700, markspace wrote: > neuneudr(a)yahoo.fr wrote: >> use integer math, and have the >> result to the exact, provable, precision you want, without needing to >> fear errors accumulating. > > > Eww. > > Sorry, there might have been good reasons to do this at one time... like > 1980... but there's no way I would spend the time or headache trying to > replace floating point math with integer math today. If anything I'd > say this sort of operation requires far more expertise than just using > the existing floating point and double primitives. If you're doing financial calculations you should always use integer arithmetic because this avoids nasty surprises due to the inability of floating point to accurately represent all possible values. This also applies to currency conversion. All FX dealing institutions and markets specify precisely how to handle the conversion: using floating point doesn't figure in their rules. All reputable financial software uses integers with monetary values held in the smallest legal unit in the currency (cents in $US and Euros, pence in the UK). AFAIK use of floating point only started when people tried to handle monetary amounts on the early 8-bit micros - the early BASICs would only handle 16 bit signed integers, so people who should have known better used floating point to handle values larger than 327.68 and managed to blind themselves to the rounding errors and limitation to 8 significant figures. Unfortunately, this habit has carried over into spreadsheets but there's no need to propagate it into Java as well. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
From: Patricia Shanahan on 18 Sep 2009 18:21
Martin Gregorie wrote: > On Fri, 18 Sep 2009 07:53:36 -0700, markspace wrote: > >> neuneudr(a)yahoo.fr wrote: >>> use integer math, and have the >>> result to the exact, provable, precision you want, without needing to >>> fear errors accumulating. >> >> Eww. >> >> Sorry, there might have been good reasons to do this at one time... like >> 1980... but there's no way I would spend the time or headache trying to >> replace floating point math with integer math today. If anything I'd >> say this sort of operation requires far more expertise than just using >> the existing floating point and double primitives. > > If you're doing financial calculations you should always use integer > arithmetic because this avoids nasty surprises due to the inability of > floating point to accurately represent all possible values. This also > applies to currency conversion. All FX dealing institutions and markets > specify precisely how to handle the conversion: using floating point > doesn't figure in their rules. I would agree with this with two exceptions: 1. There are some financial calculations for which extreme exactness does not matter, and having transcendental function approximations does. For example, calculations of the form "How long will it take me to pay off my home loan if I do X?". 2. In some cases, the required rounding rules exactly match one of the BigDecimal rounding schemes. In that case, I would consider BigDecimal for convenience with the scale factor equal to the correct number of digits after the decimal point. Patricia |