Search code examples
perldocumentation

Where in the perl documentation does it mention 0.0001, 0.00001 printing differences?


Where in the perl documentation does it mention when one will get exponential format here?

$ perl -wle 'print for 0.001, 0.0001, 0.00001, 1.00001;'
0.001
0.0001
1e-05
1.00001

No I am not talking about printf today. Where does it say something like "numbers tinier than 0.0001 will get printed differently"?


Solution

  • It's not very clear or easy to find, no. You have to dig into the internals to get anything relevant.

    From perlapi:

    Gconvert

    This preprocessor macro is defined to convert a floating point number to a string without a trailing decimal point. This emulates the behavior of sprintf("%g"), but is sometimes much more efficient. If gconvert() is not available, but gcvt() drops the trailing decimal point, then gcvt() is used. If all else fails, a macro using sprintf("%g") is used.

    And perlnumber says that conversions from native floating point to decimal string "involve steps performed by the C compiler", which I assume means the aforementioned Gconvert macro. So you have to look into how those functions behave on your system for the gritty details about when it switches between regular and scientific notation to render a number.