Live data from Hacker News

Floats Are Weird

a.exozy.me

111–120 of 120 posts

Re: Floats Are Weird

#111
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.

Well, not to be too glib, but I don't think they're too much stranger than I realize, and I think floats are much harder to reason about.

Well I didn’t count anyone who has taken a proper intro analysis course , but that’s safely within “most”.

Floats are a bit of a pain in the ass but there is nothing particularly strange about them.

The reals contain a lot of interesting structure, and things that are still being studied and understood.

Re: Floats Are Weird

#112

Earlier quoted context omitted.

I would argue the more correct answer is if you don't know what maths to use for money (including e.g. any legal rules about how you do it), you shouldn't be doing maths on money.

That strikes me as an unnecessarily elitist answer that holds us back. I'm hardly a mathematician, or even college educated, but AFAICT this all boils down to the fact that I can type a number into the computer, and it can't represent it exactly internally, so it mis represents it, silently . Where I come from, that's called "a bug", regardless of cause. Non-mathematicians (and even non-accountants and non-financiers…

That's not what I'm talking about. There are legal frameworks about how to compute something (which I only vaguely know exist, so I know enough that I personally should not be implementing any of this without significant outside input and expertise), and if you do it wrong we get deaths. This has been a major issue in at least two countries (the UK with the Post Office and Australia with Robodebt). I'd argue its more elitist thinking "I can program numbers into a computer so I can do anything involving numbers", rather than referring to non-software-developer experts.

On needing years in college to learn this: the "weirdness" of floats should be covered (it was for me) in high school science, and is also drummed in in first year science labs. Any time you actually need to work with numbers (rather than saying checking whether a group is abelian), you're dealing with how to compute and these rules predate electronics.

Re: Floats Are Weird

#113
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?

"Why should I believe in a real number if I can’t calculate it, if I can’t prove what its bits are, and if I can’t even refer to it? And each of these things happens with probability one! The real line from 0 to 1 looks more and more like a Swiss cheese, more and more like a stunningly black high-mountain sky studded with pin-pricks of light."

...see chapter 5 of Meta Math! by Gregory Chaitin: https://arxiv.org/pdf/math/0404335.pdf

Re: Floats Are Weird

#114
A number of years back I was programming in ADA, a language I generally liked, and had a case where the compiled version of a floating point constant differed from the value generated when the same string was entered at run time. Evidently, the compiler and the run time used different routines to convert strings to floats.

The difference wasn't much, but it was there.

Re: Floats Are Weird

#115
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.

Please stop repeating this nonsense. It's purely based on ignorance and only pushes more people into it.

This is universally repeated by people that have not written any modern financial software and who don't understand floats.

If all you do is add US dollars and pennies, then maybe you can get by with integers. Once you do anything else in modern finance integers puke completely. There's a reason scientific computing doesn't use integers, but prefers floats/double - they are much easier to use to get the best answers per compute.

For example, if you need to do anything with interest, which is fundamental, you'll soon find doubles are vastly better than bitsize equivalent integers, no matter what scaling/fixed-point/other tricks you employ. Add in currency differences (Yen to USD is a large multiplier, etc), any longer range calculations (50-100 year loans or flows), aggregation of varying items, derivatives, and on and on. Telling anyone in the field you're going to use integers will get you laughed at - the problem is not floats, the problem is the programmer hasn't taken a single class on numerical analysis and has not enough skill to do financial software.

For example, one of the simplest things one needs to do is compute compound interest, say computing mortgage tables, with say principal P (left), annual (or weekly, or daily..) rate R, for N years, periodic payment m, and you want to compute payments and schedule.

In reals, this is simple: each cycle you do something like

    r = (1+R/12)
    P -= m
    P = (1+r)*P
Then you write out values you need.

Trying to write this software with integers, no matter what scaling, fixed-point, shifting, and other tricks you employ, is going to be vastly more complex and error prone than simply using doubles. If you don't think so, pick a bit budget, say 32, or 64, for you base number type, and show me your code. Then I'll show you the naive one with doubles vastly outperforms your code.

This continues through all of modern finance.

So stop repeating this ignorance that one should use integers for money - that is purely a result of being ignorant about how to write robust numerical code, and only pushes people down a far worse rabbit hole when they hit issues with ints and try to patch them one at a time via ad-hoc hacks.

There's a reason scientists use floating point, not ints, to do real numerical work.

Re: Floats Are Weird

#116

Earlier quoted context omitted.

I would argue the more correct answer is if you don't know what maths to use for money (including e.g. any legal rules about how you do it), you shouldn't be doing maths on money.

That strikes me as an unnecessarily elitist answer that holds us back. I'm hardly a mathematician, or even college educated, but AFAICT this all boils down to the fact that I can type a number into the computer, and it can't represent it exactly internally, so it mis represents it, silently . Where I come from, that's called "a bug", regardless of cause. Non-mathematicians (and even non-accountants and non-financiers…

That’s because you haven’t looked at what floats are designed for. They were created for the purpose of high performance scientific computing, so they quite deliberately, and explicitly, trade off perfect accuracy for much greater performance.

Computers are perfectly good at providing infinite precision numbers and doing arithmetic on them, and most languages expose explicit number types for that purpose. But there’s a reason why floats are called floats, and not numbers. It’s because floats aren’t numbers (at least not base 10 numbers)! They’re a pretty accurate, but highly performant, approximation of base 10 numbers.

Re: Floats Are Weird

#117
post #106
post #102

Earlier quoted context omitted.

AFAIK in Europe numerical methods is mandatory in most Telco/EE degrees, but not in CS/SWE. The opposite happens with discrete maths btw

Here in Spain if you can't grasp numerical methods in CS you can't even think on earning a degree.

Here in Canada, CS is part of the Math Department at the University of Waterloo, but part of the Philosophy Department at University of Toronto.

Waterloo graduates take a Numerical Methods course in second year. I fear for the philosophers.

Re: Floats Are Weird

#118
post #97

Earlier quoted context omitted.

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.…

What does radix 10 floating point bring to the table in terms of precision other than a whole new set of rounding problems?

As far as I know, all accounting systems use 'round to nearest cent' integers (or whatever the lowest denomination is in a given currency). Is there a case where calculations using integer cents is in any way inferior to radix-ten fixed point?

Re: Floats Are Weird

#119

Earlier quoted context omitted.

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 gener…

> 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. Rationals of bigints doesn't work in general. First of all, you still have to do approximations if you take e.g. a…

Well, yeah, nothing works in general, which is why I prevaricated amongst several ideas -- but the key point is that if something has to be a default, floats work worst of all if accuracy or precision are your goals (but again, make sense for performance and performance alone -- but we really should be making accuracy the default and performance something you have to opt in to these days)

(Rational of bigints also has the advantage of being simple to understand, but I'd happily endorse pretty much anything but floats, even bigdecimals.)

Re: Floats Are Weird

#120
post #56

Earlier quoted context omitted.

> for example option prices are money amounts but floats are unavoidable in calculating them. How so? Can't you just use long integers with cents or 1/1000th of a cent?

Why would you? For most forward-looking calculations, the uncertainty of the future completely swamps any cent-rounding. Even for plain-vanilla bond price calculations, floats are the right tool for the job. Say you have a bond that pays $5 every year for 10 years, then $100. What's that worth today? Well, you have a forecast yield curve of interest rates. Say it's quoted as continuously compounded rates, so then you…

This is true in complex scenarios but not true in other finances scenarios. For example, there is a reason why any electronic exchange will use integers with implied decimal precision as the wire format and will continue to use such representations before and after encoding/decoding. We do not need to do hugely complex operations, it is mostly simple comparisons and some simple maths operations. We absolutely need exact precision and speed and it is difficult to get that when using doubles.

In parts of the stack where things are more complex and outside of the critical path, then yes, you use floating point.

Also, it isn't just that rounding the cents isn't worth it, it is that if you work with implied decimal integers with an implied 2DP, then you're going to end up with massively inaccurate results after a few operations.

Post reply on HN