I like the OP approach to let printf()
, using the "%e"
format, to do the heavy lifting.
There are many issues with double
to string conversion that existing functions already handle nicely. (INF, Nan, -0, rounding, negatives). OP use of frexp()
, power()
typically have troubles near values a power of 10 do to rounding and and finite precision.
OP is fuzzy on how much precision given only 1 example. power(0.5, 100)
would need 100 digits. Let's limit it to DBL_DIG, rather than use the %e
default of 6. (which is really 7 digits, just 6 after the DP.)
char *Heyhey_Notation(char *dest, double x) {
static const char Times[] = " times 10 to the power ";
char buffer[3 + DBL_DIG + sizeof(Times) + 20 + 1];
sprintf(buffer, "%.*e", DBL_DIG, x);
char *e = strchr(buffer, 'e'); // find exponent position
if (e) {
char *zero = e;
while (zero[-1] == '0') zero--;
*zero = '\0'; // OP wants excess zeros trimmed.
int power = atoi(&e[1]); // trim excess zeros by converting to int
sprintf(dest, "%s%s%d", buffer, Times, power);
}
else {
strcpy(dest, buffer);
}
return dest;
}
int main() {
char buf[100];
puts(Heyhey_Notation(buf, 3349.25));
puts(Heyhey_Notation(buf, -0.0));
puts(Heyhey_Notation(buf, 123));
puts(Heyhey_Notation(buf, -1234567890.0/9999999999.0));
puts(Heyhey_Notation(buf, -1/0.0));
puts(Heyhey_Notation(buf, atof("nan")));
return 0;
}
3.34925 times 10 to the power 3
-0. times 10 to the power 0
1.23 times 10 to the power 2
-1.234567890123457 times 10 to the power -1
-inf
nan