5 ms·
It gives you correct answers to simple questions like this: > console.log(0.1 * 0.1) 0.010000000000000002
by ibiza 8y ago
It gives you correct answers to simple questions like this:
> console.log(0.1 * 0.1)
0.010000000000000002
- trcollinson 8y agoI’m not sure I get your joke. Every system, JavaScript, COBOL, C, Java, etc etc etc, has this behavior. Floating point decimals when converted to binary, are a rather rough thing for a system to handle. For example, 0.1 in binary is a repeating number. Basic operations on those binary values are very very fast but not always 100% accurate due to the binary nature of decimals. There are packages which allow for very precise floating point calculations but will cause performance degradation compared to the basic operations. It all depends on what you need.
- amelius 8y agoSee also: https://news.ycombinator.com/item?id=16852788 https://news.ycombinator.com/item?id=16852788
- wolfgang42 8y agoCOBOL uses fixed-point decimal arithmetic, not binary floating-point. You'd declare these variables as PIC V99 and then get a nice neat .10×.10=.01 (or as PIC 99V999 and get 00.100×00.100=00.010, or whatever). This is because it's a Business-Oriented Language, and businesses don't want floating point rounding errors in their finances. Incidentally, this was actually faster than binary floating-point on the machines for which COBOL was designed, which used EBCDIC and had special circuitry for arithmetic on binary-coded-decimal numbers. You could operate directly on the decimal representation without having to do any conversion for I/O.
- amelius 8y agoIntel x86 can also work natively with BCD numbers. https://en.wikipedia.org/wiki/Intel_BCD_opcode https://en.wikipedia.org/wiki/Intel_BCD_opcode
- bashook 8y agoVery cool. Unfortunately it's no longer supported in Long mode (x86-64). :-(
- upofadown 8y agoI used to do a lot of BCD in embedded POS stuff. No one will miss it. You can do anything you can do in BCD with regular integers if you have any understanding of integer arithmetic. Yeah, it is going to take a little more time to convert to/from decimal digits you can display/input but processors these days have fast integer division so that doesn't matter. A current issue is that a lot of programmers are bad at integer arithmetic. It isn't really taught in school.
- rst 8y agoNit: the COBOL spec dates to 1960; EBCDIC came later, ~1963. In 1960, character codes were specific to the manufacturer, and sometimes to the machine. IBM had a 6-bit BCD code that was mostly consistent across its line, but Univac had its own which was quite different.