3 ms·
Having the full number stack supported (natural numbers - integers - rationals - reals) would be, indeed, awesome. Sadly, most people don't understand the dist
by otabdeveloper4 5y ago
Having the full number stack supported (natural numbers - integers - rationals - reals) would be, indeed, awesome.
Sadly, most people don't understand the distinctions, so this will never happen. (Even in reply to your post people keep talking about "decimals", as if the number base is at all relevant here.)
- JoshuaDavid 5y ago"Decimal" usually refers to a data type which is "integer, shifted by a known number of decimal places". So, for example, if you had an amount of money $123.45, you could represent that as a 32-bit floating point number with (sign=0 (positive), exponent=133, mantissa=7792230) which is 123.4499969482421875, but you would probably be better off representing it with a decimal type which represents the number as (integer part=12345, shift=2), or as just a straight integer number of cents. The number base is relevant, because money is discrete, and measured in units of exactly 1/10^n, and if you try to use floating point numbers to represent that you will cause your future self a world of pain.
- Tainnor 5y agoNon-decimal currencies have existed in the past and there are still some remnants: https://en.wikipedia.org/wiki/Non-decimal_currency https://en.wikipedia.org/wiki/Non-decimal_currency
- JoshuaDavid 5y agoYes, that's a good point. If you try to use decimal for those currencies, you are in for a very similar world of pain as you are by using floats for decimal currencies.
- Tainnor 5y ago> reals This is technically impossible. Almost all of the real numbers can't be represented in a computer. The most you can get is the computable reals. But to be able to represent some of those and to do arithmetic on them, you have to use somewhat complicated representations such as Cauchy sequences. The use cases for computing with exact representations of (computable) irrational numbers are fairly limited (probably mostly restricted to symbolic algebra). In 99% of cases, if you need the square root of 2 that's probably because this comes from some sort of measurement (e.g. you need the diagonal of a square with sides 1), and if it's a measurement, there's going to be an error associated with it anyway and there's no point in insisting that this number that you measured is "exactly" sqrt(2). This is different from rational (and, in particular, integer) numbers, in which case there are many valid use cases for representing them exactly, e.g. money.
- otabdeveloper4 5y ago> This is technically impossible. It's technically impossible for the integers too. > Almost all of the real numbers can't be represented in a computer. Almost all of the integers can't be represented in a computer. What, exactly, is your point?
- Tainnor 5y agoThese two things are not alike. For every integer, there exists a computer that can represent it. Even with constant memory, I can right now write a computer program that will eventually output every integer if it runs for long enough. By contrast, for almost all (i.e. an uncountable number of) real numbers there exists no computer whatsoever that can ever hope to represent any of them. Another way of seeing that is that, while Z is infinite, any single integer only requires finite amount of memory. But a real number may require an infinite amount of memory. The integers can also be represented fairly easily as a type. For the naturals, for example, it's as easy as data Nat = Z | S Nat (ML-type languages allow to do this very concisely, but you can do theoretically the same type of thing with e.g. Java and inheritance; if you use Scala or Kotlin, use a sealed class, if you use Swift, use an enum, etc.) The integers are slightly more complicated (if you just try to add a sign, you'll have to deal with the fact that you now have +0 and -0), but still not hard. Rationals are a bit harder in that now you really have multiple different representations which are equivalent, but you can also deal with that. By contrast, you won't be able to construct a type that encodes exactly the set of real numbers. The most you can do is to provide e.g. an interface (or typeclass) Real with some associated axioms and let any concrete type implement that interface.
- otabdeveloper4 5y agoYou're trying to explain the difference between countable and uncountable infinities here. The distinction is irrelevant in the context of computers. > By contrast, you won't be able to construct a type that encodes exactly the set of real numbers. You don't need to encode exactly, just like you don't need to encode the integers "exactly". All you need is a way to guarantee a finite number of significant digits in your real number approximation. Floating point numbers give you that, problem solved.