aspose file tools*
The moose likes Beginning Java and the fly likes Re: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Re: "Double" Question" Watch "Re: "Double" Question" New topic
Author

Re: "Double" Question

Joe Cheung
Ranch Hand

Joined: Oct 18, 2002
Posts: 104
Dear all,

I used the following program to calculate the sum of 2 Double value:

public class test{
public static void main(String[] e){
double a = 0;
Double b = new Double(3.3);
Double c = new Double(1.3);
a += b.doubleValue();
a += c.doubleValue();
System.out.println(a);
}
}

It will print "4.6 as the result. but if I change the value of variable "c" from 1.3 to 9.3, it will print 12.600000000000001???

What's wrong with the program and how can I rectify it such that it will print "12.6"?

Thanks!

Regards,
Joe


Joe
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Well I don't know what the exact reason for this, but just heard that it is some thing to do with the platform you are running java. But if you want to get rid of this, you can use round(). Hope this helps


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
its a recurrent theme
see this and also this recent thread


java amateur
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
BTW, does anyone know the algorithm to divide in base 2, 1 and 3 (1 / 3)?
[ April 25, 2005: Message edited by: miguel lisboa ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Plain old long division?


Is that even right?

Look at the constructors for Integer ... see if an easy way of making the library do all the work jumps out at you. If you have to do your own, Google on "binary arithmetic" for sites like this: http://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/BinaryMath.html
[ April 25, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks, Stan

i got that you drop zeros till you can subtract '11' from '100'... but the it gets slippery
i didnt quite well got how you fullfilled upper row...do you add last two digits with result or else ...?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815

So, do they teach long division in schools anymore?

The upper row is filled with the anwer to "how many times does the divisor go into the dividend?"

Simple (base 10) example: divide 1421 by 7



7 goes into 1 zero times, so we include the next digit. (We would write a zero in the top line, except that by convention we do not write leading zeros.) Now, 7 goes into 14 once, so we write a 2 above the rightmost digit that we used in the dividend (the 4). Now, we multiply the 2 by the 7 to get 14 and write that below the 1 4 in the dividend. Subtract 14 from 14 and we get a result of 0. Drop the 2 from the dividend down and get 02. 7 goes into 2 zero times, so we we write a zero in the answer (and multiply 0 by 7 to get 0, subtract that from the 2 to get 2) and drop down the 1 to make 21. (Note that the right equation does not physically multiply by zero; it justs writes the zero and drops down the 1). Now, 7 goes into 21 3 times, so we write a 3 and do the same thing. Multiply 3 by 7 to get 21 and write that and subtract, resulting in zero. Now we've used up all numbers, so we check our decimal points (they're in the right places) and return the answer: 203

Stan's did just the same thing, except he used binary, not decimal.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
that's how we do it here: 35 / 40 = 0,875


i cant format it but sure can calculate it
[ April 25, 2005: Message edited by: miguel lisboa ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Wow, being a cultural bigot it never occurred to me there were other notations. Neat, thanks! BTW: Where is "here" for you?

Oh, and I dug up a computer architecture book with the algorithm for dividing in binary. It's multiplying with two's compliments and takes about three pages to describe in full. I won't be quoting it here because I only understood the first paragraph.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Stan James:
BTW: Where is "here" for you?
Miguel's examples have had a lot of Portuguese variable names, but Portugal doesn't seem to exist on the internet*, so I'm going to guess Brazil.

I was surprised to see a different notation as well. I've always considered math to be universal in its notation, but it's good to have assumptions demonstrated to be wrong every so often.

* Yes, that was sarcastic, but I've conversed with people from all over the world and yet not one has been from Portugal. Very strange.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
I'm from Lisboa (Lisbon), Portugal

you can imagine how i felt (like Champollion) when i first looked at it!

my first thought was something along this: these binary guys sure are left handed
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by miguel lisboa:
I'm from Lisboa (Lisbon), Portugal
Boy am I glad I just cleaned my feet! mmmmm mmmm good!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Re: "Double" Question
 
Similar Threads
Parameter dependent Method call
Error in calculation
Getting same numbers
casting of double to float
A Quadrilateral extend program