Author Topic: DT's floating point tutorial (split from a help thread)  (Read 6208 times)

0 Members and 1 Guest are viewing this topic.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
DT's floating point tutorial (split from a help thread)
« on: March 22, 2011, 01:18:24 pm »
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 1099, 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×1099 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 stored

Floating 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:

SignExpS0S1S2S3S4S5S6
$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×103), 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×103, 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×103).

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 Plus

Well, 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
« Last Edit: December 16, 2011, 12:28:46 am by Deep Thought »




Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Deep Thought's Floating Point Tutorial
« Reply #1 on: June 14, 2011, 09:54:42 am »
Didn't I request this one in an ASM topic? how did it end up here?
I'm not a nerd but I pretend:

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Deep Thought's Floating Point Tutorial
« Reply #2 on: June 14, 2011, 10:39:49 am »
Great tutorial, but you might want to mention floating-point registers.  ASM programmers are used to standard 1 or 2 byte registers.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Deep Thought's Floating Point Tutorial
« Reply #3 on: June 14, 2011, 10:58:53 am »
Quote from: aeTIos
Didn't I request this one in an ASM topic? how did it end up here?

DJ split this into a new topic (thanks!). I'd forgotten entirely about this one :D

Quote from: Hot_Dog
Great tutorial, but you might want to mention floating-point registers.  ASM programmers are used to standard 1 or 2 byte registers.

You mean OP1-OP6? As aeTIos said this was actually a response to one of his questions about how floating-point numbers are stored, so I didn't really explain anything other than he asked :) If I have the time I'll expand this to a real tutorial though.




Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Deep Thought's Floating Point Tutorial
« Reply #4 on: July 10, 2011, 10:03:04 pm »
Update-bump with some more info on how to use it, and after cleaning up the format. Now also hosted at Buoyancy (:P).
« Last Edit: December 16, 2011, 12:28:32 am by Deep Thought »