Phix, numbers, printf, log10
- Posted by RobertS in February
- 1170 views
Phix 64-bit, but it similarily happens with 32-bit:
printf(1, "%1.2e", 2.25)
2.26e+0
printf(1, "%1.2e", 2.48)
2.49e+0
printf(1, "%1.2e", 2.428)
2.42e+0 truncating instead of rounding
It happens with all kinds of numbers, and any number of decimal digits — the last digit tends to be hit or miss.
Also, though a bit less annoying, %f has a "5" rounding issue, this too happens with all kinds of numbers and any number of decimal digits (also happens with Euphoria, as far as I can remember):
printf(1, "%0.1f", 2.75)
2.8 rounding up
printf(1, "%0.1f", 2.85)
2.8 rounding down
Something else: what is the best way to find the magnitude (exponent in scientific notation) of x? I've been using floor(log10(x)), but, probably due to how log10 is calculated, it's not entirely reliable:
? floor(log10(1000))
3 correct
? floor(log10(1000 - 1e-16))
2 correct
? floor(log10(1000 + 1e-16))
2 badly wrong
printf("%e", x) seems to get the exponent more reliably, but using sprintf("%e", x) and then reading the number after the "e" from the string is rather clumsy, and rounding can become an issue, too.
I think I've managed to work around these issues (thought this before, though), but things might be easier if sprint were able to return more than 10 significant digits (I'd be content with 15).
Thanks for any help or clarification where I may be confused,
Robert