Powers in a class variable declaration?


#1

Hi, I was wondering how to get around this.

[php]
class SIPrefix
{ protected static $factor = array(
‘yotta’ => pow( 10, 24 ),
‘zetta’ => pow( 10, 21 )
);
}
[/php]( this is a mere example, nothing useful or concrete )

This isn’t going to work. However I do want ‘yotta’ to point at 10^24.
For decimals I can catch this with 10e24, but how about powers of 2?


#2

Right now I’m going for the somewhat hakkitakki solution of using an ‘init()’.

[php]
class SIPrefix
{ protected static $power = array(
‘yotta’ => 24,
‘zetta’ => 21
);
protected static $factor = array();

public static function init()
{  foreach ( $power as $prefix => $pow )
    {  $factor[$prefix] = pow( 10, $pow );
    }
}

}

SIPrefix::init();
[/php] ( this is a mere example, nothing useful or concrete )

That seems to work but doesn’t get points for elegance. (IMHO)

( BTW, the 10e5 notation doesn’t like 10e-2 )


#3

Well, it seems that the “pow” function does not work with decimals.
Looking at the details on this function at: http://php.net/manual/en/function.pow.php
I found around the 8th user post down the page a bit had a solution for using decimals.
The “pow” function also does not work with certain factors. It seems the problem might be how the variable is declared. General numbers are limited to something like 16 chars. So, the results can be truncated.
Also, once you add in a negative power, same issue, limited to a certain number of chars.

I would look at the other math functions such as “exp”. One very important note is that the negative
powers work more correctly in higher versions of PHP. They do not work well in older versions, but,
seem to work better in version 5+… So, check your PHP version.


#4

The problem is that in that location PHP won’t accept any functions. Or operators.
Yet I seriously do not feel like typing out a googleplex.

I don’t see where the decimals come in, I’m only using base 10 and integer exponents.

The exp function doesn’t change anything to the problem. exp will also cause an error.
I mentioned the ‘e’ notation, that helped with positive powers of ten. ( So I’d still have to type 0.0000000000000001 completely… and it’s easy to skip a ‘0’ )
I’m getting the feeling the init() solution is the only thing that works. (But I don’t like it)


#5

Well, I do not really understand what you need. You mentioned you only need integers and powers of ten.
But, you also talked about ‘E’ values and typing .000…001 …

I looked at the PHP POW function and it should be able to create any decimal power you need. The only issue is that it will not go over a max amount which is limited by the way that PHP stores numbers. PHP uses floats, or signed 32-bit ints to store numbers. This is a tight limit on size of the results and usually turns it into an ‘E’ value.

You could use GMP math functions, but, they limit it to 10-to-the-50th powers.

You can use the BC-math version which is supposed to be for larger numbers. But, the question for you is why you need this and does it have to be a number? If your numbers are used in math functions, then might try the BCmath fuctions. Such as bcpow(); If not needed for math, but, for display purposes, you could create a simple text function to simulate the numbers.

Sorry if I have not given you an exact answer, but, 32 bits is 32 bits… Hope it helps someway…


#6

Thank you ErnieAlex.
I guess I’m just trying to do things where I’m not supposed to do them. You are right on the functions you mentioned, they do the calculations I want. Just not in the location where I try to do them. With the initialisation method I can get it all to work. I should just use that and not try to make things too ‘beautiful’. ( that’s a strictly personal opinion )

Thank you for your help and time.
O.

Oh, and by the way, 0.0001 is an integer power of ten, it’s 10^-4. :wink:


#7

Well, it is interesting that there is actually a lot of advanced math functions for PHP.
And, yes, on the int .0001… I was blank’n that out… Good luck, if it works, don’t fix it… LOL