Live data from Hacker News

What every computer scientist should know about floating-point arithmetic (1991) [pdf]

itu.dk

31–40 of 55 posts

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#31
post #6

> 0.1 + 0.1 + 0.1 == 0.3 False I always tell my students that if they (might) have a float, and are using the `==` operator, they're doing something wrong.

I have a linter in my code that shouts at me if I use exact equality for floats.

But I regret not making an exception for the constant zero, because it's one of the cases where you probably should accept it. I.e. if (f != 0.0) {...}

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#32
post #17
post #8

Earlier quoted context omitted.

.125 + .375 == .5 You should be using == for floats when they're actually equal. 0.1 just isn't an actual number.

Are you saying that my students should memorize which numbers are actual floats and which are not? > 1.25 * 0.1 0.1250000000000000069388939039

> Are you saying that my students should memorize which numbers are actual floats and which are not?

Yes.

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#33
post #10

Earlier quoted context omitted.

That has more to do with decimal binary conversion than arithmetic/comparison. Using hex literals makes it clearer 0x1.999999999999ap-4 ("0.1") +0x1.999999999999ap-4 ("0.1") --------------------- =0x3.3333333333334p-4 ("0.2") +0x1.999999999999ap-4 ("0.1") --------------------- =0x4.cccccccccccf0p-4 ("0.30000000000000004") !=0x4.cccccccccccccp-4 ("0.3")

Absolutely nobody will think this is 'clearer', this is a leaky abstraction and personally I think that the OP is right and == in combination with floating point constants should be limited to '0' and that's it.

We all know that 1/3 + 1/3 + 1/3 = 1, but 0.33 + 0.33 + 0.33 = 0.99. We're sufficiently used to decimal to know that 1/3 doesn't have a finite decimal representation. Decimal 1/10 doesn't have a finite binary representation, for the exact same reason that 1/3 doesn't have one in decimal — 3 is co-prime with 10, and 5 is co-prime with 2.

The only leaky abstraction here is our bias towards decimal. (Fun fact: "base 10" is meaningless, because every base calls itself base 10)

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#34
post #4

One thing that really did it for me was programming something where you would normally use floats (audio/DSP) on a platform where floats were abysmally slow. This forced me to explore Fixed-Point options which in turn forced me to explore what the differences to floats are.

Fixed point gave rise to the old programmers meme 'if you need floating point you don't understand your problem'. It's of course partially in jest but there is a grain of truth in it as well.

It is quite old, attributable to VonNeumann and Goldstine in 1947. Later Goldstine joked that if rescaling for every step was easy enough for Johnny it ought to be easy for everyone else.

The gag here being that perhaps that isn’t the best dividing line for programming talent.

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#35
post #6

> 0.1 + 0.1 + 0.1 == 0.3 False I always tell my students that if they (might) have a float, and are using the `==` operator, they're doing something wrong.

I have a linter in my code that shouts at me if I use exact equality for floats. But I regret not making an exception for the constant zero, because it's one of the cases where you probably should accept it. I.e. if (f != 0.0) {...}

Zero shouldn't be an exception there. If f had been set from something like f = a - b, then you're in the same situation where f might be almost but not exactly zero.

The linter wouldn't know where f came from, so it should flag all floating point equality cases, and have some way that you can annotate it for "yeah this one is okay."

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#36
post #17
post #8

Earlier quoted context omitted.

.125 + .375 == .5 You should be using == for floats when they're actually equal. 0.1 just isn't an actual number.

Are you saying that my students should memorize which numbers are actual floats and which are not? > 1.25 * 0.1 0.1250000000000000069388939039

They shouldn’t “memorize” this per se, but it should take them only a few seconds to work out in their head.

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#37
post #6

> 0.1 + 0.1 + 0.1 == 0.3 False I always tell my students that if they (might) have a float, and are using the `==` operator, they're doing something wrong.

I have a relaxed rule for myself: if I’m using the == operator on floats, I must write a comment explaining why. I use == for maybe once a year.

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#38
post #17
post #8

Earlier quoted context omitted.

.125 + .375 == .5 You should be using == for floats when they're actually equal. 0.1 just isn't an actual number.

Are you saying that my students should memorize which numbers are actual floats and which are not? > 1.25 * 0.1 0.1250000000000000069388939039

Your students should be able to figure out if a computation is exact or not, because they should understand binary representation of numbers.

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#39
post #17
post #8

Earlier quoted context omitted.

.125 + .375 == .5 You should be using == for floats when they're actually equal. 0.1 just isn't an actual number.

Are you saying that my students should memorize which numbers are actual floats and which are not? > 1.25 * 0.1 0.1250000000000000069388939039

Tell them that they can only store integer powers of 2 and their sums exactly. 2^0 == 1. 2^-2 == .25. Then say it's the same with base 10. 10^-1 == 0.1. 1/9 isn't a power of 10, you you can't have an exact representation.

Re: What every computer scientist should know about floating-point arithmetic (1991) [pdf]

#40

Earlier quoted context omitted.

I have a linter in my code that shouts at me if I use exact equality for floats. But I regret not making an exception for the constant zero, because it's one of the cases where you probably should accept it. I.e. if (f != 0.0) {...}

Zero shouldn't be an exception there. If f had been set from something like f = a - b, then you're in the same situation where f might be almost but not exactly zero. The linter wouldn't know where f came from, so it should flag all floating point equality cases, and have some way that you can annotate it for "yeah this one is okay."

The idea is not to do it with values derived from arithmetic, but e.g. from measurements where a real zero is very unlikely and indicates something different.
Post reply on HN