Rendered at 12:59:26 GMT+0000 (Coordinated Universal Time) with Cloudflare Workers.
sheept 9 hours ago [-]
In JavaScript, it's
Math.PI.toString(2)
Who knows how optimized Python or JavaScript's implementations are, but I'd imagine printing the binary representation of floating point numbers is relatively easy to implement manually because you'd just take 1 followed by the bits of the mantissa, then move the decimal point based on the exponent.
adrian_b 3 hours ago [-]
That shown in TFA is obtained by using the conversion specifier "%a" in the "printf" family of functions from "stdio", i.e. from the standard C library.
The conversion specifier "%A" works in the same way, but uses upper-case letters.
These conversion specifiers were added in the 1999 C standard.
I bet that Python and JavaScript just use "snprintf", like any other language.
In my opinion, whenever floating point numbers are written as strings with the intentions to be read again, one should use "%a" or "%A", because the conversion is exact and much faster than when doing a binary-to-decimal-to-binary conversion.
Even if the written numbers are inspected by humans, the hexadecimal representation is preferable, because when there are many digits it is even easier to use for comparisons (either for equality or for order) than the decimal representation. Any human who inspects a table of such numbers would do only comparisons, without attempting to do mentally more complicated arithmetic operations, which would be too difficult even with decimal numbers with many digits.
The conversion specifier "%A" works in the same way, but uses upper-case letters.
These conversion specifiers were added in the 1999 C standard.
I bet that Python and JavaScript just use "snprintf", like any other language.
In my opinion, whenever floating point numbers are written as strings with the intentions to be read again, one should use "%a" or "%A", because the conversion is exact and much faster than when doing a binary-to-decimal-to-binary conversion.
Even if the written numbers are inspected by humans, the hexadecimal representation is preferable, because when there are many digits it is even easier to use for comparisons (either for equality or for order) than the decimal representation. Any human who inspects a table of such numbers would do only comparisons, without attempting to do mentally more complicated arithmetic operations, which would be too difficult even with decimal numbers with many digits.