double arithmetic question...why?

  • Thread starter Thread starter kilik3000
  • Start date Start date
K

kilik3000

This may be a dumb question but...

When running the following code:

//...

int price = 57;
double taxedPrice1 = price + (price * .18);
double taxedPrice2 = price * 1.18;

//...

Why does taxedPrice1 == "67.26" while taxedPrice2 ==
"67.259999999999991" ?

-Thx
 
http://blogs.msdn.com/lucabol/archi...e-should-i-use-in-c-to-represent-numbers.aspx

If you need fractions:

a.. Use decimal when intermediate results need to be rounded to fixed
precision - this is almost always limited to calculations involving money.
b.. Otherwise use double - you will get the rounding of your calculations
wrong, but the extra precision of double will ensure that your results will
be good enough.
c.. Only use float if you know you have a space issue, and you know the
precision implications. If you don't have a PhD in numeric computation you
don't qualify.
 
This may be a dumb question but...

When running the following code:

//...

int price = 57;
double taxedPrice1 = price + (price * .18);

I believe the sub-expression (price * .18) is single-precision float, not
double-precision.

In my opinion the name "double" for this type is infelicitous because the
term "double precision" (from Fortran) has long since been forgotten. I
wonder what young Java and C# programmers think "double" is supposed to
mean.
 
int price = 57;
double taxedPrice1 = price + (price * .18);
double taxedPrice2 = price * 1.18;

//...

Why does taxedPrice1 == "67.26" while taxedPrice2 ==
"67.259999999999991" ?

In addition to what Joe wrote, see Jon Skeet's discussion on floating
point:

http://www.pobox.com/~skeet/csharp/floatingpoint.html

The basic issue is that the decimal values being used can't be represented
exactly. In fact, the variable taxedPrice1 isn't really stored as
67.26...it just happens that it differs just enough from the value stored
in taxedPrice2 that the debugger displays the value rounded.

If you look at the binary values in the debugger, you'll find they differ
by just a single bit, the least-significant one. That difference is
caused by the subtle difference in precision caused by using the constant
0.18 in one calculation and the constant 1.18 in the other. But neither
result is actually represented internally as exactly 67.26. You should
use the decimal type if that's what you want.

Pete
 
I believe the sub-expression (price * .18) is single-precision float, not
double-precision.

Nope. In fact, you can cast everything to doubles, and make all the
variables double, and you get the same result.

I am pretty sure that unless you specify the "f" at the end of a floating
point constant, it's a double, and of course the result of the expression
of an int times a double should be a double.

Pete
 

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

Back
Top