4 ms·
Ooh, floating-point arithmetic in software making financial transactions!
by TazeTSchnitzel 13y ago
Ooh, floating-point arithmetic in software making financial transactions!
- deleted 13y ago[deleted]
- stinos 13y agoindeed. (int)round($info['balance'] * 100000000) such constructs are not a good sign. Quick glance over the rest of the code also confirms this. Way too much room for improvement (and that is a friendly way of saying it).
- makomk 13y agoThat's harmless and more or less unavoidable in this case. While the official Bitcoin client uses safe fixed-point arithmetic internally, the JSON API it exposes uses floating point all over the place. This isn't a problem so long as you correctly convert the data back to fixed point as soon as you get it before doing any other arithmetic on it, exactly as this code is doing. (All the numbers are guaranteed to be small enough that this isn't going to lead to rounding errors if done correctly.)
- JumpCrisscross 13y agoPardon my naïveté - why would floating point arithmetic be a red flag in transactional code?
- route66 13y agoIt's less about the transactions but the money. >> 3 * 1005.01 => 3015.0299999999997 This might get truncated to 3015.02 and might >> 3 * 1005.01 * 10000 => 30150299.999999996 turn into a real loss.
- brianpgordon 13y agoLike Superman 3!
- stingraycharles 13y agoFloating point values are inaccurate and a complete mess if you want to get accurate results. This is a good article about the subject: http://www.johndcook.com/blog/2009/04/06/numbers-are-a-leaky-abstraction/ http://www.johndcook.com/blog/2009/04/06/numbers-are-a-leaky...
- reikonomusha 13y agoThis is patently false. Floats are not "inaccurate" and are not a "mess". They are well-defined and quite useful in a huge domain of applications.
- stingraycharles 13y agoSorry, I should've been more clear. They are well defined and useful in a huge domain of applications, especially in those areas where an approximation is perfectly fine. For representing money, however, you need to be very, very careful and be aware of the many subtle bugs that can be introduced when using floats. Also, that something is "well-defined" does not imply that is it not a mess. There are quite a few well-defined standards out there that clearly are a mess.
- reikonomusha 13y agoYou are right. For money, floating point computations are entirely inappropriate, but not because floating point is (supposedly) a "mess", but because floating point sacrifices uniform quantization for an exponential range of representable values. The IEEE-754 standard is not a mess. It is complicated, because it has to be. Extreme care and precision must be taken when talking about mathematical quantities which are neither closed nor associative under the provided operations. I challenge anyone else to make a concrete standard which supports accurate transcendental operations with numbers which range from 0 to 10^300 while remaining simple and easy to understand.
- nolite 13y agodid you not read the parent threads?
- fsloth 13y agoBecause, due to the limited precision available to floating points, they are not precise. Example, given floats a, b, and c c = a - b might result in c == a There are volumes of literature related to this. The short explanation why it is so, is that there is an infinite range of numbers, but we do not have infinite amount of memory, Therefore, to perform operations on very large (or very small) numbers efficiently (on current computer architecture) you need to round things around. If you do not have time for a degree in maths, then at least go and read this before operating on any instructions on floating point in production code, thanks: "What Every Computer Scientist Should Know About Floating Point Arithmetic" http://www.validlab.com/goldberg/paper.pdf http://www.validlab.com/goldberg/paper.pdf and http://en.wikipedia.org/wiki/IEEE_floating_point http://en.wikipedia.org/wiki/IEEE_floating_point
- damian2000 13y agoFor all of the reasons posted by others here, but in addition, using long integers or something similar to represent money is cleaner and faster. You just store the value in the lowest denomination, e.g. cents.
- beagle3 13y ago(0.3+0.3+0.3) <> 0.9. In fact, 0.9-0.3-0.3-0.3==~1.11e-16 in any IEEE 754 implementation. Now, with proper rounding in the right places, it will all work out. However, what rounding and where is much harder than using fixed point. As long as it's just addition and subtraction, you'll mostly accumulate small errors -- however, once you multiply or compare numbers, all bets are off.
- reikonomusha 13y agoAddition and subtraction can lead to enormous absolute errors. I'm fact, subtraction is one of the biggest culprits for causing instability.
- beagle3 13y agoMostly if followed by multiplication or division. While it is possible to get any magnitude of error with just addition and subtraction, that's usually not the case when just adding and subtracting numbers that are within 8 orders of magnitude, which is usually the range of money (assuming you're using double precision)
- wpietri 13y agoOne additional answer on this: money isn't a float. The dollar, for example, is an integer number of cents. A good way to represent this is using Fowler's Quantity pattern: http://martinfowler.com/eaaDev/quantity.html http://martinfowler.com/eaaDev/quantity.html That lets you encapsulate the ton of special behavior that money has. For example, for most software money can't be created or destroyed, just transfered.
- sandGorgon 13y agoThis is very interesting - is this kind of a pattern used in real financial software, or is this just an idea ?
- dan15 13y agoSome software will use a specific money or decimal type (some databases support these). Others will store money as an integer in the smallest units needed. ie. if you only need dollars to the nearest cent, store the number as cents ($1.50 is stored as 150)
- pimpl 13y agoCheck this video out, it’s a really good explanation. http://www.youtube.com/watch?v=PZRI1IfStY0 http://www.youtube.com/watch?v=PZRI1IfStY0