FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » counting the digits in a number, exponential format problem
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: counting the digits in a number, exponential format problem [message #170993 is a reply to message #170979] Tue, 14 December 2010 08:37 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 13/12/2010 19:46, -matt escribió/wrote:
> 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.

I'm not sure the whole subject really makes sense. First of all, the
number of digits depends on the base (obviously). Second, a number with
a fixed number of digits in base 10 may not have a representation in
base 2 (e.g. 1.01). Third, there'll be lots of cases where you actually
have infinite digits and you just cut at certain threshold (1/3 = 1.333).

If you absolutely need to do it, I see no other method that handling
numbers as strings rather than floats and pick a maximum number of
decimals when necessary. Compare:

<?php
var_dump( pow(101, 40) );
var_dump( bcpow('101', '40') );
?>

float(1.4888637335882E+80)
string(81)
" 148886373358822087497126463801738296202951256788286744534662595050969228887 804001 "

<?
var_dump(2/3);
var_dump( bcdiv('2', '3', 2) );
var_dump( bcdiv('2', '3', 10) );
var_dump( bcdiv('2', '3', 20) );
?>

float(0.66666666666667)
string(4) "0.66"
string(12) "0.6666666666"
string(22) "0.66666666666666666666"

<?php
var_dump( 10000000000000+0.5 );
var_dump( bcadd('10000000000000', '0.5', 1) );
?>

float(10000000000000)
string(16) "10000000000000.5"


Whatever, it may help to know the exact reason why you need to count
digits in the first place. Perhaps there's a better solution :-?


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: width measurements?
Next Topic: cheap, discount nike air max shoes, paypal payment, free shipping
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 04:10:46 GMT 2024

Total time taken to generate the page: 0.04310 seconds