Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Fraction

Greenhorn
Posts: 5
Is there someone who could show me how to sum up two numbers of fractions?

Rancher
Posts: 42975
76
Welcome to JavaRanch. You mean like one of these classes do it: java fraction class ?

Bartender
Posts: 10575
66

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
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: 42975
76
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
Any source for me to refer? I dont want to stuck at here for ages or i grow old.

Ulf Dittmer
Rancher
Posts: 42975
76
• 1
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
Thank you, Marshall.

Marshal
Posts: 58414
178
• 2
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
I'll give it a shot. thanks

Campbell Ritchie
Marshal
Posts: 58414
178
• 1
You're welcome

Winston Gutkowski
Bartender
Posts: 10575
66

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

Greenhorn
Posts: 24
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: 58414
178
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.