# Re: "Double" Question

Joe Cheung

Ranch Hand

Posts: 104

posted 10 years ago

- 0

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

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

Posts: 524

posted 10 years ago

- 0

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

Posts: 1281

miguel lisboa

Ranch Hand

Posts: 1281

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 10 years ago

- 0

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 ]

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

Posts: 1281

posted 10 years ago

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.

- 0

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

Posts: 1281

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 10 years ago
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

- 0

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.

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

Posts: 1646

posted 10 years ago

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.

- 0

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.Originally posted by Stan James:

BTW: Where is "here" for you?

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

Posts: 1281

I agree. Here's the link: http://aspose.com/file-tools |