![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Discrepancy in /var/adm/messages | sundar3350 | SUN Solaris | 15 | 03-11-2009 11:23 AM |
| How do I get past an HTML::entities discrepancy on an RPM? | jjinno | UNIX for Dummies Questions & Answers | 2 | 08-07-2007 01:39 PM |
| VSFTP oddity | Spetnik | UNIX for Dummies Questions & Answers | 7 | 04-28-2004 03:58 PM |
| NIS login discrepancy | Henrik | UNIX for Dummies Questions & Answers | 11 | 04-08-2003 10:17 AM |
| Su Password Oddity | jacobsa | UNIX for Dummies Questions & Answers | 4 | 05-30-2002 08:50 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Java double subtraction oddity (not the usual rounding discrepancy)
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|