Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By JosAH

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

  1. #1
    jayjay5 is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    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
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

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

    Moving to the new to Java section as this shouldn't be posted in a non-question section of the forums.

    Google and read up on the problem with using digital hardware to try to represent floating point numbers. The issue is that except for some defined numbers, floating point numbers are not represented fully accurately but rather only to a good approximation. So 16.65 will not be exact but will be somewhere close to this value, perhaps a little above or a little below. next, understand that when you cast a double to an int, you don't round but rather you truncate the number, so that 1664.9999999999 will become 1664.

  3. #3
    jayjay5 is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

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

    I hate to be obtuse but - - ‘Not represented accurately’ I don’t understand.
    - A little bit above or below --- why?
    -

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

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

    try

    System.out.println(1.03 - .42);

    According to "Effective Java" from J. Bloch the reason is "The float and double types are particularly illsuited for monetary calculations because it is impossible to represent 0.1 (or any
    other negative power of ten) as a float or double exactly.".

    The alternatives are "to keep track of the decimal point yourself" or to use the class BigDecimal and its relatated methods.
    Last edited by j2me64; 08-30-2012 at 09:25 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

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

    Quote Originally Posted by jayjay5 View Post
    I hate to be obtuse but - - ‘Not represented accurately’ I don’t understand.
    - A little bit above or below --- why?
    -
    Try and represent 1/3 as a decimal and then get back to me.
    Please do not ask for code as refusal often offends.

  6. #6
    jayjay5 is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

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

    I am not referring to a recurring decimal. The numbers is 16.65; that is to say 16 dollars and 65 cents. I want to convert this to pennies. Multiply by 100 and cast to an int. JAVA has an obvious malfunction.

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

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


  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

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

    Quote Originally Posted by jayjay5 View Post
    I am not referring to a recurring decimal. The numbers is 16.65; that is to say 16 dollars and 65 cents. I want to convert this to pennies. Multiply by 100 and cast to an int. JAVA has an obvious malfunction.
    Regarding "Java has an obvious malfunction", um, no, the malfunction is still in your understanding, and you still need to understand the problems with representing floating point numbers on a digital computer. This is a problem shared by all programming languages, because it has nothing to do with the language itself but rather is a limitation in the hardware. Please read the links given to you in your cross-posts. And don't feel bad or put down as this is a concept that all of us must struggle to fully understand at one time or another, and this is just your time.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

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

    Quote Originally Posted by jayjay5 View Post
    I am not referring to a recurring decimal. The numbers is 16.65; that is to say 16 dollars and 65 cents. I want to convert this to pennies. Multiply by 100 and cast to an int. JAVA has an obvious malfunction.
    I don't think you understood me.
    Can you exactly represent 1/3 as a decimal?

    The answer to that is exactly the same as the answer to why you can't represent 16.65 as a binary. Which applies to all floating point numbers in any language.

    Of course, failing that, there's always Goldberg.
    Please do not ask for code as refusal often offends.

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

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

    From the book "Effective Java", Second Edition, J. Bloch:

    Item 48: Avoid float and double if exact answers are required
    Study the class BigDecimal!

  11. #11
    jayjay5 is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

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

    I hava a pocket calculator the has the amazing ability to multiply 16.65 by 100 and produce the correct answer.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

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

    Quote Originally Posted by jayjay5 View Post
    I hava a pocket calculator the has the amazing ability to multiply 16.65 by 100 and produce the correct answer.
    Congratulations. Do you have any more worthless gems to share?

    Please take your trolling elsewhere.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

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

    Quote Originally Posted by jayjay5 View Post
    I hava a pocket calculator the has the amazing ability to multiply 16.65 by 100 and produce the correct answer.
    All this proves is that you've read none of the links provided here and in your cross-posts. You might consider reading or re-reading them before demonstrating any more ignorance.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

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

    Quote Originally Posted by jayjay5 View Post
    I hava a pocket calculator the has the amazing ability to multiply 16.65 by 100 and produce the correct answer.
    Calculator hardware != computer hardware.
    See here.

    Though I suspect you won't bother reading that one either...
    Please do not ask for code as refusal often offends.

  15. #15
    jayjay5 is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

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

    The solution is rather simple: consider (1/3)*3; this would produce .999999…. ; if you add/subtract fuzz; .0000000001 (the smallest binary number possible) ; Floating Point : Java Glossary ; If the binary representation changes dramatically; accept the number fuzzed.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

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

    A lot of calculators use BCD math (Binary Coded Decimal) so they can indeed perform the calculation 16.65*100 == 1665 exactly; they still have problems representing 1/3 in an exact decimal expansion though (it can't be done). The IEEE754 number system is like a finite bunch of cards, marked with the fractions 1/2, 1/4, 1/8, 1/16 etc. one card for each fraction. Given a number you can pick any cards you like as long as the sum of those fractions comes as close to that target number as possible. For the number 1/10 you can't find a combination of cards that total 1/10 exactly.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    jayjay5 is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

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

    It is not a question of understanding - it is a question of acceptance. I have written graphics programs (non JAVA) in which graphic images contract and expand numerous times (it’s a matrix algebra thing). Yes, in this case there must be a lower limit to which an image can be reduced and latter accurately expanded. The consequence is accuracy drift over thousands of iterations. Yes – I do understand binary repeating numbers. What about fuzz – the concept has been around for a long time. Do I accept that JAVA cannot convert 16 dollars and 65 cents to pennies?

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

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

    Quote Originally Posted by jayjay5 View Post
    It is not a question of understanding - it is a question of acceptance. I have written graphics programs (non JAVA) in which graphic images contract and expand numerous times (it’s a matrix algebra thing). Yes, in this case there must be a lower limit to which an image can be reduced and latter accurately expanded. The consequence is accuracy drift over thousands of iterations. Yes – I do understand binary repeating numbers. What about fuzz – the concept has been around for a long time. Do I accept that JAVA cannot convert 16 dollars and 65 cents to pennies?
    No, you would never use floating point variables to represent financial concepts in *any* programming language. This is what Java has the BigDecimal type. There is extra overhead required for using BigDecimal in terms of memory and speed, but for critical financial calculations, it is worth this price.

Similar Threads

  1. Optimising this simple piece of code (30 lines)
    By hvince95 in forum New To Java
    Replies: 3
    Last Post: 01-29-2012, 08:43 AM
  2. What is wrong with this simple code?
    By venandane in forum Java Applets
    Replies: 4
    Last Post: 11-06-2011, 09:55 AM
  3. Should be simple answer...
    By sleepingpeace in forum New To Java
    Replies: 4
    Last Post: 02-17-2011, 01:01 PM
  4. short [] <-> double[] conversion query
    By KittenFace in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 07:30 PM
  5. What's wrong with my (simple) code
    By kracer in forum New To Java
    Replies: 13
    Last Post: 05-01-2010, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •