Java wtf

Discussion in 'OT Technology' started by choler, Jul 15, 2004.

  1. choler

    choler New Member

    Joined:
    May 21, 2002
    Messages:
    183
    Likes Received:
    0
    Code:
    System.out.println(902.24);
        System.out.println(2714.40);
        System.out.println(902.24+2714.40);

    Why is this printing out

    Code:
    902.24
    2714.4
    3616.6400000000003
    ??????
     
  2. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    thats the answer. if you want it rounded off use DecimalFormat

    Code:
    import java.text.DecimalFormat;
    public class test
    {
    	public static void main(String args[])
    	{
    		DecimalFormat two = new DecimalFormat("0.00");
    		System.out.println(two.format(902.24+2714.40));
    	}
    }
    
     
  3. choler

    choler New Member

    Joined:
    May 21, 2002
    Messages:
    183
    Likes Received:
    0
    u are missing the point of the question... where is it getting the phantom 00000000003 from ??


    I shouldn't have to format it. The answer should be 3616.64 not 3616.6400000000003....
     
  4. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    Just because you put in 902.24 and 2714.40 doesn't mean that that is all the compiler sees. It may have garbage at the end of the number that you didn't put there but may have been in memory before. Or it may not be precise to two decimal places. In either case, that could be causing that extra garbage at the end of the number.

    Plus, even if that 3 wasn't there, the default precision it displays might just be 10 decimal places or whatever. Unless you specifically say how many decimal places you want to display, it'll put up as many as it wants. So yeah, you do have to format it.
     
  5. choler

    choler New Member

    Joined:
    May 21, 2002
    Messages:
    183
    Likes Received:
    0
    thats just gibberish...

    anyone else know y this is happening...
     
    Last edited: Jul 16, 2004
  6. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    Gibberish? I can't tell if you're trying to tell me that I'm wrong or if you just don't understand. Layman's terms: If you don't format it exactly how you want it to display, you could get unexpected results. Period.
     
  7. Shaggy007

    Shaggy007 New Member

    Joined:
    Oct 22, 2003
    Messages:
    901
    Likes Received:
    0
    Location:
    In the Springtime of my youth
    Try changing it over to a float variable.

    It's possible that because you're using a raw value the compiler is treating it as a string and just making the compiler parse it back to a int, this extra parsing might be giving you the munked up extra numbers.

    Java does automatic memory management so whatever memory he's accessing would already be freed up when he's using it.
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    It's probably just rounding error. It's a problem intrinsic to floating point numbers. It's the result of trying to approximate an infinite number of values using a finite amount of bit combinations.
     
  9. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    Watch out, you may be speaking "gibberish." Seems like he wants an explanation like "The little magic elf inside your computer is putting that extra set of numbers at the end just to fuck with your mind."
     
  10. choler

    choler New Member

    Joined:
    May 21, 2002
    Messages:
    183
    Likes Received:
    0
    It was gibberish because...

    1. Now that means... the garbage data shouldn't be there :rolleyes:

    2. Also seeming that the same thing happens across multiple runs on multiple machine, it would have be magical garbage data (because its traveling across computers and os'es) :rolleyes:

    -------

    Code:
    double t1 = 902.24;
        double t2 = 2714.4;
        double sum = t1 + t2;
        System.out.println( sum );
    that doesn't work either... :confused:



    I am not forcing any rounding here. All I am doing is adding 2 double numbers. Adding 902.24 and 2714.4 actually does equal 3616.64 (even when u do it on paper). And even if I was rounding here, I should have 3616.6 or 3617 (not the 3616.6400000000003).

    Code:
    float t1 = 902.24F;
        float t2 = 2714.40F;
        float sum = t1 + t2;
        System.out.println( sum );
    This works (which was expected) so wtf is happening with doubles... :confused: & :rant2:.

    There is something weird going on because it's not like those number are freaks, it happens in variety of cases. (51.06 + 49.9 = 100.96000000000001).

    They actually had informed answers. I realize I hurt u’re e-pride but when you post something in a public forum, think about what you are saying.

    http://www.amazon.com/exec/obidos/A...9988923/sr=2-1/ref=sr_2_1/002-0116309-3256047
     
  11. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    Alright, I'll STFU now. I was just expressing what I thought could have been the issue. If this has something to do with the design of the Java compiler (which I assume you think it is based on your link to Amazon), then I'm way out of my league. Maybe my e-pride was hurt a little, but when you blew off my input as gibberish without saying why it's gibberish, I felt like snapping back.

    I have to remember that not everyone who posts in this forum are idiots when it comes to programming. I will now gracefully bow out and let you all discuss something that is obviously over my head.
     
  12. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
  13. Shaggy007

    Shaggy007 New Member

    Joined:
    Oct 22, 2003
    Messages:
    901
    Likes Received:
    0
    Location:
    In the Springtime of my youth
    Well did a little research and it is a rounding error, we're working in a base 2 system and in that system .24 which is 6/25 can't be represented in a simple way. It will be a trailing number. So because it is a trailing number it does get rounded off at the end. Hence your extra zeros.

    It works fine with the floats because the float truncates the extra spaces and you get your nice .64

    If you change the number to XX.23 and xx.41, the error doesn't happen, but you get the same answer.

    So you're just going to have to use the Decimalformt function.

    I hope you're happy you made me fire up a java compiler again. :eek4:

    So now can't we all just get a long :hug:
     
  14. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    :hug:
     

Share This Page