0

Here is my sqrt function. I want to output the double value with specific precision. But I was getting this output: "the sqrt of 0.5 is: 0.7071069999999999 " The odd thing is, when I was debugging, the longResult is 707107

public static double precision = 0.000001;

// the main sqrt function using binary search
public static double Cal(double input){
    if(input < 0) return -1;
    if(input == 0 || input == 1) return input;

    // the sqrt value shall be between 0 ~ input. we use 二分法 to search for the value;
    double min = 0;
    double max = input;
    if(input < 1) {
        min = input;
        max = 1;
    }

    double mid = 0;

    while(max - min > precision){
        mid = (min + max) / 2;
        if(mid * mid == input) return mid;
        else if(mid * mid < input) min = mid;
        else max = mid;
    }
    long longResult = (long)((max + min) / 2 / precision);
    return longResult * precision;
}

public void Test(){
    double input = 0.5;
    System.out.println(String.format("the sqrt of %s is: %s", input,Sqrt.Cal(input)));
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
cal wu
  • 1

2 Answers2

0

Because IEEE-754 double-precision floating point numbers are not perfectly precise.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

From A Change is Gonna Come Java Puzzlers:

Avoid Float and Double where exact answers are required

Rather use BigDecimal, int, or long instead

Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value.

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89