Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Re: "Double" Question

 
Joe Cheung
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its a recurrent theme
see this and also this recent thread
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by miguel lisboa:
I'm from Lisboa (Lisbon), Portugal
Boy am I glad I just cleaned my feet! mmmmm mmmm good!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic