lainchan / λ
Computer representation of number systems
Void · 2022-07-28 04:28
Let's see if I can get my point across.
I've been thinking about implementations of number systems. IEEE 754 is the standard implementation of non-integral numbers, but they have a lot of problems. Particularly, results are never quite right and errors propagate as one manipulates them more and more over the course of a computation. On a deeper level, they provide a representation for "real" numbers, which is impossible in finite memory for all but a tiny subset, and they do so by means of their symbolic representation as decimals. It's fuarrrking crazy.
Are there alternatives? Most lisp languages already implement rationals as their own number type, so, say, 1/3 is already represented as just that, not some rude appoximation like 0.333...4. It is likely stored as a pair of integers (bignum or fixnum, probably the most optimal for the case, which more often than not would be fixnum, which is already orders of magnitude more precise than floating point).
Further number systems can get a bit (pun not intended) tricky. Take sqrt(2), or more generally a^b where both a and b can be rationals. The "laws of exponents" provide for a straightforward manipulation of numbers without incurring in crude approximation, since we are dealing with fourdered-sets of integers. And you can be sure that sqrt(2)^2 = 2. In heavily numerical computations this would be a lot better than getting errors accumulate with every operation.
What's more, you get i (ie. sqrt(-1)) for free, and thus complex numbers. A simple algebra over complex pairs can generalize the whole of C and it's subsets.
But then the question begs: what about complex exponentiation, or even irrational exponents? What if we want sqrt(2) to be the exponent, or we want i^i. What if we want e^i, or perhaps π? No doubt such numbers are heavily used.
And so we come to the point of my thread: how would can this be handled? My idea is to make special cases of π and e and use them as symbolic primitives, and finally compute them as needed for a final result. The result might be a position in a graphical display, or printed in decimals. The degree of precision needed can be set as a parameter.
Why something like this hasn't been done is beyond me. Perhaps there are efficiency penalties in such an approach? Or maybe floating point hasn't really cause problems so far, only my mathematical brain can't stand them. Or maybe my approach is being used and I'm not aware of it yet.
Do you know of any other ways to represent number systems, and solutions to the problems I mentioned, or others? How would you approach the issue of number systems representation? Especially for trascendental numbers (of which we only ever use e and π afaik.)
Incidentally, in Hacker's Delight you can read about a cute system for storing complex numbers, using base -1+i, which can be encoded as binary strings. It's on section 12-2 if you want to check it out.
Wirth · 2022-07-28 12:10
> No doubt such numbers are heavily used.
There is no point in using irrational numbers. All graphics could be made with integers only and there is also such thing as rational trigonometry
Nameless · 2022-07-28 18:34
>>32062
>rational trigonometry
Some of Wildberger's ideas are actually the inspiration for my attempt to use number systems without appealing to a preconceived notion of real numbers. But I wouldn't throw out the baby with the bathwater. The idea of angle is incredibly useful, and so are it's sine and cosine. For example, how would you draw a circle on a screen without these concepts? sin and cos are very simple and straightforward ways of manipulating angles and rendering them to a screen. That's why I suggest the use of π as a symbolic primitive, in my own work I actually use τ most of the time, and I use simple algebra on numbers of the form (a/b)τ, where τ/n is one nth of a full turn. I also use a different ι-notation for turns, but I'm sure nobody cares.
As for e, it's difficult to overstate it's usefulness in science. We just handle the way a mathematician does, taking it as an object in it's own right, and working with the numbers surrounding it (say, as a coefficient or exponent). We are also used to manipulating "non-real" quantities that square to reals in the form of quaternions and hyperbolic (or split-complex) numbers.
Using rational exponentiation creates all the algebraic (ie non-trascendental) numbers, both rational and irrational, and it just takes a finite amount of data. Certainly better than the infinite chain of 3s in the decimal expansion of 1/3 which ieee754 tries to emulate.
The key is that the "black-box" computation of these trascendental constants and functions can be deferred to the very end of a computation when they are needed for a specific result, and their needed precision could be set by a compiler directive of sorts. Even then, their result would be a rational approximation (it always is), and if the underlying library (say, OpenGL) requires floating point, then it's converted and passed in that format, at which point the issues with IEEE 754 basically lose importance.
Lunch · 2022-07-28 19:06
Found a paper that's kind of related: "What Every Computer Scientist Should Know About Floating-Point Arithmetic"
>https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Trinity · 2022-07-28 22:50
>>32060
>Why something like this hasn't been done is beyond me.
Floats can be implemented in registers. Because symbolic arithmetic is super slow, even more so with bignum. Yes, you could rely on hardware acceleration for small numbers but that's not much different from what current bignum libraries do.
The Phantom Phreak · 2022-07-28 23:40
I like this idea, but it probably boulder be useful in actual high-performance numerical computations such as fluid simulations. The reason for this is that the numerical error from double floating points is typically much lower than the numerical error from the space/time approximation.
However, there is another area where your idea would be useful: computer algebra systems. You might want to look into the fields of computational group theory and computational algebra. They are relevant to a pretty narrow field of applied research, although with quantum computing becoming big they might become much more important (for e.g. optimizing compilers of quantum programs). Perhaps you could contribute to the GAP and MAGMA programs, I'm sure they would appreciate it.
Ender · 2022-07-29 01:45
More stuff:
Finite algebraic field extensions of Q are isomorphic to the field Q[x] mod p(x), where p is a certain polynomial. If you have two elements of different extensions then all algebraic combinations of them live in the product of their fields, which is again algebraic and (if i recall correctly) can be represented by Q[x]/ gcd(p(x),q(x)). If you want a very general representation that is fast for all complex arithmetic (I.e. doesn't have the same limitations on addition as the modulus-argument notation) this is probably the direction you want to go, study up on field theory. There are also generalizations for arithmetic extensions of Z. Z<x,y,...> Is the "standard" countably generated algebra so this all can become very general for arbitrary algebras, if you write your polynomial arithmetic to be fast everything will be fast though perhaps not memory efficient.
Spooky · 2022-07-29 03:06
Transcendental extensions are harier because they don't have a nice formulation in terms of polynomials, but that shouldn't be too hard to fix, just use Q[x,pi,e].
As to speed: on processors with SIMD it would probably be very fast, though not very memory efficient. I may try to write up an x86 example for modular polynomial addition & , it would be good practice.
Sorry for brain dumping OP I'm procrastinating studies and getting interested in your project at the same time.
Neo · 2022-07-29 16:43
>>32060
There are several other representations of real numbers and it isn't even hard to find that on wikipedia. Posit is one of the most popular alternatives now. There are even hardware implementations already.
Also several other number systems have at least papers published proposing some implementations. That includes Conway's surreal numbers for example.
Why some of these systems never been implemented? Because need for them ATM is so niche that it's cheaper to simulate them using existing hardware even if it would be slower than native hardware solutions. If there would come some real demand for hardware implementing them big enough to make production of hardware using these number systems as native systems - they would be produced one day.
push · 2022-07-30 03:08
>>32081
>>32082
I never really considered field extensions much. But I've considered maybe I would need polynomials to really get all the algebraic numbers, for example the golden ratio φ.
Indeed, I think I would NEED to have a polynomial type if I am to get the full generality of the algebraic numbers, and then I'd need to stack datatypes, getting polynomial exponents, and "rational polynomials", and so on ad infinitum. Also the rational datatype is nonessential, I can just make it through negative exponents, and that would remove the redundancy of having negative exponents on top of an already existing rational number type.
Further: what's stopping me from having infinitely long polynomials? Of course, memory. But we write infinite sums all the time and we achieve that with just a Σ term. Computer can represent that in yet another number type. But wasn't I complaining about floating point trying to represent infinitely long sequences? Well in this case the infinitely long sequence is compacted with no actual loss of information. It could if needed be unrolled to any degree of accuracy. And we could represent any useful trascendental number and function this way
>>32076
CAS really does eeem to be the exact domain for my considerations. But in reality I would like to make more of a library implementation of algebraically consistent numbers for any sort of computation. So it has to be conceptually simple, and efficiency is also a very important concern. I'm really not sure how efficient it can be made, especially with all this juggling with polynomials and infinite sums. But some degree of optimization can probably be achieved.
Leto Atreides I · 2022-07-30 09:54
>>32060
>Why something like this hasn't been done is beyond me.
>Why I don't know something like this has been done is beyond me.
>What are analog computers?
>What is error analysis?
When performing numerical analysis involving discretization, error reduction is 'built in' to the way you code such that error propagation isn't an issue for the calculations you're doing.
In addition there are many coding tricks. For example you can apply a factor k to avoid rounding errors. So if your system handles arbitrary large numbers but has limited accuracy down to 0.001, multiplying your input values by factor k=1x10^3 would increase the error resolution to 0.000001. This is a very simplified example, but you get the idea.
Wintermute · 2022-07-30 13:42
>>32100
Posit works in kinda similar way >>32084
There is hardware usedin Poland for example by Polish meteorology service that runs calculations using that standard implemented in hardware
Gyula Kovacs · 2022-07-30 15:29
>>32103
Producer of that hardware https://vivid-sparks.com/