Re: counting the digits in a number, exponential format problem [message #176342 is a reply to message #176341] |
Sat, 24 December 2011 03:31 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On Fri, 23 Dec 2011 13:00:44 -0600, Gordon Burditt wrote:
>> @Jerry: i totally agree with you that PHP must convert a numeric value
>> passed to strlen() to a string at some point. what i do not understand
>> though is why it makes a difference whether the numeric is 1E-4 or
>> 1E-5. it is like that single order of magnitude drastically changes the
>> logic/conversion process of the numeric to a string and causes the
>> answer to not, as you say, 'be what i want'.
>
> Read the description of the (C language, but also applies to PHP)
> *printf format conversion %g. "The argument is printed in style f or in
> style e, whichever gives full precision in minimum space". Guess where
> it switches?
For the specific case given by the OP, it could switch over at 1.0E-3 /
1.0E-4 instead of 1.0E-4 / 1.0E-5, given that both representations give
the same number of characters.
0.001 = 1.0E-3 <- fixed format shorter
0.0001 = 1.0E-4 <- both same length
0.00001 = 1.0E-5 <- exp format shorter
In the more general case, it won't always switch over at the E-4 / E-5
boundary either, it seems from writing down various decimal fractions in
both formats that E-3 is more commonly the point where both forms take
the same space. Where the exponent is -2 or higher (in the sense that 0
is higher than -1 is higher than -2) then the number will probably be
displayed in fixed format, and where the exponent is -5 or lower (in the
same sense as above) then the number will probably be displayed in
exponential format, but if the exponent is 3 or 4, then it depends on the
number of significant digits. In the latter case, behaviour might change
in future if the underlying c function changes slightly, even though such
a change might not result in the described operation of that function
being any different to the currently described and observed operation.
I guess that when the exponent travels away from 0 in a positive
direction the effect doesn't occur, because if the digits to the left of
the fixed format decimal point are significant, adding an exponent will
always increase the space taken by the space used by the exponential
representation:
1234567890.0123456789
1.2345678900123456789E9
Rgds
Denis McMahon
|
|
|