Live data from Hacker News

Floats Are Weird

a.exozy.me

91–100 of 120 posts

Re: Floats Are Weird

#91
post #89

Earlier quoted context omitted.

the axiomatization of floating point arithmetic is much larger than the axiomatization of real arithmetic, so it is reasonable to describe the former as more complicated.

Complexity of the definition isn’t particularly interesting, the properties of the object being defined can be. The reals are stranger than most people realize.

What's a strange thing about reals?

Re: Floats Are Weird

#92
post #86

Ages ago, when I was in college, a "numerical methods" course was more or less obligatory... has that changed? Anyway, toying with a slide rule is a fun way of making these things very obvious, because you are essentially working with something like 3-digit floating point. When you find yourself subtracting 1.234 from 1.235, but you know that both the "4" and the "5" are really fuzzy around the edges, it becomes obvi…

I assume it depends on the department. CS students are afraid of non-integer values, engineering students are afraid of codes longer than 200 lines. Just kidding, mostly.

I do somewhat wonder if numerical methods have suffered from their success; like everybody uses LAPACK via Numpy but understanding it under the hood, that job always belongs to the next department over wherever you are, haha.

Re: Floats Are Weird

#93
post #47

Re money systems: I have found that problems arise no matter whether floats are used or something else: The system can be too exact. My current approach is that I try to guess (or ask) how a customer understands or checks a statement/invoice. The customer usually takes a calculator and enters the rounded numbers they see. I then try to do all programmatic calculations exactly the same way: Operation - round - operati…

I thought "money systems" always worked in whole units of some fraction of your currency, e.g. "millicents" or similar, and shunned floating point arithmetic like the plague. > The system can be too exact Indeed, high precision, low accuracy! Fixed-point arithmetic can be both precise and accurate if everyone agrees on the same system. Maybe we don't, and that's the problem?

money systems work with whatever tool is convenient to the operations staff which these days is typically Microsoft Excel and Visual Basic, neither of which come with a built in Decimal type.

this will typically get transmuted through multiple layers of javascript, xml, back end java, cobol, SQL, python, some domain specific language, and back again.

Re: Floats Are Weird

#94

i think it is our educational system that is weird. we teach students from arithmetic to differential equations assuming they are working with Real Numbers and never really talking about what that means or what a Field is. Then we present computation as something that can easily be done with Real numbers. Which is a lie because we don't even know if Pi plus E is rational or not. Then we present computers as tools of…

Great points, let me riff on that though -- what's really "weird" (but totally understandable in the historical context of underpowered computers until recently) is that we think of floats as the default representation of a real at all, when more reasonable modern default choices for all purposes outside of high performance applications would either be a rational composed of bigints, or a bigdecimal, or maybe a generating function for the coefficients of a Taylor series expansion or something like that if you really want to get nuts. In any case you can have as much precision as you can afford, although for anything but rationals it'll be an approximation always, and the question is just how many digits you care for. A lot of choices but the only one that's really absurd is the actual default of floats which are terrible for every purpose except their original raison detre, which was performance in a time when computers were so slow that buying a floating point unit to put in your computer was a normal desirable upgrade for a PC owner (and still crucial for high thoroughput computing, video games, graphics rendering, ML, etc)

I do remember being taught the difference between rationals and irrationals in high school.

Another aside... The really crazy difference is between the definable and non definable numbers. The first which includes everything from pi to every busy beaver number is countably finite just like the integers, only the nondefinables (numbers whose shortest mathematical definition would not be finite or not exist) give the reals their uncountably infinite nature

Re: Floats Are Weird

#95
post #89

Earlier quoted context omitted.

Complexity of the definition isn’t particularly interesting, the properties of the object being defined can be. The reals are stranger than most people realize.

What's a strange thing about reals?

to start, there are an uncountable infinity of them so 100% are not representable. Also when notated typically, there are 2 representations of lots of numbers.

Re: Floats Are Weird

#96
post #17

I decided years ago that the next time I hear someone suggesting we use floats / doubles to represent money amounts, I am going to punch them in the face.

violence typically does not solve issues of rounding

Re: Floats Are Weird

#97
post #47

Re money systems: I have found that problems arise no matter whether floats are used or something else: The system can be too exact. My current approach is that I try to guess (or ask) how a customer understands or checks a statement/invoice. The customer usually takes a calculator and enters the rounded numbers they see. I then try to do all programmatic calculations exactly the same way: Operation - round - operati…

I thought "money systems" always worked in whole units of some fraction of your currency, e.g. "millicents" or similar, and shunned floating point arithmetic like the plague. > The system can be too exact Indeed, high precision, low accuracy! Fixed-point arithmetic can be both precise and accurate if everyone agrees on the same system. Maybe we don't, and that's the problem?

IBM partially has a hold on banking because they have supported decimal floating point. By switching binary floating-point from radix 2 to radix 10 (decimal exponents) they get rid of some of the problems.

IEEE has had these Decimal types for a while, but IBM is the only company shipping CPUs with them.

Fixed decimal is still used where appropriate, but Decimal floating point is a critical feature for some processes.

While personal taxes typically drop the cents, tariffs and other taxes are often small percentages as an example.

There are software implementations if you don't need performance.

But Decimal64 has a lot more represented values and it is easier to trap on some edge cases.

As Decimal floating point types were added into C23 we will see if hardware support follows.

Note this is also why HP calculators were more accurate for a given bit size, because the engineering ones used radix 10 floats.

Re: Floats Are Weird

#98
post #81

It’s not so much that floats are weird, but that they are not reals. Most of the problems happen due to confusion about this fact. Reals are much weirder/more interesting than floats, imo.

the axiomatization of floating point arithmetic is much larger than the axiomatization of real arithmetic, so it is reasonable to describe the former as more complicated.

yeah i have never really seen someone breakdown the axioms of floating point mathematics, like how do you define addition in a system that is not closed under "normal" addition.

Re: Floats Are Weird

#99

Earlier quoted context omitted.

the axiomatization of floating point arithmetic is much larger than the axiomatization of real arithmetic, so it is reasonable to describe the former as more complicated.

yeah i have never really seen someone breakdown the axioms of floating point mathematics, like how do you define addition in a system that is not closed under "normal" addition.

mathematicians have spent a lot of time investigating objects that lack parts of the normal algebraic structure. In a way it's fields like the real numbers that are the unusual (rather than "normal").

Re: Floats Are Weird

#100
That's called the stability of the algorithm. The first algorithm is not stable whereas the second with the logarithm is.

Another example: compare the two algorithms, that are actually pure-mathematically equal:

  def fc(x): 
      return sqrt(x+1) - sqrt(x)
  def fd(x): 
      return 1/(sqrt(x+1) + sqrt(x))

For large values of x (1e16 for example) plot the results and see the difference.

  xs = np.linspace(10\*14,10\*16,10000) 
  plt.figure(figsize=(8, 6), dpi=120) 
  plt.plot(xs,[fc(x) for x in xs]) 
  plt.plot(xs,[fd(x) for x in xs])
Post reply on HN