chrischen Posted April 12, 2008 Share Posted April 12, 2008 Hi I need to write a PHP function for this equation: [ ((2/3)(x-50))^(1/3) ] + 4 The problem is that PHP converts the 1/3 to a decimal or something, but I need it to take the cube root. Right now with that equation above in PHP any X value under 50 produces a nonreal answer and thus NaN as a result. The equation works fine on my Ti-84. So basically I need to figure out how to take cube roots in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/100818-solved-convert-cube-root-function-into-php/ Share on other sites More sharing options...
AndyB Posted April 12, 2008 Share Posted April 12, 2008 http://ca.php.net/manual/en/function.pow.php - does that help? Quote Link to comment https://forums.phpfreaks.com/topic/100818-solved-convert-cube-root-function-into-php/#findComment-515565 Share on other sites More sharing options...
chrischen Posted April 12, 2008 Author Share Posted April 12, 2008 I tried using pow($base, 1/3) however any negative number still becomes nonreal. So it's not taking the cube root. Quote Link to comment https://forums.phpfreaks.com/topic/100818-solved-convert-cube-root-function-into-php/#findComment-515566 Share on other sites More sharing options...
chrischen Posted April 12, 2008 Author Share Posted April 12, 2008 $ret = exp( (1/3) * log((2/3)*($x-50)) ) + 4; I used that but same thing, negative numbers are still not cube rooted. It returns NAN if X < 50. Quote Link to comment https://forums.phpfreaks.com/topic/100818-solved-convert-cube-root-function-into-php/#findComment-515569 Share on other sites More sharing options...
AndyB Posted April 12, 2008 Share Posted April 12, 2008 If I understand, the only problem now is when x<50. Why not test x-50 to decide whether the cube root of the absolute value of x-50 needs to be multiplied by -1 or not? Quote Link to comment https://forums.phpfreaks.com/topic/100818-solved-convert-cube-root-function-into-php/#findComment-515595 Share on other sites More sharing options...
chrischen Posted April 12, 2008 Author Share Posted April 12, 2008 Acutally I solved it by using this algorithm to get the cube root. Works with negatives too. function cube( $c ) { $x = 2; do { $x = $x - (pow($x,3) - $c)/(3*pow($x,2)); $count++; } while($count < 50); return $x; } Quote Link to comment https://forums.phpfreaks.com/topic/100818-solved-convert-cube-root-function-into-php/#findComment-515611 Share on other sites More sharing options...
chrischen Posted April 12, 2008 Author Share Posted April 12, 2008 Ignore this Quote Link to comment https://forums.phpfreaks.com/topic/100818-solved-convert-cube-root-function-into-php/#findComment-515622 Share on other sites More sharing options...
chrischen Posted April 12, 2008 Author Share Posted April 12, 2008 For some reason if you try to cube root -16 it breaks... So I changed $x to equal 3 instead of two. All number from -99 to 99 have been tested. function cube( $c ) { $x = 3; do { $x = $x - (pow($x,3) - $c)/(3*pow($x,2)); $count++; } while($count < 50); return $x; } Quote Link to comment https://forums.phpfreaks.com/topic/100818-solved-convert-cube-root-function-into-php/#findComment-515636 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.