|
18 | 18 |
|
19 | 19 |
|
20 | 20 | #include "stdlib/math/base/special/ldexp.h" |
21 | | -#include "stdlib/math/base/special/frexp.h" |
22 | | -#include "stdlib/math/base/special/pow.h" |
23 | | -#include <stdlib.h> |
24 | 21 | #include <stdint.h> |
25 | 22 | #include <stdio.h> |
26 | | -#include <inttypes.h> |
27 | | - |
28 | | -static double rand_double() { |
29 | | - int r = rand(); |
30 | | - return (double)r / ( (double)RAND_MAX + 1.0 ); |
31 | | -} |
32 | 23 |
|
33 | 24 | int main( void ) { |
34 | | - double sign; |
35 | | - double frac; |
36 | | - int32_t exp; |
37 | | - double x; |
38 | | - double v; |
| 25 | + double y; |
39 | 26 | int i; |
40 | 27 |
|
41 | | - for ( i = 0; i < 100; i++ ) { |
42 | | - if ( rand_double() < 0.5 ) { |
43 | | - sign = -1.0; |
44 | | - } else { |
45 | | - sign = 1.0; |
46 | | - } |
47 | | - |
48 | | - // Generate a random number: |
49 | | - frac = rand_double() * 10.0; |
50 | | - exp = (int32_t)( rand_double()*616.0 ) - 308; |
51 | | - x = sign * frac * stdlib_base_pow( 10.0, (double)exp ); |
52 | | - |
53 | | - // Break the number into a normalized fraction and an integer power of two: |
54 | | - stdlib_base_frexp( x, &frac, &exp ); |
55 | | - |
56 | | - // Reconstitute the original number: |
57 | | - v = stdlib_base_ldexp( frac, exp ); |
| 28 | + const double frac[] = { 0.5, 5.0, 0.0, 3.5, 7.9 }; |
| 29 | + const int32_t exp[] = { 3, -2, 20, 39, 14 }; |
58 | 30 |
|
59 | | - printf( "%e = %lf * 2^%" PRId32 " = %e\n", x, frac, exp, v ); |
| 31 | + for ( i = 0; i < 5; i++ ) { |
| 32 | + y = stdlib_base_ldexp( frac[ i ], exp[ i ] ); |
| 33 | + printf( "ldexp(%lf, %d) = %lf\n", frac[ i ], exp[ i ], y ); |
60 | 34 | } |
61 | 35 | } |
0 commit comments