xref: /plan9/sys/src/ape/lib/ap/stdio/pow10.c (revision 3e12c5d1bb89fc02707907988834ef147769ddaf)
1 #include <errno.h>
2 #include <float.h>
3 #include <math.h>
4 
5 static	long	tab[] =
6 {
7 	1L, 10L, 100L, 1000L, 10000L,
8 	100000L, 1000000L, 10000000L
9 };
10 
11 double
pow10(int n)12 pow10(int n)
13 {
14 	int m;
15 
16 	if(n > DBL_MAX_10_EXP){
17 		errno = ERANGE;
18 		return HUGE_VAL;
19 	}
20 	if(n < DBL_MIN_10_EXP){
21 		errno = ERANGE;
22 		return 0.0;
23 	}
24 	if(n < 0)
25 		return 1/pow10(-n);
26 	if(n < sizeof(tab)/sizeof(tab[0]))
27 		return tab[n];
28 	m = n/2;
29 	return pow10(m) * pow10(n-m);
30 }
31