Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?

    public class Lincoln7
    {
    public static void main (String[] args)
    {
    int units;
    double money;

    money = 16.65;
    units = (int)(money * 100.0);
    System.out.println ("wrong answer " + units );

    }}


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    Answered already in cross-post in another forum.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    For anyone who can relate to this issue, see the answer here: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?

    As for jayjay, although cross-posting is allowed, you should always make us aware that you have done so, as to not waste anyone's time.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    Answered already...
    ...see the answer here...
    You know, just because the question was posted on another forum and received a response doesn't mean we can't have something to offer. Even if we don't think it's worth our while, I don't see any reason to be churlish about it.

    Furthermore, regardless of the Original Poster's point of view, I come here to learn, and so do lots of other people. There are literally hundreds of people who come to javaparogrammingforums.com each and every day to pick up hints and kinks and insight about problem solving with Java. Instead of giving them links to other places, maybe we can give other places reasons to give links to us.

    I mean, after pointing out the reason for the "wrong" answer (inexact representation of decimal fractional numbers as internal binary floating point numbers) maybe someone could point out there is a static Math function that rounds a floating point number to the nearest integral value.

    The result is still a floating point number, but its fractional part is equal to zero. Then, when you convert that rounded value to an int you get the "right" answer.

    I mean, different people have different ways of trying to help, just as different people have different ways of learning. The way I see it, brushing people off is a really, really (really) good way of making sure they won't bother us again and make us "waste our time" trying to figure out a way to help.


    On the other hand, some people's idea of giving help might include an example:

        public static void main (String [] args)
        {
            int units;
            double money;
     
            money = 16.65;
            units = (int)Math.round(money * 100.0);
            System.out.printf("money*100.0 = %.13f, (int)Math.round(money * 100.0) = %d\n",
                    money*100.0, units);
        }
    Output:

    money*100.0 = 1664.9999999999998, (int)Math.round(money * 100.0) = 1665


    Heck! We might even give them a reference for further amusement and amazement: Java Math Library

    At least one member here has posted a link (numerous times) to a fine narrative explanation of floating point number representation and its implications for Java programmers. Maybe that person can chime in with that excellent link. (I don't have it at the tip of my fingers just now, but here's a fairly complete description at Wikepedia.)

    Now, I am new to Java and new to this forum, so maybe I shouldn't be so "preachy," but sometimes I just can't help myself.


    Cheers!

    Z
    Last edited by Zaphod_b; August 29th, 2012 at 08:09 PM.

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    Well if the OP didn't cross-post in the first place we wouldn't be having this discussion.

    The post at the other forum had sufficient information for the OP to do research himself, which would benefit his learning to a greater extent.
    You giving him the code and a pat on the back is just going to make him bash the code in, find that it works, and think no more about it. In my eyes, this isn't a very beneficial approach.
    Of-course my point doesn't have such a great impact for such a small problem, but the principle remains.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. The Following User Says Thank You to newbie For This Useful Post:

    curmudgeon (August 29th, 2012)

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    Even if we don't think it's worth our while, I don't see any reason to be churlish about it.
    Not churlish, I think, just blunt. The "direct, cut-through-the-bullshit communications style" that esr talks about.

    When responding to a cross poster I will include the links - because the original poster should have. If I think the question has been answered, I'll say that to save people wasting their time. Of course an answer that satisfies me and an answer that satisfies the OP may not be the same thing! But it's up to the OP to seek clarification as part of the ongoing dialogue they have started.

    Personally I'll think twice (and a third time) before contributing anything to a discussion that's been cross posted. It's confusing at best and suggests to me (rightly or wrongly) a poster who isn't prepared to put as much into a thread as all the other respondents combined. That's a tall order, and I can't enforce my expectation: all I can do alert others to the cross post and walk away. Others need not feel this way, but a cross poster ought to be aware that many will.

    -----

    Maybe the floating point link is this one: What Every Computer Scientist Should Know About Floating-Point Arithmetic

    It's often cited and very good. But, I feel, maybe too mathematical. Often what is needed is some appreciation in general terms about how a floating point quantity differs from the infinitely precise numerical values of mathematics. And, as in this case, about what (int) does and the library methods available (as in #4).

  8. The Following 3 Users Say Thank You to pbrockway2 For This Useful Post:

    curmudgeon (August 29th, 2012), Norm (August 29th, 2012), Tjstretch (August 29th, 2012)

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    Another point of view: anyone researching their own problem and finding a thread on his topic would like to be able to read what others had posted on another forum. By providing a link to the other forum, that OP can go see what else was said.
    Also, as you (Z) point out, perhaps the answers on the other forum weren't very good, then you can add enlightenment on this forum. If the forums are cross-linked, then anyone researching a problem can get lots more answers.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    I appreciate the reasoned, articulate, and civil responses.

    I would like to apologize for the following transgressions (no excuses):
    1. By including editorial material in the form of a challenge I provoked responses. My challenge did nothing to add to the value of this thread, and in fact distracted from any useful information that might have been offered. If I felt the need to express my concerns, I think it would have been more proper to open my own thread in the Cafe section of the forum and invite discussion there.

    2. The information that I proffered answered a question that was not asked.

      If the Original Post had asked "how do I get the right answer," I would not have given complete example code, but would have showed the link to the Math class and mentioned that the round() function could be used. Sometimes I think value can be added by expanding (a little) beyond the question. That may or may not be appropriate, but if I felt the "need" to expand beyond the limits of the original question, it still would have been more proper to stick to the "no spoon-feeding" protocol that is one of the very good features of this forum.


    I come here not only to try to learn some Java, but also to try to learn how to help others. I'll try to do better.

    Bottom line:

    "I never learned from a man who agreed with me."
    ---Robert A. Heinlein

    Cheers!

    Z

  11. The Following 2 Users Say Thank You to Zaphod_b For This Useful Post:

    curmudgeon (August 30th, 2012), pbrockway2 (August 30th, 2012)

  12. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    Z, you're doing great. I wish I was as articulate.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

    For those interested in the discussion within the crosspost, see
    double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?

    And some reading for those unfamiliar:
    The problems with cross-posting

Similar Threads

  1. NullPointerException Error in Code that Prints Array Elements
    By Atypically Engineered in forum Collections and Generics
    Replies: 8
    Last Post: April 6th, 2012, 11:21 PM
  2. Something wrong with really,really simple code.
    By Programmer142 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 5th, 2011, 08:01 AM
  3. Help with code that prints number of evens, odds, and 0's
    By trogan234 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2011, 07:50 PM
  4. my code displaying some minor wrong o/p ....plz suggest me an answer !
    By naved in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 12:59 PM
  5. nothing prints after runing code
    By darego in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2011, 11:14 AM