4 ms·
This article seems to appear every so often on HN and Reddit. It used to be at sun.com. I'm glad Oracle didn't 404 it. The title isn't accurate though. The art
by jordanb 14y ago
This article seems to appear every so often on HN and Reddit. It used to be at sun.com. I'm glad Oracle didn't 404 it.
The title isn't accurate though. The article is mostly about using IEEE 754 to manage computational error, which is a pretty esoteric subject.
Most programmers spend precious little time using floating point and don't need to worry about computational error. Even among those who use it a great deal -- those doing numerical analysis -- computational error doesn't seem to be something they concern themselves with. My guess is that in most instances it's swamped by experimental error.
My thoughts on this article are:
1: It's certainly good that it's available online. It'd be better with a different title.
2: It's good that modern CPUs support IEEE 754 for the rare instance that people use it.
3: It's worth reading the first third or so of it, just to get an idea of what computational error is
4: The most important thing the "typical" programmer needs to know is that dollar values shouldn't be represented in floating point. Ideally they shouldn't be represented as integers either because the integer rounding truncates towards zero, which is not how an accountant would round.
- zerohp 14y ago> Most programmers spend precious little time using floating point and don't need to worry about computational error. I think this is true of experienced programmers, but I see inexperienced programmers tripping over floating point misunderstandings quite often. In almost every case they shouldn't have been using floating point at all.
- _3u10 14y agoI'm considering creating a programming language to be used on the internet, are you saying it would be a bad idea to make all numbers in my new language floating point?
- jacktoole1 14y agoIf you stick to integer math with 64-bit floating point numbers, then everything actually works out pretty reasonably, since all 32-bit ints can be represented exactly as 64-bit floats (doubles). As long as you use floor/ceil/round where appropriate, you get most of the benefits of integer arithmetic, without the type checking (which you lose in a dynamic language anyways). Some people feel strongly against this, but for dynamically typed language I enjoy its simplicity (for static languages, type systems are meant to enforce constraints on types, so multiple number types can be useful). It has done well in [at least] javascript and lua, and while the former is anything but an example in how to design a programming language, IMHO, the latter may be. So no, it wouldn't be a bad idea to make all numbers 64-bit floats. It sounds like you might likely be compiling to javascript, in which case using 64-bit floats will mean you have one less problem to deal with while implementing it.
- fleitz 14y agoIt's perfectly possible to generate accountant rounding using integers. If the accuracy required is one cent then the system must merely multiply by 10 before calculating and then use dutch round to the nearest 10 before dividing by 10. I'm pretty sure you could do it with quarter cent accuracy rather than 10ths of a cent. You just shouldn't calculate with 1 cent accuracy if the rounding methodology is important to your business, but truth be told most businesses have a lot more to optimize than the rounding methodology used.
- daniel-cussen 14y ago>> but truth be told most businesses have a lot more to optimize than the rounding methodology used. Perfectly true. I can see exceptions being managing other people's money--banks, accountants, and the IRS had damn well better get this right.