Advertisement
Guest User

fractional exponents BaSH/bc extention

a guest
Oct 29th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.52 KB | None | 0 0
  1. #!/bin/bash
  2. scale=$1;
  3. echo ${scale:="20"} >> /dev/null;
  4. echo "number to raise:";read y;
  5. echo "numerator of exponent:";read n;
  6. echo "denominator of exponent:";read d;
  7. x=1;prev=0; #//x = guess;prev = loop control testing;
  8. for((z=0;z<50;)) do
  9. x=$(echo "scale=$scale;$x*(1-1/$d)+$y/$d/$x^($d - 1)"|bc); #//see newton method for finding the Nth root of a number.
  10. if [[ "$x" == "$prev" ]]; then z=555; fi;
  11. prev=$x;
  12. if [[ $(echo $2|head -c 2) == "-d" ]]; then echo $prev; fi;
  13. done;
  14. echo;echo -n "$y^($n/$d)="; echo "$x^$n"|bc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement