I've been going through a java tutorial, and ran across some strangeness in this small example...
Code:
class SqrRoot {
public static void main(String args[]) {
double num,sroot,rerr,resquare;
for(num = 1.0; num < 100.0; num++) {
sroot = Math.sqrt(num);
System.out.println("Square root of " + num + " is " + sroot);
// compute rounding error
resquare = (sroot * sroot);
rerr = num - resquare;
System.out.println("Rounding error: num + " - " + resquare + " = " + rerr);
System.out.println();
}
}
}
The return is strange -- when I saw the results, I written a small program that subtracted a number of literal floating point numbers, all with the normal binary to decimal oddness (1.002 - 1.001 = .000999999997); yet when I run the above code, I get what's below:
Code:
Square root of 1.0 is 1.0
1.0 - 1.0 = 0.0
Square root of 2.0 is 1.4142135623730951
2.0 - 2.0000000000000004 = -4.440892098500626E-16
Square root of 3.0 is 1.7320508075688772
3.0 - 2.9999999999999996 = 4.440892098500626E-16
....
I'm not sure if it's something I'm doing wrong or what is causing it. I've searched all over and all I've found is people asking about the hardware limitations of floating point arithmetic. Any help would be greatly appreciated.
-----Post Update-----
After seeing it posted here I just realized I wasn't taking into account the scientific notation... Boy to I feel dumb now
