5 ms·
I love these things! Approximation is art. I have a similar example in my book Geometry for Programmers (https://www.manning.com/books/geometry-for-programmers
by okaleniuk 4y ago
I love these things! Approximation is art.
I have a similar example in my book Geometry for Programmers (https://www.manning.com/books/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 ;-)
- okaleniuk 4y agoOh, and I have a free explanation too: https://wordsandbuttons.online/sympy_makes_math_fun_again.html https://wordsandbuttons.online/sympy_makes_math_fun_again.ht...
- JunkEmu 4y agoThat was a really fun read. Thanks for sharing!
- stkdump 4y agoCPUs 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.
- sacnoradhq 4y agoIt 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.
- adgjlsfhk1 4y agothe real problem is Intel screwed up their sin implementation by only using 66 bits of pi and then refused to fix it.
- boyka 4y agoThe realization that a 6th order approximation is good enough given our definition of float is beautiful.
- kolbe 4y agoIt is technically 12th order, but every other coefficient is zero, so it calculates a polynomial approximation in x^2 space
- commandlinefan 4y agoYou can approximate 8 to 8 significant figures using 987654321/123456789 = 8.0000000729
- piperswe 4y agoInterestingly, this pattern ([all digits reversed] / [all digits] ~= [base] - 2) holds in base 16 as well: 0xfedcba987654321 / 0x123456789abcdef = 1147797409030816500 / 81985529216486900 ~= 13.99999999999999878027
- starsep 4y agoThis 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 https://ideone.com/DvljON https://ideone.com/DvljON