DEV Community

loading...

Discussion on: Has type safety gone too far?

Collapse
pchinery profile image
Philip

What bugs can you possibly make by "accidentally" multiplying an integer by a double?

Well, in real-world mathematics, this is not a problem at all. But even though we usually think that floating point numbers are like real numbers, they actually are discrete numbers approximating a real value. This can lead to strange effects, when you add or subtract two numbers with largely different order of magnitude. Here is a good example:

stackoverflow.com/questions/210049...

To be honest, it often is not a problem in every day code, but it still is not a safe operation. So there is a good reason why the compiler says that you have to be explicit about it. (The syntax should be as easy as possible to not clutter the code though.)

So I don't think languages take type-safety too far and I am often happy to rely on a compiler to avoid stupid things instead of leaving this to the runtime. But in the end, that's a personal preference as well.

Collapse
tsupinie profile image
Tim Supinie

But even though we usually think that floating point numbers are like real numbers, they actually are discrete numbers approximating a real value. This can lead to strange effects, when you add or subtract two numbers with largely different order of magnitude.

Right, but that's a problem with how we represent numbers in computers, not with a lack of type enforcement. A better reason might be to get you to think about why you're trying to combine floats and integers in arithmetic operations, which seems like a weird thing to do. I'm open to examples in which that's "proper", though.