tags:

views:

170

answers:

2

I'm having trouble calculating roots of rather large numbers using bc_math, example:

 - pow(2, 2)     // 4, power correct
 - pow(4, 0.5)   // 2, square root correct
 - bcpow(2, 2)   // 4, power correct
 - bcpow(4, 0.5)     // 1, square root INCORRECT

Does anybody knows how I can circumvent this? gmp_pow() also doesn't work.

+1  A: 

I'm not a PHP programmer but looking at the manual it says you have to pass them in as strings i.e.

bcpow( '4', '0.5' )

Does that help?

Edit: The user contributed notes in the manual page confirm that it doesn't support non-integer exponents.

I did come across this discussion of a PHP N-th root algorithm after a quick search so perhaps that's what you require.

Troubadour
No, it still outputs 1 instead of 2.
Alix Axel
@eyze: Does `bcsqrt` work?
Troubadour
Yes, bcsqrt works but I also need to find roots with other indexes such as 3, 5 and so on.
Alix Axel
@eyze: Yes, I know. I just wanted to check that it was working.
Troubadour
The last link you provided is useful however it wrongly returns 1.5 for the square root of 2, is there any more precise workarround?
Alix Axel
+2  A: 

For future reference here it is:

Alix Axel
+1: Nice one! Thanks for sharing.
Troubadour
Yeah, but it seems to be buggy. =( Check my comment: http://mgccl.com/2007/01/30/new-upgrade-on-bcroot-speed-boost#comment-2999
Alix Axel