PHP Classes

PHP can do this natively

Recommend this page to a friend!

      Root66  >  All threads  >  PHP can do this natively  >  (Un) Subscribe thread alerts  
Subject:PHP can do this natively
Summary:the pow() function can be used to obtain roots
Messages:3
Author:Michael Richey
Date:2023-02-19 22:27:23
 

  1. PHP can do this natively   Reply   Report abuse  
Picture of Michael Richey Michael Richey - 2023-02-19 22:27:23
I hate to do this to you, because you obviously put work into this class. This can be accomplished with a single built-in function.

#Cube root of 27:
echo pow(27,1/3); // returns 3

#4th root of 256:
echo pow(256,1/4); // returns 4

#5th root of 3125:
echo pow(3125,1/5); // returns 5

#6th root of 46656:
echo pow(46656,1/6); // returns 6

#7th root of 823543:
echo pow(823543,1/7); // returns 7

It isn't necessary to use a fraction, you can substitute a float if you know it - or if you calculate it another way. pow() treats 1/4 the same as 0.25.

  2. Re: PHP can do this natively   Reply   Report abuse  
Picture of Lance Otis Lance Otis - 2023-02-19 23:33:12 - In reply to message 1 from Michael Richey
Micheal: Yep I know. Thank you for the feedback. The effort was merely to demonstrate the academic examination of the means to create a non-iterating class that did not use PHP's built-in root function. Otherwise it would be a non-educational useless single element class. I did use the ^ for integers power vs a*a*a*a*a just to keep the it simple.
// Lance

  3. Re: PHP can do this natively   Reply   Report abuse  
Picture of Lance Otis Lance Otis - 2023-02-19 23:46:15 - In reply to message 2 from Lance Otis
Micheal; just to clear things up the ^ in the previous message was my shorthand for pow(a,x). This was noted in the readme file: "(PHP powers are denoted by pow(num,pow) here we are using carat notation: num^pow)".
// Respectfully, Lance