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 );
}}
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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 );
}}
Answered already in cross-post in another forum.
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.
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
Answered already...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....see the answer here...
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:
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
Last edited by Zaphod_b; August 29th, 2012 at 07:09 PM.
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.
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
curmudgeon (August 29th, 2012)
Not churlish, I think, just blunt. The "direct, cut-through-the-bullshit communications style" that esr talks about.Even if we don't think it's worth our while, I don't see any reason to be churlish about it.
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).
curmudgeon (August 29th, 2012), Norm (August 29th, 2012), Tjstretch (August 29th, 2012)
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.
If you don't understand my answer, don't ignore it, ask a question.
I appreciate the reasoned, articulate, and civil responses.
I would like to apologize for the following transgressions (no excuses):
- 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.
- 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
curmudgeon (August 30th, 2012), pbrockway2 (August 30th, 2012)
Z, you're doing great. I wish I was as articulate.
If you don't understand my answer, don't ignore it, ask a question.
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