News for hackers. Stuff that matters.

Why do programming languages suck at math?

What's the whole reason behind distinguishing between integers and float/doubles?

units = 2

price = 3.17

2 * 3.17 = ERROR

It blows my mind that so much effort has put into things like functions taking functions as arguments, and the characteristics of classes - yet a computer can't handle basic calculator math out of the box. PHP, Ruby, Swift, OCaml.. what gives?

What is the complexity behind this?

( 13 comments | on HN )
Re:Why do programming languages suck at math? by ordu on Sunday June 25, 2017 @07:34PM (#14632849)
The most of programming languages suck even with integer numbers, their integers is more like ring of integers modulo 2^n.[1] Its not true for every programming language, but for languages like C, C++ it is. To implement math integers one will need to allow that integers to grow in size and to be more like strings. But such a engineering decision will hurt speed of program and raise memory usage. Though some languages do it. Common Lisp, for example, uses arbitrary precision numbers, and so you can get very big numbers while calculating without any special efforts like using external library for arbitraty precision numbers, GMP for instance.[2]

Computer numbers is not math numbers, and its better to always remember this fact.

[1] https://en.wikipedia.org/wiki/Multiplicative_group_of_intege...

[2] https://gmplib.org/

Re:Why do programming languages suck at math? by smt88 on Sunday June 25, 2017 @06:47PM (#14632659)
This has been answered pretty extensively on Q&A sites like StackOverflow and Quora, so I'd recommend Googling it for the most thorough, specific, and well-vetted answers.

Here are links to get you started:

- https://stackoverflow.com/questions/1089018/why-cant-decimal...

- https://stackoverflow.com/questions/5098558/float-vs-double-...

> It blows my mind that so much effort has put into things like functions taking functions as arguments, and the characteristics of classes - yet a computer can't handle basic calculator math out of the box.

Much effort was put into it, but no amount of work allows a computer to violate the laws of math. The fact that computers use binary and have limited memory isn't something that can be hand-waved away.

You're also talking about two totally different types of problems. Classes make programming languages easier to organize (at least theoretically) -- it's easy to change the syntax of a language, but hard to know exactly how it should look to be the easiest to use.

Re:Why do programming languages suck at math? by laynetrain on Sunday June 25, 2017 @07:12PM (#14632762)
Thanks for the links. It all seems like excuses.
Re:Why do programming languages suck at math? by smt88 on Sunday June 25, 2017 @07:16PM (#14632782)
Yeah, you're right. Computer scientists are whiny little babies who can't sit down and work hard enough to overcome the laws of mathematics. What a bunch of losers, focusing on designing programming languages instead of defying natural laws.

And you know what, why haven't they invented human-level AI yet? Are they just that lazy?

Don't even get me started on chemists... too lazy and stupid to learn a simple method to turn iron into gold.

Re:Why do programming languages suck at math? by laynetrain on Sunday June 25, 2017 @07:35PM (#14632850)
And we can't even time travel or leave the galaxy yet (◔_◔). C'mon scientists.

https://www.google.com/#safe=off&q=2*3.18

Re:Why do programming languages suck at math? by cratermoon on Sunday June 25, 2017 @07:48PM (#14632908)
Re:Why do programming languages suck at math? by kazinator on Sunday June 25, 2017 @07:49PM (#14632910)
One person's "excuses" may be another's "set of rationalizations for a collection of engineering requirements that aren't ideal in every dimension".
Re:Why do programming languages suck at math? by Gibbon1 on Sunday June 25, 2017 @08:36PM (#14633093)
When I've brought this up it's pretty obvious that while their were historical reasons behind it, not it because of cargo cult ideas of how numeric variables should work.

In any higher level language the default numeric variable should be an algebraic decimal float, not a binary int or float. Doesn't mean ints and floats shouldn't exist, but they shouldn't be the default.

Meaning when a programmer writes

   number x = 2/3;
number should equal 2/3. Not an approximation of 2/3.
Re:Why do programming languages suck at math? by dragonwriter on Sunday June 25, 2017 @07:55PM (#14632931)
> 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.

Re:Why do programming languages suck at math? by laynetrain on Sunday June 25, 2017 @09:03PM (#14633206)
It was actually working with Ruby and dollar/cents amounts for Stripe that led to this frustration, haha. Thanks for the insight!
Re:Why do programming languages suck at math? by microwavecamera on Sunday June 25, 2017 @08:59PM (#14633192)
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.
Re:Why do programming languages suck at math? by microwavecamera on Sunday June 25, 2017 @09:31PM (#14633370)
Short answer, no decimal in binary and math is hard. :) Here's a couple handy videos explaining it:

https://www.youtube.com/watch?v=PZRI1IfStY0

https://www.youtube.com/watch?v=Zzx7HN4wo3A&t=59s

Re:Why do programming languages suck at math? by sds111 on Monday June 26, 2017 @02:59AM (#14634653)
Simply put: Out compilers and interpreters force us humans to think more like the underlying machines, rather than vice-versa. It is ass-backwards. We should absolutely demand that our tools help the computers under what we want and not the other way around.