# Floating Point Math System on 08M2 chip

#### Brewer

##### New Member
Hello,
I'm new to PICAXE after spending time with the BASIC Stamp and then Arduino hardware. I was looking for a micro controller that was smaller and used less power than the Arduino boards so I though I would take a look at PICAXE. I wanted to use floating point, fp, math in a power meter project but quickly found that PICAXE BASIC doesn't do fp. I took the challenge and developed a system for doing fp math on a 08M2 chip. If you need to calculate with numbers from -32768e-128 to 32767e127 and don't need speed or a lot of program memory, then this system might be useful to someone.

The system does fp addition, subtraction, multiplication, division, square root, and factorial. More functions could be added but I had already used more than half of the chip's program memory and general purpose variables. I suspect that chip resource usage could be improved, but it's good enough for now.

Floating point numbers are expressed in scientific notation as m x 10^p. The power of 10, p, is stored in a byte variable, b, associated with an integer word, w, representing m. Therefore, a fp variable uses 3 bytes. The overflow and underflow of b around 0 is used to denote positive and negative powers. If p is positive then b = 0+p, but if b is negative then b = 0 - p = 256 - p. P is restricted to the range of -128 <= p < 128. If p >=0 then 0 <= b <= 127. If p<0 then 128 <= b <= 255. This means that if bit 7 of b is 1 then b is negative, if 0 then b is positive. Negative values of m are handled the same way, except since w is a word, bit 15 of w is set to 1 to denote a negative number and b is adjusted to restrict positive values of m from 0 to 2^15 -1 = 32767. If m >=0 then 0 <= w <= 32767. If m < 0 then 32768 <= w <= 65535 This means that w has only 4 or 5 significant digits, but this is sufficient for most engineering calculations.

The attached code listing describes how to use the system with numerous comments. The code includes a demonstration of the system to calculate the unknown leg of a large right triangle given the hypotenuse and the other leg.

#### Attachments

• 15.6 KB Views: 139

#### oracacle

##### Senior Member
the inclusion of parenthesis would be helpful. I have done a fair amount for DoF and field of view cautions using the uM FPU, they calculations tend to have nested parenthesis and also logs like log 10 and log e
heres the calc for symmetrical DoF as example, granted it could be broken down into its constituent parts
2*F*c*(((Si-f)/f)+1) / (((Si-f)/f)^2)
heres Ev calculation
log2*f^2/s
which also get rearranged for HDR calculations
s=f^2/2^Ev

where ^2 is square (written in excel ^ is raise to power of)

#### Brewer

##### New Member
You must have misunderstood. I'm not using a FPU. The Floating Point calculations are all done in PICAXE BASIC on a 08M2 chip. What do you mean by the "the inclusion of parenthesis would be helpful."?

#### BESQUEUT

##### Senior Member
I did a very similar job...
I will have a look to your library, and maybe a convergence is possible...

#### nasi

##### New Member
FP Math on Picaxe

Hello Brewer

I just wanted to say that this is fantastic.
I have been dancing around using Picaxe, Maximite, Arduino and various other custom options.
I recently got in touch with uMega to consider the uM-FPU however they replied and said they had ceased production.
I am fairly expereinced at programming however I am slow developer and it takes me time to fully grasp new concepts.
What you have done here has really solidified my intent to keep using picaxe as they are cheap low powered and come in different variants.
No to say I wont keep using maximite and barely arduino I am just glad something like Floating Point options I can look at more now on Picaxe.
I have plenty of picaxe chips which I could i2c or SPI as a Co-Processor and this has excited me.

Thank you Brewer

Hope all is well.

Nasi