java - Using recursive power function in log function, without Math.functions -
like says in title have problem i'm required make own power , log methodss , use them on input. power function seems fine until try use in loga(b) function. 1 problem don't understand loop in log method copied 1 in class understand later. problem don't know how built in math.pow() works, if had code or pseudocode guess how own power function different. in advance , please go easy on me i'm still quite new of this.
import java.util.scanner; public class numbers { public static void main(string[] args) { scanner input = new scanner(system.in); double i1; double i2; system.out.print("type a:"); i1 = input.nextdouble(); system.out.print("type b:"); i2 = input.nextdouble(); input.close(); sumrange(i1, i2); power(i1, i2); log(i1, i2); system.out.println("sum b: " + sumrange(i1, i2) + "\nfibonacci a: " + fibonacci(i1) + "\nfibonacci b: " + fibonacci(i2) + "\na^b:" + power(i1, i2) + "\na%b: " + mod(i1, i2) + "\nloga (b)" + log(i1, i2)); } // if i'm honest don't understand how log code works exactly, // know code calculates correct log // math.pow() function // own power function doesn't seem work , don't know how // math.pow() // function different mine static double log(double i1, double i2) { double value = 0; (double = 1; > .001; /= 10) { while (!(power(i1, value) > i2)) { value += i; } value -= i; } return value; } static double power(double i1, double i2) { if (i2 == 0) { return 1; } if (i2 == 1) { return i1; } // line below seems cause problems since used in log(double, // double) method return i1 * power(i1, i2 - 1); } // excluded 3 other methods work fine , don't depend on // each other work
your power method works integer i2
, else recursion never terminate.
for non-integer i2
@ least linear interpolation after reduction between 0
, 1
. bad approximation large basis values i1
better nothing.
if( 0<=i2 , i2 <=1) return 1+i2*(i1-1);
what negative i2
?
if( i2 < 0 ) return 1/power(i1,-i2);
google "libm/exp.c" professional implementations of exponential in c
standard library, there different variants, , "libm/pow.c".
Comments
Post a Comment