473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

double arithmetic question...why?

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

Jul 5 '07 #1
4 1356
Joe
http://blogs.msdn.com/lucabol/archiv...t-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.

<ki*******@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
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

Jul 5 '07 #2

<ki*******@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
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.
Jul 5 '07 #3
On Thu, 05 Jul 2007 13:09:09 -0700, <ki*******@gmail.comwrote:
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
Jul 5 '07 #4
On Thu, 05 Jul 2007 14:23:42 -0700, Michael A. Covington
<lo**@ai.uga.edu.for.addresswrote:
>int price = 57;
double taxedPrice1 = price + (price * .18);

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
Jul 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Christian Christmann | last post by:
Hi, just a general question. Which data type is more precise for large values: float or double? Thank you. Chris
7
by: ma740988 | last post by:
I've got an unpacker that unpacks a 32 bit word into 3-10 bits samples. Bits 0 and 1 are dont cares. For the purposes of perfoming an FFT and an inverse FFT, I cast the 10 bit values into doubles....
6
by: Jesper, DK. | last post by:
A little preamble A year ago I had a very odd bug in my program, in an if clause it picked and executed the else statement even though the if clause should evaluate to true. Normally you know that...
8
by: abdul_n_khan | last post by:
Hello, I have a basic question related to datatypes. I am trying to read a value using Microsoft's ADO recordset from a field (lets call it 'Price') with datatype decimal(19,6) => 19 = Precision,...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
6
by: Alexander Stoyakin | last post by:
Hello, please advise on the following issue. I need to check that difference between two double values is not higher than defined limit. int main() { double limit = 0.3; double val1 = 0.5,...
9
by: Tobias | last post by:
Let: double x,y; double z = max(x,y); Does the standard ensure that ( z == 0.0 ) || ( x/z == 1.0 ) || ( y/z == 1.0 ) always gives true?
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
2
by: Markus Dehmann | last post by:
I have two integers i1 and i2, the second of which is guaranteed to be between 0 and 99, and I encode them into one double: double encoded = (double)i1 + (double)i2 / (double)100; So, for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.