Live data from Hacker News

Implementing Cosine in C from Scratch (2020)

austinhenley.com

1–10 of 139 posts

Re: Implementing Cosine in C from Scratch (2020)

#6
post #4

lookup tables and lerp are the most common solution I've seen (for a wide range of functions). You can also memorize with an LRU cache.

>> lookup tables and lerp are the most common solution I've seen

The author missed one nice table method. When you need sin() and cos() split the angle into a coarse and fine angle. Coarse might be 1/256 of a circle and fine would be 1/65536 of a circle (whatever that angle is). You look up the sin/cos of the coarse angle in a 256 entry table. Then you rotate that by the fine angle which uses another 256 sin * cos entries. The rotation between coarse values is more accurate than linear interpolation and give almost the same accuracy as a 65536 entry table using only 768 entries. You can use larger tables or even another rotation by finer angle steps to get more accuracy.

Re: Implementing Cosine in C from Scratch (2020)

#7
I learned how to write programs from Scratch[0]. Back in the pre-1.4 days (IIRC) there was no native sin/cos functions, so they had to be "implemented" somehow to translate arbitrary angles into delta (x,y). The way that I found people did this was as follows (for sin(A), cos(A)):

Invisible sprite:

[ when I receive "sine" ]:

  [ go to center ]

  [ turn A degrees ]

  [ move 1 step forward ]
Now, anyone that wants to know sin(A), cos(A) just has to read that sprite's X and Y position.

[0]: https://scratch.mit.edu

Re: Implementing Cosine in C from Scratch (2020)

#8
post #4

lookup tables and lerp are the most common solution I've seen (for a wide range of functions). You can also memorize with an LRU cache.

> You can also memorize with an LRU cache.

That's probably not going to perform well for an operation this fast. The computation is faster than main memory read.

Re: Implementing Cosine in C from Scratch (2020)

#9
post #6
post #4

lookup tables and lerp are the most common solution I've seen (for a wide range of functions). You can also memorize with an LRU cache.

>> lookup tables and lerp are the most common solution I've seen The author missed one nice table method. When you need sin() and cos() split the angle into a coarse and fine angle. Coarse might be 1/256 of a circle and fine would be 1/65536 of a circle (whatever that angle is). You look up the sin/cos of the coarse angle in a 256 entry table. Then you rotate that by the fine angle which uses another 256 sin * cos en…

> Then you rotate that by the fine angle

Explain?

Are you trying to reduce the error in the linear interpolation by describing the convex curve between the endpoints?

The derivative of the cosine is the sine, and the derivative of the sine is the cosine...so I'd expect the required output of the fine angle table to interpolate 0.15 between 0.1 and 0.2 and the required output to interpolate 45.15 between 45.1 and 45.2 degrees will be very different.

Post reply on HN