Using Integers Instead of Reals
When developing a sketch to read the built-in accelerometer of the FranchPad MSP430FR5739, I decided to use integer numbers only.
They are many advantages for using integers over reals:
Now, using integers instead of real isn't required on the LaunchPad Stellaris, as the LM4F120H5QR includes a floating point unit.
The arcsin Example
The trick consists on approximating the real function by linear functions on segments, and defining as few pivotal points as required.
Let's tart by simplifying! The arcsin function on the [-1, +1] range can be halved as arcsin( -Φ ) = - arcsin(Φ).
Then, pivotal points —in orange— are selected in order to minimise the error between the approximation —in blue— and the real function —in green.
There are as many segments on the [0, 0.707] range as on the [0.707, 1] range, due to the steepy curve close to 1.
Finally, the map() function makes writing the linear functions very easy.
y = map(x, minX, maxX, minY, maxY)
map() works in integer as long as all the parameters are integers.
Here, multiplier for the input value is 1000 times to make comparisons easier and 100 times for the output.
Managing Decimals with Integers
The % or modulo and / or integer division functions are very useful for printing decimal values.
Let's take π ≈ 3,14159265 and avoid real numbers. π becomes 314 and printX100(314) displays 3.14. How does it work?