Using Integer Math instead of Floating Point for Speed
It’s a well-known fact that integer calculations are much faster than using real types like float, double or decimal. We don’t usually think about it that much these days because of the maths co-processors built into modern CPU’s. Recently, thanks to the Raspberry Pi and like, I’ve remembered my first love of Electronics and have being playing with a few CPU that are much less sophisticated.
It reminded me of a little trick we used to use in the old days of the Z80 and 8048, and that was to use integer calculations instead of floats. Let me explain:
You probably know that Pi can be approximated by 22/7, which only gives the value correct to 2 decimal places.
But 355/113 is correct to 6 decimal places.
And 103993/33102 is correct to 9 decimal places.
Pi is actually mathematically irrational (i.e. it cannot be written exactly as the ratio of two integers).
Some well known mathematical constants as Rational Number approximations (‘fractions’)
|c (speed light)||24559/8192||
Floats may be more convenient, but integers are definitely faster. Have you got any useful ones to add?