6 Converts string of char into a double.
7 Supports scientific notation, signed exponents.
9 Warning: very large or small numbers exibit rounding errors.
15 long double val
, power
, expower
;
17 for(i
=0; isspace(s
[i
]); i
++) // skip spaces
20 sign
= (s
[i
] == '-') ? -1 : 1; // mark sign for number
21 while(!isdigit(s
[i
])) // skip over signs and non numbers, dollar signs
25 while(isdigit(s
[i
])) // calc val of nums before decimal
27 val
= 10.0 * val
+ (s
[i
] - '0');
31 if(s
[i
] == '.') // skip over decimals
35 while(isdigit(s
[i
])) // calc val of nums after decimal
37 val
= 10.0 * val
+ (s
[i
] - '0');
42 val
= sign
* val
/ power
;
45 if(s
[i
] == 'e' || s
[i
] == 'E')
47 ++i
; // move past e or E
48 exsign
= (s
[i
] == '-') ? -1 : 1; // mark sign for number
49 while(!isdigit(s
[i
])) // skip over sign(s)
55 expower
= 10.0 * expower
+ (s
[i
] - '0');
60 /* cannot get pow() to work in math.h ... improvising :) */
63 while(expower
> 0) { val
= val
* 10; --expower
; }
65 while(expower
> 0) { val
= val
/ 10; --expower
; }
72 char s
[] = "-123.456e-7";
76 char r
[] = "123.45e-6";
80 char t
[] = "123.45E6";
84 char u
[] = "-123.45e56";