Why floating point?It's all about
precision. Sure, you might not need 14 digits to store a high score in your game (unless you're
Konami or some other big TCG maker that doesn't care about point inflation), but keep in mind what you're programming on. It's a calculator, and calculators are supposed to be accurate. Imagine if calculators could handle only integer numbers from 0 to 65,535! How fun would that be?
But that presents a problem: The Z80 calculator can only handle two-byte registers, or integers up to 65535. How would you use that for fractions and decimals? Sure, you could use
8.8 fixed-point notation, where the first byte is the integer part and the second is the fraction. But that still gives you only 16 bits of precision, limiting you to numbers between 0 and 255 (or −128 to 127 signed), with fractions in multiples of 1/256. The closest you could get to π would be 3.140625. So you turn to
multi-byte fixed point, where you store fixed-point numbers as long as you want in RAM. But that means you're limited in digits to how much memory you plan to use. For a number like 10
99, you'd need at least 42 bytes! (Nice number.)
That's where floating point comes in.
Floating-point numbers are stored in RAM, like fixed point, but in
scientific notation. In other words, the number could start at any decimal place you want, and extend as many digits you want. There's no need to extend the number to the decimal point, and you can keep each number the same size. This is clearly an advantage on a calculator that handles numbers up to 9.9999999999999×10
99 with a bit more than 24 KB of RAM to work with, and that's what this tutorial teaches you.
How floating-point numbers are storedFloating point numbers are represented differently from the usual registers you're used to. They're stored in RAM, as nine-byte data structures in scientific notation. For example, this is what −1337 looks like:
Sign | Exp | S0 | S1 | S2 | S3 | S4 | S5 | S6 |
$80 | $83 | $13 | $37 | $00 | $00 | $00 | $00 | $00 |
Sign/Number Line (Sign)Bit 7 (the bit furthest to the left when you write it out) tells you whether the number is positive or negative, while bits 2 and 3 tell you whether it's
real or complex. In other words, a valid sign byte is one of the following:
- %00000000 (positive and real)
- %10000000 (negative and real)
- %00001100 (positive and complex)
- %10001100 (negative and complex)
So in this case, the sign byte would be %10000000, or $80.
Exponent (Exp)This is where the fun starts. Floating points are always stored in scientific notation (as in −1.337×10
3), so you need to know what
power of 10 it's being taken to. In this case, that would be three.
But you don't just store a three here; no, that would be too easy, and remember that TI wants to
screw us up. So instead, you add $80 to whatever the exponent is, then store it. So for −1337, or −1.337×10
3, it would be
3 + $80, or $83.
Significand (S0-S6)This is the actual number itself. There are 7 bytes per number, each of which holds two digits (hence the 14 digits of accuracy on a TI-83 Plus series calc).
It's stored in
BCD (binary-coded decimal) format, in which each nibble holds a decimal digit (0-9). So a valid byte in the significand would be one of the following:
- $00-$09
- $10-$19
- $20-$29
- $30-$39
- $40-$49
- $50-$59
- $60-$69
- $70-$79
- $80-$89
- $90-$99
The first digit (upper nibble of
S0) is the
characteristic, or the number before the decimal point when written in scientific notation (the 1 in −1.337×10
3).
You could actually store a non-BCD value there (such as $BA). It causes some interesting effects when you try doing math with it. Try it for yourself to see (it shouldn't cause a crash, but I won't be held responsible if it does).
Floating-point numbers on the TI-83 PlusWell, it is a calculator, and that means a ton of floating points. TI's Equation Operating System (EOS™; yes, it is trademarked) uses floating-point numbers for all the reasons mentioned earlier and more, and it has some handy features for dealing with them.
MOAR COMING SOON