• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fraction  RSS feed

 
Allan Pui
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there someone who could show me how to sum up two numbers of fractions?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. You mean like one of these classes do it: java fraction class ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allan Pui wrote:Is there someone who could show me how to sum up two numbers of fractions?

Is it the forumlae that you want? If so, this page might help.

The only bit that I disagree with is that you have to find the lowest common denominator in order to do addition/subtraction, you simply need to find A common one. That said, their calculation is exactly how I would do it:
a/b + c/d = ((a*d) + (b*c)) / (b*d).
and for subtraction you simply change the signs.

HIH

Winston
 
Allan Pui
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying. What i meant is, i try to calculate either sum or subtraction of fractions, but it turned out to be nothing but 4 when i put the code into spmething like this:

System.out.println(4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11));
So, i put each number into double variable, like double a = 3 , double b= 5 and so on. Then, i put the variable into it. Yup, it works.
Unfortunately, it's stupid in my opinion.
Is there any correct and more precise method to calculate these fractions.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If floating point accuracy matters then you may be better served by using the BigDecimal class. It avoids the roundoff errors and numerical limits inherent in using float and double.
 
Allan Pui
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any source for me to refer? I dont want to stuck at here for ages or i grow old.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BigDecimal (<-- this is a link to its javadocs) is part of the JRE class libraries, so you don't need anything else to use it.
 
Allan Pui
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Marshall.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you are getting 4 is that you are subtracting the results of integer division. In integer division 1 / 2 gives 0.
BigDecimal is awkward to use until you have a little practice: start reading here (read the whole thread) and maybe the other links there.
 
Allan Pui
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give it a shot. thanks
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The reason you are getting 4 is that you are subtracting the results of integer division.

@Allan: And the reason for that is that you're using integer literals.

4 is an integer (in fact, an int).
4.0 is a double.

See if you can use that to correct your expression.

HIH

Winston
 
Alfie Noakes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure on this, but it seems to me from my experience that with fractions, you need at least one of the values to be a double for a non-rounded answer. So if you are dividing by just a number, and not a variable, you should treat it as a double by adding a decimal place.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens is that you are causing the values to be cast to a double. You can then assign that result to a double variable. You may need the double first in the expression; if you calculate 2 / 3 + 4 / 5.0, the cast does not take effect until after you pass the + sign.
The rules for integer division is that it always returns a whole number, and the rounding convention is round towards 0. It has been like that as long as I can remember, and probably since computers were invented.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!