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

• August 29th, 2012, 06:24 PM
jayjay5
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 );

}}
• August 29th, 2012, 06:31 PM
curmudgeon
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
• August 29th, 2012, 06:36 PM
newbie
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
For anyone who can relate to this issue, see the answer here: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?

As for jayjay, although cross-posting is allowed, you should always make us aware that you have done so, as to not waste anyone's time.
• August 29th, 2012, 07:31 PM
Zaphod_b
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
Quote:

Quote:

You know, just because the question was posted on another forum and received a response doesn't mean we can't have something to offer. Even if we don't think it's worth our while, I don't see any reason to be churlish about it.

Furthermore, regardless of the Original Poster's point of view, I come here to learn, and so do lots of other people. There are literally hundreds of people who come to javaparogrammingforums.com each and every day to pick up hints and kinks and insight about problem solving with Java. Instead of giving them links to other places, maybe we can give other places reasons to give links to us.

I mean, after pointing out the reason for the "wrong" answer (inexact representation of decimal fractional numbers as internal binary floating point numbers) maybe someone could point out there is a static Math function that rounds a floating point number to the nearest integral value.

The result is still a floating point number, but its fractional part is equal to zero. Then, when you convert that rounded value to an int you get the "right" answer.

I mean, different people have different ways of trying to help, just as different people have different ways of learning. The way I see it, brushing people off is a really, really (really) good way of making sure they won't bother us again and make us "waste our time" trying to figure out a way to help.

On the other hand, some people's idea of giving help might include an example:

Code java:

``` public static void main (String [] args) { int units; double money;   money = 16.65; units = (int)Math.round(money * 100.0); System.out.printf("money*100.0 = %.13f, (int)Math.round(money * 100.0) = %d\n", money*100.0, units); }```
Output:

money*100.0 = 1664.9999999999998, (int)Math.round(money * 100.0) = 1665

Heck! We might even give them a reference for further amusement and amazement: Java Math Library

At least one member here has posted a link (numerous times) to a fine narrative explanation of floating point number representation and its implications for Java programmers. Maybe that person can chime in with that excellent link. (I don't have it at the tip of my fingers just now, but here's a fairly complete description at Wikepedia.)

Now, I am new to Java and new to this forum, so maybe I shouldn't be so "preachy," but sometimes I just can't help myself.

Cheers!

Z
• August 29th, 2012, 08:42 PM
newbie
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
Well if the OP didn't cross-post in the first place we wouldn't be having this discussion.

The post at the other forum had sufficient information for the OP to do research himself, which would benefit his learning to a greater extent.
You giving him the code and a pat on the back is just going to make him bash the code in, find that it works, and think no more about it. In my eyes, this isn't a very beneficial approach.
Of-course my point doesn't have such a great impact for such a small problem, but the principle remains.
• August 29th, 2012, 09:28 PM
pbrockway2
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
Quote:

Even if we don't think it's worth our while, I don't see any reason to be churlish about it.
Not churlish, I think, just blunt. The "direct, cut-through-the-bullshit communications style" that esr talks about.

When responding to a cross poster I will include the links - because the original poster should have. If I think the question has been answered, I'll say that to save people wasting their time. Of course an answer that satisfies me and an answer that satisfies the OP may not be the same thing! But it's up to the OP to seek clarification as part of the ongoing dialogue they have started.

Personally I'll think twice (and a third time) before contributing anything to a discussion that's been cross posted. It's confusing at best and suggests to me (rightly or wrongly) a poster who isn't prepared to put as much into a thread as all the other respondents combined. That's a tall order, and I can't enforce my expectation: all I can do alert others to the cross post and walk away. Others need not feel this way, but a cross poster ought to be aware that many will.

-----

Maybe the floating point link is this one: What Every Computer Scientist Should Know About Floating-Point Arithmetic

It's often cited and very good. But, I feel, maybe too mathematical. Often what is needed is some appreciation in general terms about how a floating point quantity differs from the infinitely precise numerical values of mathematics. And, as in this case, about what (int) does and the library methods available (as in #4).
• August 29th, 2012, 11:25 PM
Norm
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
Another point of view: anyone researching their own problem and finding a thread on his topic would like to be able to read what others had posted on another forum. By providing a link to the other forum, that OP can go see what else was said.
Also, as you (Z) point out, perhaps the answers on the other forum weren't very good, then you can add enlightenment on this forum. If the forums are cross-linked, then anyone researching a problem can get lots more answers.
• August 30th, 2012, 10:24 AM
Zaphod_b
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
I appreciate the reasoned, articulate, and civil responses.

I would like to apologize for the following transgressions (no excuses):
1. By including editorial material in the form of a challenge I provoked responses. My challenge did nothing to add to the value of this thread, and in fact distracted from any useful information that might have been offered. If I felt the need to express my concerns, I think it would have been more proper to open my own thread in the Cafe section of the forum and invite discussion there.

2. The information that I proffered answered a question that was not asked.

If the Original Post had asked "how do I get the right answer," I would not have given complete example code, but would have showed the link to the Math class and mentioned that the round() function could be used. Sometimes I think value can be added by expanding (a little) beyond the question. That may or may not be appropriate, but if I felt the "need" to expand beyond the limits of the original question, it still would have been more proper to stick to the "no spoon-feeding" protocol that is one of the very good features of this forum.

I come here not only to try to learn some Java, but also to try to learn how to help others. I'll try to do better.

Bottom line:

"I never learned from a man who agreed with me."
---Robert A. Heinlein

Cheers!

Z
• August 30th, 2012, 10:32 AM
Norm
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
Z, you're doing great. I wish I was as articulate.
• August 30th, 2012, 04:25 PM
copeg
Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -
For those interested in the discussion within the crosspost, see
double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?

And some reading for those unfamiliar:
http://www.javaprogrammingforums.com...s-posting.html