153 points · by signa11 · 3y ago · 102 comments
Yeah, the actual implementation is fairly different, but that’s just computer gibberish. Most of us no longer need to think of strings as an offset into memory to an array of characters that happen to be interpretable as a group, but that’s really what it is despite any abstractions.
The real mystery is why most modern languages continue to use floats and integers as the default numeric types rather than some sort of BigDecimal representation, given that precision and programmer intuitiveness trumps raw performance in the vast majority of cases.
And we use floats for much the same reason as you would use scientific notation. It is faster and, at large, "significant digits" don't go away just because you can write more digits.
Appeals to intuition are tough. Intuition is not some intrinsic property of the number system. It is a learned property. There is really no reason we couldn't be better at teaching it.
And with an exponent bias which isn't part of what anyone learns as basic scientific notation. Used for reasons of efficiency that don't have anything to do with the efficiency humans have in reading the exponent of scientific notation.
It's like how a theremin is basically just a monophonic piano that ramps smoothly between notes, floating free in space, scaled and rotated vertically 90-degrees, with one's entire hand being used as a single finger.
Luckily, you don't need to understand the exponent bias to understand floating point. All you need to know is that you have for instance 23 bits of mantissa (plus the implicit one), an exponent with range from -126 to +127 (plus the special values -127 and +128), and a sign bit. The exact details of how the exponent is encoded in the 8 bits reserved to it are only important when you're playing tricks with the floating point representation (other than "all zero bits is +0.0", which is often important when zeroing a struct with memset).
Indeed, for imperial units, this would have made a ton of sense? 1/16th of an inch would directly be .1 (1*16^-1), after all. Would be better if we used 16 symbols for counting, of course, such that we didn't mix decimal in that notation.
This, sadly, quickly devolves into debates about metric versus imperial. :(
If there was hardware support for 128-bit floating point, we could just use that: 113 bits of precision and 15 bits of exponent is enough for nearly any number you would ever want to store or process. We'd just need to agree on a sensible rounding scheme for turning those into array indexes.
Also I don't think it would matter if a language like Python would switch to BigDecimals as the default option. Python does such a ridiculous large amount of stuff unrelated to the actual math, that bit of lost performance won't break the camel's back.
In case anyone hasn't seen it yet, if you print a double with enough fractional values, you'll eventually get its "true" value (because 2 divides 10).
>>> print('%.60f' % 0.1)
0.100000000000000005551115123125782702118158340454101562500000
It's only zeros after these zeros.Here are the "true" values of 0.2, 0.3, and their sum:
>>> print('%.60f' % 0.2)
0.200000000000000011102230246251565404236316680908203125000000
>>> print('%.60f' % 0.3)
0.299999999999999988897769753748434595763683319091796875000000
>>> print('%.60f' % (0.1 + 0.2))
0.300000000000000044408920985006261616945266723632812500000000
Turns out 0.1 + 0.2 is actually the number right after "0.3": >>> print('%.60f' % math.nextafter(0.3, 100))
0.300000000000000044408920985006261616945266723632812500000000
If you don't use a ton of decimal digits, by default the runtime prints the shortest decimal number that has the same binary representation as the true value when converting that decimal number to binary. Much of the confusion around floats is due to that convenience feature that aims to make floats less confusing.One of the nice things is that it will be faster than the arbitrary precision libs and the sum of any two numbers will be representable.
But arbitrary precision libraries are incredibly slow. Compared to radix 2 floats.
Note that it won't ever be the default, as the standard imprecise but deterministic floats are an important feature for floats.
Even with a piece of paper of arbitrary length, the real numbers are uncomputable almost everywhere and even if they weren't you still need to keep things in the countable infinity sets to be semi-decidable.
We will see if decimal types, which require 4 bits for the radix become more common now that c/cpp support them but that may be optimistic.
If you round to the nearest, like, 10E-8 (maybe relative if your numbers being printed have massive range) it should be basically fine with doubles.
Unless you're displaying an unreasonable number of places to the right of the point, it's 3.
Nothing - *nothing* - needs more than about four or five significant digits.
BigDecimal actually doesn't fix anything. The underlying problem is that decimal representations are rarely finite--in base 10, 1 / 3 has to get rounded to 0.333, so the same problem comes up as 1 / 3 * 3 = 0.999 ≠ 1. If you want to fix that, you have to move to rational representations, but then you get flummoxed by things like sqrt(2) or atan(1/10) or pi.
No, the thing is that 0.1 + 0.2 is 0.3 in double arithmetic. The result differs from the closest representation to 0.3 only a tiny bit--like measuring the distance between LA and NY and being off by a few atoms. The problem is that == is checking for equality of all bits rather than a tolerance, and that string printing is using rules that it prints the shortest base-10 representation that rounds to that extra floating-point value, both of which exaggerate the importance of the last few bits. So the real mystery is why no one's designed a language that makes floating-point comparisons default to relative tolerance of 10^-10 or so.
Bc the very next step is a footgun: "the function said these two values are equal but they're not."
It's also strange that people use floats to represent 3D coordinates. Why should coordinates closer to the origin of the scene have more precision than those further away?
(Single precision) float-induced rounding at large offsets would introduce a lot of jitter to these calculations, which looks and feels very janky.
I've had success discussing floats with compute engineers by framing it like a really large fixed point number with all zeroes, except a small bit range. The location and value of the bit range are set by the exponent and mantissa, respectively. This makes precision issues and things like denormals straight forward to discuss.
For example 0.01 + 2.01 can already be written like ((100*0.01) + (100*2.01)) / 100 which seems trivial to implement in the language.
with something like 1000000000000000.0000001 + 1000000000000002.0000001 it seems to me that one could split the operation into 2?
One could even discard 2 numbers worth of accuracy and have a representation of 1/3 and 2/3. I'm not that attached to 4294967295 Having 1/3 would be more useful.
Why would we call an implementation correct if it produces the wrong result?
Floating point are used because quite simply they are the only way to do it that has wide support that can be reused.
A good fixed width or Decimal implementation that could be reused and implement all the maths you want for Reals are... Rare.
The exception is Java and JavaScript mostly. But JS has Bigint now. Just not as a default.
I’m not saying get rid of these distinctions entirely. But where they matter, for performance reasons or whatever, they should be opt-in rather than the default.
Many floats (such as 14.7) are not even represented exactly, float operations have a large number of issues.
If you're wondering about all this, see:
What Every Computer Scientist Should Know About Floating-Point Arithmetic
~ David Goldberg, March, 1991 issue of Computing Surveys
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.h...
(and elsewhere)
IMO, there is a huge qualitative difference between 15 and 14.7; 15 is an integer, while 14.7 isn’t.
I also think most people would know that instinctively. Reason? I think most people learn arithmetic progressing more or less along these lines:
- positive integers
- non-negative integers (i.e. learning and understanding the concept of ‘zero’)
- non-negative rationals
- all rationals
- decimal fractions
- all reals
Phrased another way: I would think the majority of people would learn to count to 15 long before learning what 14.7 means.Also, as sfpotter already said, switching to doubles only isn’t a panacea. Neither are bigints (won’t help representing the result of ⅓ exactly, for example), rationals (won’t help representing the typical hypothenuse of a triangle exactly, for example), or radicals (won’t help computing the area of a unit circle exactly)
A CAS would help, but by then, you’re giving up lots and lots of performance.
(that doesn’t mean I am a fan of languages that silently round integer division. Silently moving to bigints and rationals IMO can be a good choice, but silently moving from an exact type to an inexact one IMO never is)
Speak for yourself :)
Implementing a big decimal with feature-parity with floats is going to be a big pain.
Anyway, what about irrational numbers?
Python does big ints by default. Giving up at E-1 seems like a reasonably pragmatic but arbitrary choice.
This got me thinking, what are some things that DON’T change that are worth learning? HN I think is mostly focused on the latest things that change but there’s lot of value in focusing on things that don’t change.
Database normalization (1NF, 3NF, BCNF, etc) is worth knowing about when designing any kind of datastore. At least consider the tradeoffs of breaking BCNF. Almost all work on this was done in the 70s.
Linear algebra always turns up in unexpected places. Traditionally you would see it mostly in anything even vaguely related to computer graphics, but now it turns out to be quite useful for ML as well. Mostly stuff from the late 1800s, some a couple millennia older.
TCP/IP is mostly unchanged since the 70s, ARP is from the 80s, and HTTP1.0 is from the 90s. Combine those 4 and you have a good mental model of how the network stack works, even if HTTP3.0 is a bit more fancy.
Linux and Windows are quite different from each other, but the broad strokes of each hasn't changed much in the last two decades. For example knowing how they manage memory or how their respective thread schedulers work is potentially useful (though Linux thread schedulers do change from time to time. Windows seems to be happy with the architecture of theirs though).
Backus–Naur Form is an incredibly useful notation for describing context-free grammars. Stuff like valid syntax of a programming language, or how an address can look like. It's from the late 50s, though predecessors reach back much further. Extended Backus–Naur Form from the 90s is probably what you want to use today, it has an ISO standard and a great wikipedia page.
[0] https://jvns.ca/
Because it is clearly an ad? It explains what will you get and at what price. It does not explain "how Integers and Floats work". They want you to buy the zine, which is what they are advertising.
> Julia Evans has written a bunch of such zines in the past as well and they are quite informative
Good for them. Why do you think this makes it not an ad?
I don’t like arguing by analogy, but I don’t have a better way to make my point—-what if instead, old lady paid for a cpu that was clocked at 3.2ghz, and instead the cpu was hardware throttled down to 2.8ghz.
I expect when I purchase an item, all the features I paid for operate correctly—-even if I never expect to use all of them or use them to their full potential.
Julia has become a really, really serious pro over the years, but never lost the passion for blogging the journey. In terms of mostly self-taught hackers, an iconic legends like jwz or something at this point.
Keep it up another decade!
https://ciechanow.ski/exposing-floating-point/
and his companion website