Tyler Pittson
Tyler Pittson

Reputation: 43

Math Calculation not Correct

Can Somebody please tell me what is wrong. when I type the equations for low and high into a calculator I get low = 118.129 high = 113.629.
But for some reason both low and high are showing 119.0 when I run the code.

match_FRC = 82;//Double.parseDouble(FRC_match_textbox.getText().toString());
match_DTR = 1.455;//Double.parseDouble(DTR_match_textbox.getText().toString());

//math functions 
low = Math.round((match_FRC * match_DTR)/((1/100)+1));
high = Math.round((match_FRC * match_DTR)/((5/100)+1));

Upvotes: 1

Views: 207

Answers (3)

user207421
user207421

Reputation: 311048

Are you typing Math.round() into the calculator?

Math.round() delivers a long, with no fractional part. Your expectatation that low will contain a fractioanl part therefore has no basis.

Upvotes: 0

DominiqueWarthel
DominiqueWarthel

Reputation: 1

I is simple like a math!

5/100 (Integer) = 0
0 + 1 = 1
82*1.455 = 119.31
119.31 / 1 = 119.31
round ( 119.31 ) = 119

just change 100 to 100.0

low = Math.round((match_FRC * match_DTR)/((1/100.0)+1));
high = Math.round((match_FRC * match_DTR)/((5/100.0)+1));

Upvotes: 0

Alnitak
Alnitak

Reputation: 340045

You need to use a floating point constant instead of integers, e.g.:

low = Math.round((match_FRC * match_DTR)/((1.0 / 100)+1));

As it is, your ((1 / 100) + 1) is an integer expression that evaluates to exactly 1.

Making the 1 into 1.0 (or the 100 into 100.0) will cause promotion of the other operands (and the expression as a whole) into floating point.

Upvotes: 6

Related Questions