Post Reply 
Approximation of Sine function in Integer Forth
03-12-2022, 04:25 PM (This post was last modified: 03-13-2022 10:51 AM by Martin Hepperle.)
Post: #1
Approximation of Sine function in Integer Forth
Recently I wanted to demonstrate a MS-DOS driver for plotting HPGL on a VGA screen.

To show how this driver could be used from any programming language I selected Forth as one example. The classical Forth implementations, like figForth did not support floating point calculations. Thus, everything has to be done with Integers, in my case in the range +/-32767.

HPGL works nicely with integers as it defines its device units as 0.25 mm per unit.

In order to generate some interesting plots with circles and spirals, I wanted to have an approximation of the trigonometric sine and cosine functions.
As I had to stick with integers, I decided to scale the sine by a factor of 1000.
So the approximation produces values in [-1000...+1000].
The input should be the angle in degrees, so that the angular resolution would be 1 degree.

The restriction to signed 16-bit integers generally limits polynomial terms to powers of two and all integer divisions truncate. The often used power series around zero needs at least powers of 3 to produce reasonable accuracy over the range [0 to 90] degrees.

First I tried a simple parabolic curve covering the range [0 to 180] degrees, which worked, but circles looked more like well rounded rectangles.
In a second attempt I generated series expansions at the center point of four intervals over the range [0 to 90] degrees. While this requires tests for the interval, it is still reasonable fast.
Some manual tweaking of the equations to the restriction of integer arithmetic produced a quite acceptable approximation.

The attached PDF document shows my current set of equations and their deviation from the sine function as well as their implementation in Forth and RPL.

Note: I am not looking just for a circle generator (there are a few integer algorithms for digital raster devices), but for the trigonometric functions for general applications.

Maybe there is another more elegant or more accurate approach?

Martin

[edit: programs adapted to match equations below graph]
[edit: added Garth's solution to the graph and page 2]


Attached File(s)
.pdf  Sine-Approximation-Integer.pdf (Size: 1,005.32 KB / Downloads: 53)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Approximation of Sine function in Integer Forth - Martin Hepperle - 03-12-2022 04:25 PM



User(s) browsing this thread: 1 Guest(s)