Re: counting the digits in a number, exponential format problem [message #171000 is a reply to message #170998] |
Tue, 14 December 2010 17:30 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 12/14/2010 10:27 AM, -matt wrote:
> maybe i did not word my question well enough... let me try again.
>
> first of all, this is not a question about machine precision. i am
> well aware of the limitations of floating point values and that some
> (many) numbers are not actually able to be exactly represented by a
> computer. for the sake of argument lets forget that, because i do not
> think my question relates to it at all.
>
> at its core, my question really wants to know why there is an
> inconsistency in PHP shown by the following example:
>
> strlen(3E-1) = strlen(0.3) = 3
> strlen(3E-2) = strlen(0.03) = 4
> strlen(3E-3) = strlen(0.003) = 5
> strlen(3E-4) = strlen(0.0003) = 6
>
> but then for some reason
>
> strlen(3E-5) = strlen((float)3E-5) = strlen(0.00003) = 6
> strlen(3E-6) = strlen((float)3E-5) = strlen(0.000003) = 6
>
> don't get hung up on the numbers. this has happened for any number i
> have chosen in the jump between E-4 and E-5. there just seems to be
> some inconsistency in how PHP internally represents the variables. and
> i would like to know if anyone know what it is, why it is, and/or how
> to avoid it?
>
> also, in response to Jerry, PHP is a weakly typed language which means
> it doesn't require (nor support for that matter) explicit type
> declaration of variables. so saying that strlen only works on strings
> doesn't make sense. in PHP a variable is a variable is a variable.
> there is no difference.
>
Untrue. There are differences between various types; it's just that PHP
converts between them under the covers. And sometimes this causes
problems, i.e. 0 == false, but 0 !== false.
If you give strlen() a numeric value, PHP has to convert this to a
string before taking the length. The result may or may not be what you
want.
For more information on types, see
http://www.php.net/manual/en/language.types.php.
For more information on type juggling, see
http://www.php.net/manual/en/language.types.type-juggling.php
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|