Live data from Hacker News

Implementing Cosine in C from Scratch (2020)

austinhenley.com

71–80 of 139 posts

Re: Implementing Cosine in C from Scratch (2020)

#71
post #67

Earlier quoted context omitted.

You can also use the Chebyshev polynomials to economise a higher degree taylor series with bit of a loss of accuracy.

you can't because boycott Chebyshev

You can't boycott Chebyshev, he died 1894 or so.

Re: Implementing Cosine in C from Scratch (2020)

#72

Earlier quoted context omitted.

But isn't this code rarely called in practice? I guess on intel architectures the compiler just calls the fsin instruction of the cpu.

> I guess on intel architectures the compiler just calls the fsin instruction of the cpu. Do people do that in practice? It's on the FPU, which is basically legacy emulated these days, and it's inaccurate.

> the FPU, which is basically legacy

you'll pry my long doubles from my cold, dead hands!

Re: Implementing Cosine in C from Scratch (2020)

#73

Earlier quoted context omitted.

But isn't this code rarely called in practice? I guess on intel architectures the compiler just calls the fsin instruction of the cpu.

No. The fsin instruction is inaccurate enough to be useless. It gives 0 correct digits when the output is close to 0.

> 0 correct digits when the output is close to 0

this is an amusing way to describe the precision of sub-normal floating point numbers

Re: Implementing Cosine in C from Scratch (2020)

#74

Earlier quoted context omitted.

> I guess on intel architectures the compiler just calls the fsin instruction of the cpu. Do people do that in practice? It's on the FPU, which is basically legacy emulated these days, and it's inaccurate.

> the FPU, which is basically legacy you'll pry my long doubles from my cold, dead hands!

The question is if you wouldn't be better served with double-doubles today. You get ~100 bits of mantissa AND you can still vectorize your computations.

Re: Implementing Cosine in C from Scratch (2020)

#75
post #74

Earlier quoted context omitted.

> the FPU, which is basically legacy you'll pry my long doubles from my cold, dead hands!

The question is if you wouldn't be better served with double-doubles today. You get ~100 bits of mantissa AND you can still vectorize your computations.

Sure. There should be a gcc flag to make "long double" become quadruple precision.

The thing is, my first programming language was x86 assembler and the fpu was the funniest part. Spent weeks as a teenager writing almost pure 8087 code. I have a lot of emotional investment in that tiny rolling stack of extended precision floats.

Re: Implementing Cosine in C from Scratch (2020)

#76

Earlier quoted context omitted.

No. The fsin instruction is inaccurate enough to be useless. It gives 0 correct digits when the output is close to 0.

> 0 correct digits when the output is close to 0 this is an amusing way to describe the precision of sub-normal floating point numbers

It is much more amusing if you describe it in ulps; for some inputs the error can reach > 2^90 ulps, more than the mantissa size itself.

Re: Implementing Cosine in C from Scratch (2020)

#78
This is a fun article!

Alternative avenues that complement the approaches shown:

- Padé Approximants (https://en.wikipedia.org/wiki/Pad%C3%A9_approximant) can be better than long Taylor Series for this kind of thing.

- Bhaskara I's sin approximation (https://en.wikipedia.org/wiki/Bhaskara_I%27s_sine_approximat...) is easily adaptable to cosine, remarkably accurate for its simplicity and also fast to calculate.

Re: Implementing Cosine in C from Scratch (2020)

#79

Earlier quoted context omitted.

This has been the standard algorithm used by every libm for decades. Its not special to Musl.

But isn't this code rarely called in practice? I guess on intel architectures the compiler just calls the fsin instruction of the cpu.

x87 trig functions can be very inaccurate due to the Intel's original sloppy implementation and subsequent compatibility requirements [1].

[1] http://nighthacks.com/jag/blog/134/index.html

Re: Implementing Cosine in C from Scratch (2020)

#80
post #69

Earlier quoted context omitted.

But isn't this code rarely called in practice? I guess on intel architectures the compiler just calls the fsin instruction of the cpu.

FSIN only works on x87 registers which you will rarely use on AMD64 systems -- you really want to use at least scalar SSE2 today (since that is whence you receive your inputs as per typical AMD64 calling conventions anyway). Moving data from SSE registers to the FP stack just to calculate FSIN and then moving it back to SSE will probably kill your performance even if your FSIN implementation is good. If you're vector…

Moving between x87 and xmm registers is actually fairly cheap (it's through memory, so it's not free, but it's also not _that_ bad). FSIN itself is catastrophically slow.
Post reply on HN