Results 1 to 18 of 18
 08292012, 11:44 PM #1Member
 Join Date
 Aug 2012
 Posts
 6
 Rep Power
 0

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 nonquestion 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.
 08302012, 03:29 PM #3Member
 Join Date
 Aug 2012
 Posts
 6
 Rep Power
 0
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?

 08302012, 03:51 PM #4
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; 08302012 at 09:25 PM.
 08302012, 04:51 PM #5Moderator
 Join Date
 Apr 2009
 Posts
 12,519
 Rep Power
 21
 08302012, 10:08 PM #6Member
 Join Date
 Aug 2012
 Posts
 6
 Rep Power
 0
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.
 08302012, 10:26 PM #7Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,641
 Rep Power
 8
Re: double to int conversion  3 simple lines of code  prints wrong answer 1664 

Re: double to int conversion  3 simple lines of code  prints wrong answer 1664 
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 crossposts. 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.
 08312012, 09:34 AM #9Moderator
 Join Date
 Apr 2009
 Posts
 12,519
 Rep Power
 21
Re: double to int conversion  3 simple lines of code  prints wrong answer 1664 
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.
** This space for rent **
 08312012, 10:12 AM #10
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
 09132012, 09:23 PM #11Member
 Join Date
 Aug 2012
 Posts
 6
 Rep Power
 0
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.
 09132012, 11:21 PM #12

Re: double to int conversion  3 simple lines of code  prints wrong answer 1664 
 09142012, 09:42 AM #14Moderator
 Join Date
 Apr 2009
 Posts
 12,519
 Rep Power
 21
Re: double to int conversion  3 simple lines of code  prints wrong answer 1664 
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.
** This space for rent **
 09142012, 05:08 PM #15Member
 Join Date
 Aug 2012
 Posts
 6
 Rep Power
 0
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.
 09142012, 08:48 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,126
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 09152012, 12:22 AM #17Member
 Join Date
 Aug 2012
 Posts
 6
 Rep Power
 0
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?

Re: double to int conversion  3 simple lines of code  prints wrong answer 1664 
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

Optimising this simple piece of code (30 lines)
By hvince95 in forum New To JavaReplies: 3Last Post: 01292012, 09:43 AM 
What is wrong with this simple code?
By venandane in forum Java AppletsReplies: 4Last Post: 11062011, 10:55 AM 
Should be simple answer...
By sleepingpeace in forum New To JavaReplies: 4Last Post: 02172011, 02:01 PM 
short [] <> double[] conversion query
By KittenFace in forum New To JavaReplies: 3Last Post: 12062010, 08:30 PM 
What's wrong with my (simple) code
By kracer in forum New To JavaReplies: 13Last Post: 05012010, 03:55 PM
Bookmarks