how does (23+40)/2 become 31?

H

Homer Simpson

Hi everyone,

I wrote a quick method to perform a calculation to determine the average of
two numbers. When 23 and 40 were used, the result was 31, not 31.5! I
discovered the problem, I was using long data types, not double. So, how
does c# and/or .NET round numbers? Does is just truncate the value? If
rounded, I think 31.5 should have been rounded to 32.

Thanks,
Scott
 
M

Marko Becirevic

I wrote a quick method to perform a calculation to determine the average
of
two numbers. When 23 and 40 were used, the result was 31, not 31.5! I
discovered the problem, I was using long data types, not double. So, how
does c# and/or .NET round numbers? Does is just truncate the value? If
rounded, I think 31.5 should have been rounded to 32.

(long) 23 + (long) 40 / (long) 2 = long

it always rounds to smaller number: 1.99 rounded is 1.

Cast first operand to double, do calculations, add 0.5 and then round:

(int) (((double) x + y) / 2 + 0.5)
 
F

Frisky

Marko,

In integer (longs) math, the fractional part is thrown away.

Math.Round() can round. But, see the posts in this newsgroup regarding
rounding. Math.Round() may not work the way you want it to. It follows IEEE
specifications.

If you want a fractional result, you must convert or use floating point
numbers (e.g. float, double, decimal) prior to multiplication or division.
Obviously addition and subtraction cannot introduce fractional parts.

When using constants like 23 and 40, you can append them with a letter to
tell the compiler their type. (f=float, d=double, m=decimal)

So, 23f is the float number 23.0, and 40d is the double 40.0.

Otherwise, as you suggest, you must cast (convert) to the appropriate
floating point representation prior to making the conversion.

Hope this helps...

Frisky
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top