|
-
Oct 2nd, 2006, 10:26 AM
#1
Thread Starter
Fanatic Member
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
-
Oct 2nd, 2006, 04:36 PM
#2
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.
-
Oct 3rd, 2006, 01:21 AM
#3
Re: Possible Loss of Precision
 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
-
Oct 3rd, 2006, 03:52 AM
#4
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.
-
Oct 9th, 2006, 08:06 AM
#5
Lively Member
Re: Possible Loss of Precision
yes it is, considering you're manipulating an integer, you should know that the result will be an integer.
-
Oct 9th, 2006, 10:35 AM
#6
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.
-
Oct 9th, 2006, 10:41 AM
#7
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
-
Oct 9th, 2006, 09:42 PM
#8
PowerPoster
Re: Possible Loss of Precision
 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
-
Oct 9th, 2006, 11:22 PM
#9
Re: Possible Loss of Precision
 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
-
Oct 12th, 2006, 12:28 AM
#10
PowerPoster
Re: Possible Loss of Precision
 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
-
Oct 12th, 2006, 10:23 AM
#11
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
-
Oct 15th, 2006, 10:51 PM
#12
PowerPoster
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
-
Oct 15th, 2006, 11:48 PM
#13
Re: Possible Loss of Precision
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|