News for hackers. Stuff that matters.
Re:What Every Computer Scientist Should Know About Floating-... by hnick on Monday June 29, 2020 @01:40AM (#23674695)
The real problem I see mostly happen when arithmetic gets involved. Two simple and precise floats added or subtracted can get you something like 5.00000000001 if you aren't careful.

A fairly innocuous perl snippet shows this:

  DB<1> $a+=0.1 for 1..10; print $a - 1
  -1.11022302462516e-16
I ran into a bug like this on a website, I don't know what they were doing to the inputs but a simple 60.9 or similar turned into 60.9000000001 and tripped up the back-end service.
Re:What Every Computer Scientist Should Know About Floating-... by slavik81 on Monday June 29, 2020 @04:12AM (#23675522)
The problem is that they aren't precise floats. 0.1 has no finite representation in binary. It's an infinite repeating string of digits, like 1/3rd is in decimal. Thus, when you add a bunch of them together, the inherent error in rounding them off at some finite number of digits slowly adds up.

The same is true of all multiples of 0.1, unless they're also a multiple of 0.5. Hence why your 60.9 is not actually 60.9. It was never exact to begin with.

Re:What Every Computer Scientist Should Know About Floating-... by hnick on Monday June 29, 2020 @04:55AM (#23675728)
Yes, that is true. But usually symptoms don't surface until errors accumulate which is why I found that web form interesting.

'Precise' was the wrong word. What I meant was the system reports back the exact entered value 0.1 (rather than what it has in memory, which is 0.100000001490116119384765625 according to https://www.h-schmidt.net/FloatConverter/IEEE754.html). Most programs will do this fairly well.

I confirmed javascript is entirely capable of storing and retrieving the number I entered, so I'm not entirely sure what they were doing to confuse it :) Must have been some secret maths.

Re:What Every Computer Scientist Should Know About Floating-... by DarkWiiPlayer on Monday June 29, 2020 @09:20AM (#23677298)
> 0.1 has no finite representation in binary.

b1 / b1010

I don't see the problem. Did you mean to say it has no finite representation in the IEEE float format?

> It was never exact to begin with.

But the format sadly gets used in situations where people expect it to be reasonably accurate, where it isn't, because developers either underestimate the "reasonable" or overestimate the "accurate" part.

It's just sad that most mainstream programming languages have no native accurate data type for rational numbers.

Re:What Every Computer Scientist Should Know About Floating-... by slavik81 on Sunday July 12, 2020 @06:05PM (#23815156)
I meant in positional notation, but most people know that as "decimal notation". Unfortunately, that implies base 10, so I avoided the term.

I gave both an example in binary and an example in decimal to clarify my imprecise terminology. I worry that by objecting only to the binary example, you may confuse people into thinking the two are different.

Re:What Every Computer Scientist Should Know About Floating-... by account42 on Monday June 29, 2020 @11:12AM (#23678321)
So are you saying that ⅓ has a finite presentation in decimal? Finite binary rationals ⊋ finite binary numbers.
Re:What Every Computer Scientist Should Know About Floating-... by DarkWiiPlayer on Thursday July 02, 2020 @09:06AM (#23712706)
> So are you saying that ⅓ has a finite presentation in decimal?

Yes: ⅓

Re:What Every Computer Scientist Should Know About Floating-... by Gracana on Monday June 29, 2020 @10:55AM (#23678148)
> Did you mean to say it has no finite representation in the IEEE float format?

Of course that's implied.

Re:What Every Computer Scientist Should Know About Floating-... by DarkWiiPlayer on Thursday July 02, 2020 @09:10AM (#23712734)
It was a rhetoric question; I just wanted to point out that people blame these inaccuracies on "binary representations" when in reality it's just this one binary representation that's broken.