wie es im Titel sagt Ich habe das Problem, dass ich meine eigenen Macht und Log-Methoden machen muss und sie auf meine Eingabe verwenden. Meine Power-Funktion scheint in Ordnung, bis ich versuche, es in meiner Loga (b) -Funktion zu verwenden. Ein Problem ist, dass ich die for-Schleife in meiner Protokollmethode nicht verstehe, da ich diese in der Klasse kopiert habe, um sie später zu verstehen. Ein anderes Problem ist, dass ich nicht weiß, wie das eingebaute Math.pow() funktioniert, wenn ich Code oder Pseudocode dafür hätte, könnte ich erraten, wie meine eigene Power-Funktion anders ist. Vielen Dank im Voraus und bitte gehen Sie einfach auf mich Ich bin immer noch ziemlich neu in all dem.Verwendung rekursiver Power-Funktion in Log-Funktion, ohne Math.Functions
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 your a:");
i1 = input.nextDouble();
System.out.print("type your b:");
i2 = input.nextDouble();
input.close();
sumRange(i1, i2);
power(i1, i2);
log(i1, i2);
System.out.println("Sum from a to b: " + sumRange(i1, i2) + "\nFibonacci for A: " + fibonacci(i1)
+ "\nFibonacci for B: " + fibonacci(i2) + "\nA^B:" + power(i1, i2) + "\na%b: " + mod(i1, i2)
+ "\nloga (b)" + log(i1, i2));
}
// if I'm honest I don't understand how the log code works exactly,
// what I do know is that this code calculates the correct log
// with Math.pow() function
// My own power function doesn't seem to work and I don't know how the
// MAth.pow()
// function is different from mine
static double log(double i1, double i2) {
double value = 0;
for (double i = 1; i > .001; i /= 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;
}
// The line below seems to cause problems since used in log(double,
// double) method
return i1 * power(i1, i2 - 1);
}
// I excluded my 3 other methods as they work fine and don't depend on
// each other to work
Es ist eine schlechte Idee, doppelte und ganzzahlige Literale in Ihren Vergleichen und arithmetischen Operationen zu mischen. – pjs