posted 4 years ago

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

posted 4 years ago

Is it the

The only bit that I disagree with is that you have to find the

a/b + c/d = ((a*d) + (b*c)) / (b*d).

and for subtraction you simply change the signs.

HIH

Winston

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Allan Pui

Greenhorn

Posts: 5

posted 4 years ago

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.

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

Allan Pui

Greenhorn

Posts: 5

Ulf Dittmer

Rancher

Posts: 42975

76

posted 4 years ago

- 2

The reason you are getting 4 is that you are subtracting the results of integer division. In integer division

BigDecimal is awkward to use until you have a little practice: start reading here (read the whole thread) and maybe the other links there.

`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.

posted 4 years ago

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

See if you can use that to correct your expression.

HIH

Winston

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

Articles by Winston can be found here

posted 4 years ago

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

posted 4 years ago

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.

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.