Re: counting the digits in a number, exponential format problem [message #170999 is a reply to message #170998] |
Tue, 14 December 2010 16:51 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
-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.
well
(a) thats jerry but
(b) internally it does know the difference, The fact that it seamlessly
converts between at its own whim is typical of the noddy approach of
modern languages and software, that likes to make what its designers
consider is the right decision on your behalf, because you are too
stoopid to tell it exactly what to do.
In Jerry's case, this is a fully justified assumption.
>
> in response to Alvaro, the reason i would like to count the digits is
> because i do not know how wide each number is in a set of numbers and
> for each number i need to set a width for a placeholder for the
> number. i do not want to force a certain number of digits to always be
> outputted so it is constant width, so i would like to adjust my
> placeholder width based on the number width.
well try something like strlen(sprintf("0.03%f",$mynum)) then..unless
you really want to force it, the language will have at some point a
decision whether to output in decimal or exponential format.
|
|
|