对数计算器精度为双倍

时间:2017-11-02 22:14:12

标签: java

我试图制作一个对数计算器并遇到一个问题,我的程序从0.099999开始而不是从0.0开始增加0.1以找到功率所以正确的结果被跳过,因为它不是一个确切的值(I知道我可能有最差的算法,但我还在尝试新事物。

import java.util.*;

public class Main{
public static void main(String args[]){

    double base = 0;
    double answer = 0;
    double answer2 = 0;
    double power = 0;
    Scanner x = new Scanner(System.in);

    System.out.print("Enter base: ");
    base = Double.parseDouble(x.next());

    System.out.print("\nEnter answer: ");
    answer = Integer.parseInt(x.next());


    if(answer != 1){

    while(answer2 != answer){

        power +=0.1;
        answer2 = Math.pow(base,power);
        System.out.println("power: " +  power + " answer: "+ answer2); 

    }

}

    System.out.println("the power is: " +power);



}

}

0 个答案:

没有答案