Results 1 to 13 of 13

Thread: Possible Loss of Precision

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734

    Possible Loss of Precision

    Say you have:
    balance = balance + interestRate / 100.0 / 12.0 * balance;

    Where balance is an int, and interestRate is a double. This will give a "possible loss of precision error." However say you're given:
    balance += interestRate / 100.0 / 12.0 * balance;

    This will not give output any warnings... my question is just "Why?"


    "X-mas is 24.Desember you English morons.." - NoteMe

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Possible Loss of Precision

    Because the compiler is stupid?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Possible Loss of Precision

    Quote Originally Posted by CornedBee
    Because the compiler is stupid?
    Actually it's because the compiler is smart
    the "+=" forces an implicit cast to (int)
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Possible Loss of Precision

    That's exactly the reason why it should warn. Implicit casts from floating point to integral result in loss of precision that is not immediately obvious.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: Possible Loss of Precision

    yes it is, considering you're manipulating an integer, you should know that the result will be an integer.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Possible Loss of Precision

    But you'd have to look up the declaration of the variable to know that it is an integer. And the declaration of the other variable to know that it's a float.

    Besides, the warning should be on the implicit cast of the float, not the manipulation of the int.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Possible Loss of Precision

    CornedBee is right, this bug is being fixed in the 6th version of Java
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Possible Loss of Precision

    Quote Originally Posted by ComputerJy
    CornedBee is right, this bug is being fixed in the 6th version of Java
    Even we say that this is a bug, is it helpfull in some situations, isn't it? Three four times I used this in my codes.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Possible Loss of Precision

    Quote Originally Posted by eranga262154
    Even we say that this is a bug, is it helpfull in some situations, isn't it? Three four times I used this in my codes.
    Even if they fixed it, you still can do this. You will only have to use explicit casting
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Possible Loss of Precision

    Quote Originally Posted by ComputerJy
    Even if they fixed it, you still can do this. You will only have to use explicit casting

    It's true. Why did you say that "You will only have to use explicit casting"
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  11. #11
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Possible Loss of Precision

    Because the implicit casting is a bug that should be fixed
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  12. #12
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Possible Loss of Precision

    ComputerJy,

    Can you explain how it becomes a bug. I thought it is reality of java and so I used it time to time.

    Thanks.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  13. #13
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Possible Loss of Precision

    Quote Originally Posted by eranga262154
    ComputerJy,

    Can you explain how it becomes a bug. I thought it is reality of java and so I used it time to time.

    Thanks.
    Because there is no way a compiler should make any changes to your code without notifying you
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width