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
Implementing Cosine in C from Scratch (2020)
71–80 of 139 posts
Re: Implementing Cosine in C from Scratch (2020)
#72Earlier 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.
you'll pry my long doubles from my cold, dead hands!
Re: Implementing Cosine in C from Scratch (2020)
#73Earlier 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.
this is an amusing way to describe the precision of sub-normal floating point numbers
Re: Implementing Cosine in C from Scratch (2020)
#74Earlier 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!
Re: Implementing Cosine in C from Scratch (2020)
#75Earlier 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.
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)
#76Earlier 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
Re: Implementing Cosine in C from Scratch (2020)
#77Re: Implementing Cosine in C from Scratch (2020)
#78Alternative 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)
#79Earlier 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.
Re: Implementing Cosine in C from Scratch (2020)
#80Earlier 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…