News:

Let's find out together what makes a PIC Tick!

Main Menu

Floats and integers

Started by John Drew, May 03, 2021, 09:10 AM

Previous topic - Next topic

John Drew

Floating point numbers have been with Proton/Positron for many years and one of the reasons why the compiler is so powerful. However floats are not always the most accurate or fastest way of doing calculations.

Some years back I wrote an article describing how integers may replace floats for some calculations. Tim Box helped me with this so that the attached zip is a combined effort by us both. The article, previously published on the old site, will provide you with the essentials of what is happening under the hood of the compiler and perhaps help you make your programs faster or more accurate or maybe even both. The invisible floating point library takes up space despite the apparent simplicity in BASIC, so don't be tricked. All that being said, there are many occasions where floats can provide the best solution. As some say, it's "Horses for courses"
John

top204

#1
An excellent article John.

Many thanks for your support of the compilers over the years. It is always a good feeling to see one's creations being used for practical purposes, and people appreciating what has been created.

Floating Point mathamatics is something that 99% of programmers do not fully understand, and do not understand that it is simply a bit shifting mechanism to compress values. Over the years, the lack of understanding has actually caused aircraft to crash and space craft to miss a planet or actually crash into a planet because of Floating Point values that do not quite fit into the compression, then get rolled over to other calculations that aren't quite right, so the final result is not what it should have been, so the planet is not quite where it should be, or the planet's surface is a bit closer than the calculation said it was. :-)

normnet

Is their a way to do integer math for Sin and Cos to include the decimal places?

John Drew

#3
Thanks for your comments Les.
@Norm, there are some approximate ways.
Here's a simple way to calculate the sine very approximately using integers.

Sine X = 4 X (180 -X) / (40500-X(180-X))

X is an integer.

All the Xs are the value not a multiply symbol, I'm sure you worked that out.

The final division of the top line  will need to be done with a float because it will be less than one.

This method is far less accurate than using the inbuilt sine function in Proton.

As an experiment I used 10 degrees and it came up with 0.1752 whereas it's closer to 0.1736 in real life.

All the same it's an interesting experiment. To do it properly it has to be done with high accuracy floats.

Substituting 10 deg for X the calculation looks like this:
40 times 170 = 6800
40500 - 1700 = 38800
6800÷38800=0.175257

I'm doing this on my phone watching tellie with my wife. I look up every now and then to ask "What did he say". You can guess the answer I get :)
John