| Author | Topic: Math (Read 94 times) |
Eddy-B Administrator
     member is offline
Joined: Jun 2008 Gender: Male  Posts: 19 Location: Netherlands Karma: 0 |  | Math « Thread Started on Sept 17, 2008, 7:00pm » | |
I've picked up the project again, and started implementing more math capabilities.
Uptill now, the Console BASIC was able to do simple calculations using only the most basic operators (+ - * / %). One of my goals was to create a BASIC that allows for some more complex graphics than just plotting a few points and lines. To do circles i'd need Sine and Cosine functions, which brings me to the number system: the current Console BASIC has only 16bit integers to work with. In order to use fractions i will need either a floating point or a fixed point number system.
I soon figured that 16bit floating-point was rather useless, since it would have ridiculously little accuracy, and i don't want to use 32 bits because of the limited SRAM of the microcontroller.
So i'm left with 16bit fixed-point. Question is: where do i put this fixed point ? There are two favorable 16bit fixed-point systems: Q5.11 and Q8.8 The first one giving me 3 significant digits after the decimal point leaving 4 bits and a signbit for the integer part (-15.999 ~ 15.999). The second gives me only 2 significant digits, BUT i have a full byte for the integer part (-127.99 ~ 127.99). There is a third possibility: Q9.7 which gives me a range of -255.99 ~ 255.99 but the second digit behind the decimal point is questionable, and may have a 1-bit error after a calculation... I'd have to look into that. For now it seems like it will work fine.
| -- www.ElectronicsPit.com -- |
|
Eddy-B Administrator
     member is offline
Joined: Jun 2008 Gender: Male  Posts: 19 Location: Netherlands Karma: 0 |  | Math « Reply #1 on Sept 17, 2008, 8:11pm » | |
I've tested a CORDIC sine/cosine algorythm that i'm planning to implement in the Console BASIC, using both the Q8.8 and Q9.7 systems:
The Q8.8 system never has more than 1 bit error (due to rounding), where the Q9.7 has errors upto 2 bits. So i think i'm going with the Q8.8 system.
I need to implement these functions for sure.. maybe others SIN COS TAN SQR ( SQR(16384) and higher will result in overflow in Q8.8)
Here a test run of the SIN function with CORDIC on a Q8.8 fixed point system
| -- www.ElectronicsPit.com -- |
|
Eddy-B Administrator
     member is offline
Joined: Jun 2008 Gender: Male  Posts: 19 Location: Netherlands Karma: 0 |  | Re: Math « Reply #2 on Sept 20, 2008, 9:56pm » | |
I've added the SIN, COS and TAN functinos to my BASIC. I've also included two SQR variants: one that returns an integer, and one that returns a fixed-point number.
I also added conversion functions to and from fixed-point plus a ROUND function, that rounds a FP number to the nearest integer equivalent.
So far, everything works fine, be it that SIN(1o) and as a result also COS(89o) and TAN(89o) has a significant error due to the limited accuracy of the Q8.8 system.
| -- www.ElectronicsPit.com -- |
| |
|