Hacker News new | ask | show | jobs
by dragonwriter 3284 days ago
> What's the whole reason behind distinguishing between integers and float/doubles?

Because historically CPUs worked efficiently natively on integers, and everything else was software. (Modern CPUs also work efficiently natively on floats, but with different representation and operations.)

Note that not all languages (even o n your list) have the problem you describe though; plenty of automatic coercion with numeric operators so, e.g., float times int works (doing a float operation). Ruby, for instance, does this.

And many also do the calculation you present exactly (not merely without errors) because they treat decimal literals as exact numbers (using either a decimal or rational type, rather than binary floating point.) Perl 6 and Scheme, for instance. I think Haskell also can, if the right priority for numeric literal types is set.

> What is the complexity behind this?

It's not really complex, it's just a matter of prioritizing making performance vs. accuracy & generality simple, with a dash of consideration of programming history which shapes expectations.

2 comments

It was actually working with Ruby and dollar/cents amounts for Stripe that led to this frustration, haha. Thanks for the insight!
I just did a quick test on my comp with python 2.7.13, perl 5.24, perl 6, ruby 2.3.3 and php 7 and they all worked.