The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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 12:39 PM
VSFTP oddity Spetnik UNIX for Dummies Questions & Answers 7 04-28-2004 02:58 PM
NIS login discrepancy Henrik UNIX for Dummies Questions & Answers 11 04-08-2003 09:17 AM
Su Password Oddity jacobsa UNIX for Dummies Questions & Answers 4 05-30-2002 07:50 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-13-2009
Trones Trones is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 5
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
  #2 (permalink)  
Old 06-14-2009
CRGreathouse CRGreathouse is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 104
Yep, you found it. That's just the usual roundoff error, shown to too many decimal places.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0