Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://math.stackexchange.com/a/3494710
- //Compile with C++20
- #include <bit>
- #include <cmath>
- #include <cstdint>
- #include <cstdio>
- float test(float x) {
- auto negtwelfth_root = [](float a) {
- float r = std::bit_cast<float>( 0x44c3cf20 - (std::bit_cast<uint32_t>(a)>>16)*0x1555 );
- float r3=r*r*r; float r6=r3*r3; float r12=r6*r6;
- float h = 0x1.ff078cp-1f - r12*a;
- r = (0x1.61bf5ap-4f + 0x1.905614p-5f*h)*h*r + r;
- return r;
- };
- return std::sqrt(x) * negtwelfth_root(x);
- }
- double gt(float x) {
- return std::pow( (double)x, 1.0/2.4 );
- }
- int main() {
- uint32_t start = std::bit_cast<uint32_t>( std::nextafter(0.0031308f,9999.0f) );
- uint32_t stop = std::bit_cast<uint32_t>( 1.0f );
- double maxerr = 0.0;
- double maxrelerr = 0.0;
- for (uint32_t a=start;a<=stop;++a) {
- float x = std::bit_cast<float>(a);
- float res = test(x);
- double ref = gt(x);
- double err = std::abs( res-ref );
- double relerr = std::abs( (res-ref) / ref );
- if (err >maxerr ) maxerr =err ;
- if (relerr>maxrelerr) maxrelerr=relerr;
- }
- printf("maxerr = %11.4e\n",maxerr );
- printf("maxrelerr = %11.4e\n",maxrelerr);
- getchar();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement