Oh, and I have a free explanation too: https://wordsandbuttons.online/sympy_makes_math_fun_again.ht...
That was a really fun read. Thanks for sharing!
CPUs do have a 'hardware' (I guess microcode) implementation in the 80387 era FPU. But it isn't used. One reason is that the software implementation is faster in modern floating point code. I don't know about GPUs but I would guess compilers transparently take care of that.
Re:Cosine Implementation in C
by sacnoradhq on Friday March 31, 2023 @03:35AM
(#35383870)
It depends on the precision and accuracy and data types of the arguments and result. The 80387 was absolutely faster than software emulation for F32/64/80 by hooking real-mode interrupt handlers the way it was back to the 8086/8087. For I8/16, lookup tables were faster. For I32, a quality result would be very expensive. There were no GPUs in the 387 era, and there wasn't SIMD (MMX) either. In the x86 era, compilers weren't that smart. If they were, then they were slow.
Re:Cosine Implementation in C
by adgjlsfhk1 on Friday March 31, 2023 @08:40AM
(#35386211)
the real problem is Intel screwed up their sin implementation by only using 66 bits of pi and then refused to fix it.
The realization that a 6th order approximation is good enough given our definition of float is beautiful.
It is technically 12th order, but every other coefficient is zero, so it calculates a polynomial approximation in x^2 space
Re:Cosine Implementation in C
by commandlinefan on Friday March 31, 2023 @05:58PM
(#35394216)
You can approximate 8 to 8 significant figures using 987654321/123456789 = 8.0000000729
Interestingly, this pattern ([all digits reversed] / [all digits] ~= [base] - 2) holds in base 16 as well: 0xfedcba987654321 / 0x123456789abcdef = 1147797409030816500 / 81985529216486900 ~= 13.99999999999999878027
This pattern holds in many bases:
1.0 1 1 1.4 12 21 2.111111111111111 123 321 3.020618556701031 1234 4321 4.002680965147453 12345 54321 5.000262295081967 123456 654321 6.000020444462617 1234567 7654321 7.000001321561743 12345678 87654321 8.0000000729 123456789 987654321 9.000000003504939 123456789a a987654321 10.00000000014928 123456789ab ba987654321 11.000000000005706 123456789abc cba987654321 12.000000000000197 123456789abcd dcba987654321 13.000000000000007 123456789abcde edcba987654321 14.0 123456789abcdef fedcba987654321
I have a similar example in my book Geometry for Programmers (https://www.manning.com/books/geometry-for-programmers). There I make a polynomial approximation for sine that is: 1) odd (meaning antisymmetric); 2) precise at 0 and Pi/2 (and -Pi/2 automatically); 3) have precise derivatives and 0 and Pi/2 (and -Pi/2); 4) have better overall precision than the equivalent approximation with Taylor series; and it only consists of 4 members! Adding more members makes it impractical in the modern world since we have hardware implementations for sine on CPUs and GPUs.
Spoiler alert, the secret sauce is the integral equation ;-)