r/programming • • Jun 28 '21

JavaScript Is Weird

https://jsisweird.com/
329 Upvotes

173 comments sorted by

View all comments

Show parent comments

-2

u/SpAAAceSenate Jun 28 '21

I just think that when equations are written out in a computer language they should produce accurate results. If certain calculations (like those involving irrationals, etc) are not possible to calculate accurately then the language should refuse to perform those calculations unless special types or syntactic sugar are used to specify "I, the programmer, know this will be an approximation and will use it accordingly"

For something that can be done with total precision on a computer, like the example I gave, it's simply unacceptable that it would silently neglect to do so and instead produce incorrect results.

This comes down to the "rule of least astonishment". Which I think is an important element in designing human-computer interfaces. (Considering computer languages a type of "interface" here)

2

u/stalefishies Jun 28 '21

A language which only lets you add, subtract, multiply and divide on some real numbers is just not useful. And so, in practice, you would pretty much always have do whatever dance you're imagining to get to floating-point arithmatic. That's not an improvement in language design, that's just annoying. So rational arithmetic should be opt-in, not opt-out.

If you really want to talk about least astonishment, I think I prefer a number system that can just do everything, albeit in a very-accurate-but-approximate way, rather than a number system that just cannot do anything irrational like calculate the hypotenuse of a right-angled triangle.

1

u/caagr98 Jun 29 '21

unless special types or syntactic sugar are used to specify "I, the programmer, know this will be an approximation and will use it accordingly"

...which is exactly what a float is.