Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

php - Calculating roots with bc_math or GMP

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...