Live data from Hacker News

How do computers calculate sine?

androidcalculator.com

141–150 of 167 posts

Re: How do computers calculate sine?

#141
post #68

Earlier quoted context omitted.

Oh, wow, forgot about fp-contract. It says it is off in C by default, what about C++?

Read closer, it defaults to fast, not off

I would have expected to be a bug in the documentation? Why would they turn FMA off for standard compliant C mode, but not for standard compliant C++ mode?

But the documentation does appear to be correct: https://godbolt.org/z/3bvP136oc

Crazy.

Re: How do computers calculate sine?

#142
post #23

It's much clearer if you read one of the source code of the libm. Plan 9: https://9p.io/sources/plan9/sys/src/libc/port/sin.c Freebsd: https://cgit.freebsd.org/src/tree/lib/msun/src/k_sin.c

Ah, I always referred to the musl implmenetation, but I just now realized that they copied the Freebsd one.

Re: How do computers calculate sine?

#143
post #109

sine is easy because the series is globally convergent and fast converging

It would also be extremely inaccurate. The x^n numerators grow very quickly and digits get lost because unlimited precision isn't available. Likewise, the n! denominators also grow rapidly. Then the series is alternating which means cancellation is happening for every added term. If you don't believe me try for x=10.

[deleted]

Re: How do computers calculate sine?

#144

Earlier quoted context omitted.

I always wonder when hearing about these old optimizations why they aren't used in contemporary code. Wouldn't you want to squeeze every bit of performance even on modern hardware?

The "processor-memory performance gap" is a big reason why lookup tables aren't as clear of a win on modern hardware as they were on the SNES. If it takes two CPU cycles to read from RAM, a lookup table will basically always be faster than doing the math at runtime. If it takes fifty cycles (because, while your RAM may be faster, your CPU is a lot faster), and your processor has more advanced hardware that can do mor…

I think this is the only answer that addresses the issue. We always underestimate the cost of a read. and overestimate the cost of a compute.

Re: How do computers calculate sine?

#145
post #112

Earlier quoted context omitted.

Games targetting pre-Pentium PCs also used precomputed trig tables. Pentium was fast enough that it didn't matter as much. Just a few years later it was slower to read a trig precomputed table.

in other words, for those of us who remember, they used the equivalent of a slide rule

More like a Trigonometry table, which predates even slide rules:

https://en.wikipedia.org/wiki/Mathematical_table

Re: How do computers calculate sine?

#146
post #23

It's much clearer if you read one of the source code of the libm. Plan 9: https://9p.io/sources/plan9/sys/src/libc/port/sin.c Freebsd: https://cgit.freebsd.org/src/tree/lib/msun/src/k_sin.c

Does 9front keep the same sin.c implementation?

Re: How do computers calculate sine?

#147
post #10

This made me realize that trigonometric functions are not deterministic across different CPU architectures, OS, and programming languages (floating point precision aside). E.g. I would assume that Math.sin(x) returns the same thing in NodeJS on Windows and Mac/M1, but it turns out it is necessarily so. https://stackoverflow.com/questions/74074312/standard-math-f...

So you can use sin(x) for various x to tell what you are running on. Maybe even in the browser?

There is a shader which uses similar idea to detect what kind of GPU you have https://www.shadertoy.com/view/slySWV

Re: How do computers calculate sine?

#148
post #68

Earlier quoted context omitted.

Oh, wow, forgot about fp-contract. It says it is off in C by default, what about C++?

Read closer, it defaults to fast, not off

it defaults to off for standard-compliant mode. Which in my mind was the default mode as that's what we use everywhere I have worked in the last 15 years. But of course that's not the case.

In any case, according to the sibling comment, the default is 'fast' even in std-compliant mode in C++, which I find very surprising. I'm not very familiar with that corner of the standard, but it must be looser than the equivalent wording in the C standard.

Re: How do computers calculate sine?

#149
post #109

sine is easy because the series is globally convergent and fast converging

It would also be extremely inaccurate. The x^n numerators grow very quickly and digits get lost because unlimited precision isn't available. Likewise, the n! denominators also grow rapidly. Then the series is alternating which means cancellation is happening for every added term. If you don't believe me try for x=10.

You need around 26 terms for x=10 if you do it without reduction and you want an accuratish result for double precision.

You wouldn't evaluate the terms naively from left-to-right.

x - x^3/3! + x^5/5! - ... = x * (1 - x^2/(2*3) * (1 - x^2/(4*5) * ... ) )

I just checked in python and you get a result that is around 1000*machine epsilon off. Not great, not terrible.

Re: How do computers calculate sine?

#150

What’s the best way to calculate it by hand? I’m brushing up my math basics (I graduated CS while dodging the math requirements) and it frustrates me that in trig I need to remember values at all. The values such as sqrt(2)/2 make sense but how hard is it to calculate sin(5 degrees) by hand?

Five is small enough that you can get away with sin(5 deg) = 5pi/180
Post reply on HN