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 );

}}

Printable View

- 08-30-2012, 12:44 AMjayjay5double 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 );

}} - 08-30-2012, 01:07 AMFubarableRe: 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. - 08-30-2012, 04:29 PMjayjay5Re: 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?

- - 08-30-2012, 04:51 PMj2me64Re: 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. - 08-30-2012, 05:51 PMTollsRe: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
- 08-30-2012, 11:08 PMjayjay5Re: 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.

- 08-30-2012, 11:26 PMdoWhileRe: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
- 08-31-2012, 01:03 AMFubarableRe: 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 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.

- 08-31-2012, 10:34 AMTollsRe: 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. - 08-31-2012, 11:12 AMj2me64Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
From the book "Effective Java", Second Edition, J. Bloch:

Quote:

Item 48: Avoid float and double if exact answers are required

- 09-13-2012, 10:23 PMjayjay5Re: 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.

- 09-14-2012, 12:21 AMDarrylBurkeRe: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
- 09-14-2012, 03:57 AMFubarableRe: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
- 09-14-2012, 10:42 AMTollsRe: 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... - 09-14-2012, 06:08 PMjayjay5Re: 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.

- 09-14-2012, 09:48 PMJosAHRe: 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 - 09-15-2012, 01:22 AMjayjay5Re: 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?

- 09-15-2012, 04:50 AMFubarableRe: 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.