Java double subtraction oddity (not the usual rounding discrepancy)


 
Thread Tools Search this Thread
Top Forums Programming Java double subtraction oddity (not the usual rounding discrepancy)
# 1  
Old 06-13-2009
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 Smilie
# 2  
Old 06-14-2009
Yep, you found it. That's just the usual roundoff error, shown to too many decimal places.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

System call oddity

Hi all, I'm trying to use bash to create a basic parser for some text, because basic grep/sed/awk have all my needs covered. So, I'm creating the chain of grep/sed I need and trying the execute that on the data I gather. The problem is, it's not working as it does when hardcoded. e.g. ... (3 Replies)
Discussion started by: beomagi
3 Replies

2. UNIX for Dummies Questions & Answers

[Solved] awk oddity

I have to apologize for my ignorance so this question is probably stupid. How does awk process a file? Does it read from top of input file to end of file going line by line? Yoda helped me create an awk script that helps me parse the named.conf file and output it into a .csv file but when... (8 Replies)
Discussion started by: djzah
8 Replies

3. Solaris

Sort Oddity

All, I'm baffled by some sort behavior on SunOS 5.10 and looking for guidance. Running the command: sort -t'|' -k5,5 -k7,7 -k1,1 -k2,2 -k3,3 -k6,6 -k8,8 test Against the file "test": thirdA||||first||second|Data thirdB||||first||second| thirdC||||first||second|Data... (11 Replies)
Discussion started by: effigy
11 Replies

4. BSD

Cron Oddity

Our webserver is running FreeBSD, for the last few days we have been having an issue with our cronjobs/mysql server. Specifically we have jobs that execute php scripts to do various things to the website. When running these php scripts manually, via external browser, or lynx on the server, they... (8 Replies)
Discussion started by: mrfr0g
8 Replies

5. UNIX for Dummies Questions & Answers

TR squeeze oddity

I discovered that where 'tr -s' works as expected on grepped input, it appears to completely fail on dig results. I am not sure if this is because of some sort of non posix compliancy, or what. Here is what I did: The command below works as expected, squeezing all repeated spaces to a single... (3 Replies)
Discussion started by: bdmeyersc
3 Replies

6. Shell Programming and Scripting

Not the usual ftp script question

I have an order file that gets updated periodically on a ftp server. The file format is "number one-day of the quarter-file number". For example, the format would look like this for today and would increment as such. 1-26-1 1-26-2 1-26-3 etc After I grab each oder file, I will rename... (9 Replies)
Discussion started by: cstovall
9 Replies

7. UNIX for Dummies Questions & Answers

VSFTP oddity

I am using smartFTP client on my windows machine to connect to a linux box of mine (different location) to download some large files 100+meg. The linux box is running vsftp. After several minutes of downloading, the connection suddenly dies, and I cannot reconnect to the box via FTP using ANY... (7 Replies)
Discussion started by: Spetnik
7 Replies

8. UNIX for Dummies Questions & Answers

Su Password Oddity

Hello Forum, I am having an odd occurence. When i do a su in a terminal or console session, I am able get authenticated as superuser without using the full or correct root password. Anyone have any idea? -AJ (4 Replies)
Discussion started by: jacobsa
4 Replies

9. UNIX for Dummies Questions & Answers

Textual oddity

This is my first post!! I really hope this question hasn't been asked a thousand times before(I'll bet it has). Anyway, here it is. I am new to the UNIX world by the way so forgive my ignorance. OK, whenever I write code on my windows machine and then try to port it over to UNIX I always get... (3 Replies)
Discussion started by: Jubba
3 Replies
Login or Register to Ask a Question