Skip to content

Commit 5db702a

Browse files
authored
docs: update examples
Signed-off-by: Gunj Joshi <[email protected]>
1 parent da7c52f commit 5db702a

File tree

1 file changed

+6
-32
lines changed
  • lib/node_modules/@stdlib/math/base/special/ldexp/examples/c

1 file changed

+6
-32
lines changed

lib/node_modules/@stdlib/math/base/special/ldexp/examples/c/example.c

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,18 @@
1818

1919

2020
#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>
2421
#include <stdint.h>
2522
#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-
}
3223

3324
int main( void ) {
34-
double sign;
35-
double frac;
36-
int32_t exp;
37-
double x;
38-
double v;
25+
double y;
3926
int i;
4027

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 };
5830

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 );
6034
}
6135
}

0 commit comments

Comments
 (0)