Results 1 to 13 of 13
- 12-10-2011, 07:22 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
getting weird answer when multipling
when im multilpying using this code
a*.1
im keep get this answer
0.a000000000000001
i dont understand where its coming from, all i want is .a without all the other stuff, what is the easiest way of doing this and why is it happening?
a is an int, and im using the newest versoin of netbeans
any help is greatly apperciated
- 12-10-2011, 07:39 PM #2
Re: getting weird answer when multipling
Please post the full text of the statement(s) you are using that creates that output.
- 12-10-2011, 07:42 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
Re: getting weird answer when multipling
int tempDays = 30 - days[i];
else if (tempDays < 0){
String lateFee = Double.toString((tempDays*-1)*.1);
JLabel label7 = new JLabel ("$ " + lateFee);
this enough? its for a gui window
- 12-10-2011, 07:45 PM #4
Re: getting weird answer when multipling
Your posted code does not have a print statement. Where do you see the String that you posted?
0.a000000000000001
- 12-10-2011, 07:45 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
Re: getting weird answer when multipling
It's just the nature of how multiplication works on computer. Try using BigDecimal (I think that it). http://docs.oracle.com/javase/6/docs...malFormat.html
Last edited by sunde887; 12-10-2011 at 08:18 PM.
- 12-10-2011, 08:08 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
Re: getting weird answer when multipling
sorry for being confusing, this is the window that is printed
and i want it to display .7 instead of .700000000000001
this is the whole for loop that figures out how much each book is over due by if at all
for (int i=0; i <= (books.length-1); i++){
int tempDays = 30 - days[i];
if (tempDays >= 0){
String outDays = Integer.toString(tempDays);
JLabel label6 = new JLabel (outDays);
box1.add(label6);
box1.add(Box.createVerticalStrut(5));
JLabel label7 = new JLabel ("N/A");
box2.add(label7);
box2.add(Box.createVerticalStrut(5));
}
else if (tempDays < 0){
String outDays = Integer.toString(tempDays*-1);
JLabel label6 = new JLabel ("Over due by " + outDays + " day(s)");
box1.add(label6);
box1.add(Box.createVerticalStrut(5));
String lateFee = Double.toString((tempDays*-1)*.1);
JLabel label7 = new JLabel ("$ " + lateFee);
box2.add(label7);
box2.add(Box.createVerticalStrut(5));
}
}
would the easiest thing to do be subtract the 0.0000000000000001 or is there a ronding class?
- 12-10-2011, 08:10 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
Re: getting weird answer when multipling
See post # 5
- 12-10-2011, 08:13 PM #8
Re: getting weird answer when multipling
There are number format classes that will allow you to specify the number of decimal places.
Also the String class has a format method.
- 12-10-2011, 08:33 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
Re: getting weird answer when multipling
sweet it works now thanks for the help
- 12-10-2011, 08:35 PM #10
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: getting weird answer when multipling
That "1" at the end doesn't make any sense as repeating decimals would make all the digits after 10 digits the same as the first 10, which is "0".
- 12-10-2011, 11:29 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: getting weird answer when multipling
That "1" at the end doesn't make any sense
Not that I've ever read IEEE 754-1985, but the general principle is clear enough. The language only deals with floating point quantities using a finite amount of precision: there are, after all, infinitely many such quantities so that representing them all uniquely would require an infinite amount of memory/paper/screen space/time etc. The point is that, there being an infinite number of them, the symbols or bit patterns in memory for at least some of them would have to be arbitrarily large. So the language (and the computer) is obliged to work with imprecise surrogates which are "near enough".
(Integer values suffer from the same problem, but here the computer offers complete precision but at a cost of ignoring all the integer values past a certain point. Even with limited precision the floating point values must also be limited. The bottom line is you can only specify a finite number of different numbers.)
Floating point numbers are, in a sense, "fuzzy". You get a certain number of decimal places, and that's it. Where an actual real world quantity is being represented this way there are no grounds for thinking that the digits you don't see are zero, or seven, or anything else. They are simply not represented.
Although the relationship between Java floating point types (and those other languages) and real world quantities is that of "imprecise resemblance", the operations on them do have to be defined precisely (otherwise nobody would know what is going on). That is where the IEEE standard comes in and, as far as I know, Java adheres to it exactly.
Of course the IEEE folk may have cooked up a crazy standard! Their cleverness and the usefulness of the standard over time is only so much evidence in their favour. But before suggesting a better or more straight forward alternative it might pay to read the standard and consider the issues that it addresses.Last edited by pbrockway2; 12-10-2011 at 11:34 PM.
- 12-11-2011, 10:47 AM #12
Re: getting weird answer when multipling
And since nobody's posted it so far: What Every Computer Scientist Should Know About Floating-Point Arithmetic
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 12-11-2011, 02:04 PM #13
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Similar Threads
-
i want to float the answer
By vibaviattigala in forum New To JavaReplies: 2Last Post: 07-16-2011, 05:02 PM -
Compare true Answer /Player answer
By Eilime in forum New To JavaReplies: 1Last Post: 04-24-2011, 09:51 PM -
Could anyway answer me this?
By Jojomofoman in forum New To JavaReplies: 14Last Post: 12-17-2010, 12:14 AM -
i want my answer to a whole number or i think an int, please help?
By soc86 in forum New To JavaReplies: 3Last Post: 11-02-2008, 02:29 AM -
Plz answer this question ...
By raghu2114 in forum Advanced JavaReplies: 2Last Post: 09-19-2008, 07:36 PM
Bookmarks