Re: counting the digits in a number, exponential format problem [message #170985 is a reply to message #170979] |
Mon, 13 December 2010 19:28 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 12/13/2010 1:46 PM, -matt wrote:
> Hi,
>
> I was wondering if anyone had a good way of counting the digits in a
> given (general) number.
>
> Obviously this is trivial for certain numbers. For example, the number
> of digits in 523 (or any other natural number) can be found either by:
>
> ceil(log10(523)) or strlen(523)… and I am sure there are other ways.
>
> Once you start dealing with floats it gets a little more complicated,
> but there are solutions. For example, the number of digits in 3.3 can
> be found with:
>
> $value = 3.3;
> if ((int)$value == $value)
> {
> return strlen($value);
> } else {
> return strlen($value)-1; //the -1 removes the count for the ‘.’
> }
>
> In general the above equation SHOULD work for any float or double, but
> it does not. My problem arises when PHP uses exponential formatting
> and for some reason there is a break down at E-5. For example:
>
> $value = 3.3E-4;
> if ((int)$value == $value)
> {
> return strlen($value);
> } else {
> return strlen($value)-1;
> }
>
> returns the value 6, which is correct. (There are 6 digits in
> 0.00033). However, if we try:
>
> $value = 3.3E-5;
> if ((int)$value == $value)
> {
> return strlen($value);
> } else {
> return strlen($value)-1;
> }
>
> it returns 5. The reason it does this is because at E-5 it appears
> that PHP stops using the full decimal value and leaves it in the
> exponential format. Thus strlen no longer makes sense because it does
> strlen(3.3E-5) (and treats not only the ‘.’ but also the ‘E’ and ‘-‘
> as characters and includes them in the length calculation) instead of
> converting the number to a value and performing strlen(0.000033). Even
> if I alter the function to cast the value [return strlen((float)
> $value)-1;] it still returns 5.
>
> There is some difference in PHP between numbers which are E-4 and
> numbers which are E-5. The numbers I care about go out to E-8.
>
> If anyone knows why PHP does this, how to force PHP to not use
> exponential formatting, or how to count digits in an exponentially
> formatted float less than E-4 I would appreciate the help.
>
> P.S. I have tried ini_set("precision", "12"); and the like, but that
> does not help. My interest is not in adjusting PHP’s precision, only
> how it treats the numbers.
strlen() works on strings, not values (hence the name). And floating
point numbers are almost never exact values - i.e. 0.00033 is actually
0.00032999999999999999819588758498412062181159853935242 (plus or minus,
depending on your precision).
This is not PHP - this is how floating point numbers work in most
computers and languages (the exception being those which support BCD
formats).
As for formatting - there are any number of ways to format the data, i.e.
$s = sprintf("$12.10f");
echo strlen($s) . "->$s\n";
Prints
12->0.0000033000
Of course, you can also strip trailing zeros, if you wish.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|