How might one create a sine/cosine table for a circle with less than 360 degrees?

752 Views Asked by At

The number of degrees in a circle is completely arbitrary. 360 just so happens to work really well because it has tons of factors, and we've been using it for the longest time. We just stuck with it.

However, I'm in a situation where I need to quickly get sine and cosine values for a circle that has less than 360 degrees. I need to know the most efficient way to create a sine/cosine table for a circle with 256 degrees.

I'm honestly surprised there isn't some kind of program that allows you to enter the max degrees in a circle, and produce the appropriate table. I found nothing like that in the little bit of research I did.

Technically, you could take a normal value based on a 360 degree circle and divide that that by 1.40625, however that obviously wouldn't be very accurate or efficient. Especially considering you would still technically have 360 values. I need a table that has 256 values, not 360. That's the whole point.

So yeah, my question is basically in the title. How would it be done? How would I create a sine/cosine table for a circle with less than 360 degrees?

4

There are 4 best solutions below

3
On BEST ANSWER

So I'm assuming that your question involves having a "new" unit for measuring angles, in a similar way that there are $360$ degrees in a circle, but only $2 \pi$ radians. Here's a general way of going about this (adapted to the $256$ in a circle), but can be applied to any positive real number of "whatevers":

First, we want to find a linear function $g:[0,256] \rightarrow [0, 360]$ so as to take the desired domain, $[0,256]$, and scale it to the usual domain, $[0, 360]$. To find this, notice that we'd want $g(0) = 0$ and $g(256) = 360$. We know that $g$ will be in the form $mx + b$. To find these, we do the usual method: $\displaystyle m = \frac{\Delta y}{\Delta x} = \frac{360}{256}$, and the $y$-intercept is simply $0$. Thus, $\displaystyle g(x) = \frac{360}{256}x$.

Next, take the sine function for degrees, $\sin:[0, 360] \rightarrow [-1,1]$ and compose it with $g$. Notice that the result, $\sin(g(x))$, has domain $[0,256]$ and range $[-1,1]$, as desired.

Why does this work? The linear function above is simply a general solution to a ratio problem, since we assume that there's a proportional relationship between our "new" unit for measuring angles, the plumbus, and the old unit for measuring angles, the degree. In particular, suppose we know there are $256$ plumbi per $360$ degrees, and we want to know how many degrees are in $\theta$ plumbi for some given $\theta$. Answering this merely entails solving for $x$ in the ratio $\displaystyle \frac{256}{360} = \frac{\theta}{x}$.

As an aside, this is the same approach one uses to convert between different temperature scales (though the linear function may not have a $y$-intercept of zero).


As far as actually coding this, if your available libraries are restricted by the parameters of your assignment so as to not have access to the usual trig functions, you can instead get a very accurate approximation of the values by evaluating a truncated Taylor expansion, e.g.:

$$\sin(x) \ \approx \ x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!}$$

where $x$ is a radian input (you'll have to do a similar adjustment to the above for plumbus input). If you're okay having, say, hundredths-digit accuracy, this will actually save computing time since the built-in trig functions are going to be doing the same method but giving you superfluous accuracy.

0
On

Multiply each of the standard angles ($30^o,\;45^o,60^o,$ etc.) by $\frac{32}{45}$ and assign the known values of the original angle to the new angle.

12
On

Is this for a programming project? You might just want to use a table for $0^o$ to $180^o$, then use the identity $sin(\theta) = -sin(\theta - 180^o)$ and $cos(\theta) = \cos(360^o - \theta)$.

In a C/Java like syntax:

float my_sin(int deg) {
    if (deg > 180) {
        return -sin_table[deg-180];
    }
    return sin_table[deg];
}

float my_cos(int deg) {
    if (deg > 180) {
        return -cos_table[360 - deg];
    }
    return cos_table[deg];
}

Edit:

The formula table you showed me in the comment

is $2^{15} \sin(\theta)$, where $\theta$ is a measurement in degrees from $0$ up to $360$. In python:

    >>> from math import pi, sin
    >>> for i in range(360):
    ...     print int((2**15) * sin(i * 2 * pi / 360))
    ...
    0
    571
    1143
    1714
    2285
    2855
    3425

This makes sense to me. The Saturn probably uses fixed point arithmetic with $2^{15}$ as the denominator.

You might want to port this to StackOverflow. I can help you more if you post an example of the sin table being used in the game. What I'm eager to see is stuff like sin[T] where T is either 32 or 0x20 which would correspond to 45 degrees.

Edit 2:

So the sin table contains a lookup table of approximately $$256 \sin(\frac{2 \pi i}{256})$$ An interesting point is where it switches over from positive to negative (line numbers added)

126 00 12
127 00 0C
128 00 06
129 00 00
130 FF FA
131 FF F4

I do not know why the sign change does not happen exactly at 128 (which corresponds to $\pi/2$. Also the file contains another quarter rotation to accommodate the usage of $\cos(\theta) = \sin(\theta + \pi/2)$.

CalcSine:
    andi.w  #$FF,d0
    add.w   d0,d0
    ;;; This is where you need the bigger table
    ;;; as we have #$80 == pi/2
    addi.w  #$80,d0
    move.w  Sine_Data(pc,d0.w),d1 ; cos
    subi.w  #$80,d0
    move.w  Sine_Data(pc,d0.w),d0 ; sin
rts

Beyond that there is nothing special here. Just dividing a complete rotation into 256 pieces. I don't fully understand the calcangle bit, but I suspect it takes an $x$ and $y$-coordinate and outputs the angle it forms with the $x$-axis. Like if you input the coordinates $(1,1)$ it spits out the hexadecimal equivalent of 45 degrees.

>>> from math import atan2, pi
>>> print atan2(1,1) * 180 / pi
45.0
0
On

Continuing from Kaj Hansen's answer, I think that you could use better approximations using Padé approximants (instead of Taylor series). For example $$\sin(x)\approx x\frac{ 1-\frac{53 }{396}x^2+\frac{551 }{166320}x^4} { 1+\frac{13 }{396}x^2+\frac{5 }{11088}x^4}$$ would give an error of less than $3.0\times 10^{-6}$ while Taylor expansion up to $O(x^9)$ would give $1.5\times 10^{-4}$.

Similarly $$\cos(x)=1-x^2 \frac{\frac{1}{2}-\frac{3 }{104}x^2+\frac{59 }{131040}x^4}{1+\frac{1}{39}x^2+\frac{17 }{65520}x^4 }$$ would give a maximum error of $2.9\times 10^{-7}$.