0

I am building one calculator and facing this basic problem. How can I get the number when I am doing square root and then square of that number. Here is the problem and help me to get the number back after doing square.

public static void main(String[] args) {

    double aa = 3;
    System.out.println(aa); // output 3.0

    double bb = Math.sqrt(aa);
    System.out.println(bb); // output 1.7320508075688772
    double cc = Math.pow(bb, 2);
    System.out.println(cc); // output 2.9999999999999996, why not 3 ??      

}
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
Abhijit
  • 360
  • 4
  • 14
  • 1
    This is a floating point precision error. Don't expect to get exactly the right value. – KSFT Mar 29 '15 at 19:58
  • you should go through post to understand this problem http://stackoverflow.com/questions/588004/is-floating-point-math-broken?rq=1 – codiacTushki Mar 29 '15 at 20:05
  • I read ur links but could not get what code i have to do to get result. I understand it is a precision issue with any fraction number, but what i have to do to get it solve? I want sqrt of 3 X sqrt of 3 = 3 – Abhijit Mar 29 '15 at 20:23

0 Answers0