Live data from Hacker News

How do computers calculate sine?

androidcalculator.com

31–40 of 167 posts

Re: How do computers calculate sine?

#31
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...

[deleted]

Re: How do computers calculate sine?

#32
post #16

Earlier quoted context omitted.

Safer to assume that floats are never deterministic.

Floats follow a clear specification which determines precisely how basic arithmetic should work. They should work the same on all popular modern platforms. (Whether specific software libraries are the same is a separate question.)

But transcendentals like sine are not part of the strictly defined basic arithmetic; they are intentionally defined with relaxed behavior.

Re: How do computers calculate sine?

#33
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...

Well, what's fun is that (AFAIK) trigonometric functions tend not to be implemented in the newer floating point instructions, such as AVX or SSE. So while what you say is true about the x87 implementation of those functions, for anything targeting a machine built in the last 20 years it's likely the code will run consistently regardless the architecture (barring architecture floating point bugs, which aren't terribly…

They do, however, have some intrinsics for trig functions in AVX in their compilers. Not as good as having an instruction of course.

Re: How do computers calculate sine?

#34
post #29

Earlier quoted context omitted.

Somewhat annoyingly the ascribe standard only specifies that various math functions return an approximation but does not set any bounds on that approximation. So for many functions you could just return NaN and still be compliant.

Isn’t NaN the one value that can’t possibly count as an approximation, because it’s not a number and unordered? ;)

You might think so, but if it’s not specified in the standard…

Re: How do computers calculate sine?

#35
post #27

Earlier quoted context omitted.

Well, what's fun is that (AFAIK) trigonometric functions tend not to be implemented in the newer floating point instructions, such as AVX or SSE. So while what you say is true about the x87 implementation of those functions, for anything targeting a machine built in the last 20 years it's likely the code will run consistently regardless the architecture (barring architecture floating point bugs, which aren't terribly…

Sadly even SSE vs. AVX is enough to often give different results, as SSE doesn't have support for fused multiply-add instructions which allow calculation of a*b + c with guaranteed correct rounding. Even though this should allow CPUs from 2013 and later to all use FMA, gcc/clang don't enable AVX by default for the x86-64 targets. And even if they did, results are only guaranteed identical if implementations have chos…

GCC won't use FMA without fast-math though. Even when AVX is otherwise enabled.

Re: How do computers calculate sine?

#36
post #29

Earlier quoted context omitted.

Isn’t NaN the one value that can’t possibly count as an approximation, because it’s not a number and unordered? ;)

You might think so, but if it’s not specified in the standard…

It is the worst possible approximation though.

Re: How do computers calculate sine?

#37
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...

Well, what's fun is that (AFAIK) trigonometric functions tend not to be implemented in the newer floating point instructions, such as AVX or SSE. So while what you say is true about the x87 implementation of those functions, for anything targeting a machine built in the last 20 years it's likely the code will run consistently regardless the architecture (barring architecture floating point bugs, which aren't terribly…

> x87 is just a really weird and funky instruction set that's best left in the gutter of history

hmmm, can you use the long doubles in sse or avx? They are glorious, and as far as I see from playing with godbolt, they still require dirtying your hands with the x87 stack.

Re: How do computers calculate sine?

#38
post #27

Earlier quoted context omitted.

Sadly even SSE vs. AVX is enough to often give different results, as SSE doesn't have support for fused multiply-add instructions which allow calculation of a*b + c with guaranteed correct rounding. Even though this should allow CPUs from 2013 and later to all use FMA, gcc/clang don't enable AVX by default for the x86-64 targets. And even if they did, results are only guaranteed identical if implementations have chos…

GCC won't use FMA without fast-math though. Even when AVX is otherwise enabled.

Sure it will:

> -ffp-contract=fast enables floating-point expression contraction such as forming of fused multiply-add operations if the target has native support for them

> The default is -ffp-contract=off for C in a standards compliant mode (-std=c11 or similar), -ffp-contract=fast otherwise.

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#ind...

Re: How do computers calculate sine?

#39
post #32

Earlier quoted context omitted.

Floats follow a clear specification which determines precisely how basic arithmetic should work. They should work the same on all popular modern platforms. (Whether specific software libraries are the same is a separate question.)

But transcendentals like sine are not part of the strictly defined basic arithmetic; they are intentionally defined with relaxed behavior.

Even there many standard libraries provide very good precision at least within sane domain.

Re: How do computers calculate sine?

#40
post #29

Earlier quoted context omitted.

Isn’t NaN the one value that can’t possibly count as an approximation, because it’s not a number and unordered? ;)

You might think so, but if it’s not specified in the standard…

I was submitting an interpretation of the standard.
Post reply on HN