473,408 Members | 1,753 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,408 software developers and data experts.

Problem with casting a double to int

Hi every c++ expert, today i've seen a strange case when i was debugging.
When I cast a double to an int, the value is altered but it doesn't look like neither round up nor round down.

Here is the code:

double temp = (double )(pQinQplOdds[iOddsCount-1]*100); //temp = 820.00000
int odds = static_cast<int>(temp);

odds should get 820 but it doesn't. It turns out getting a value of 819 !!

Anyone could tell me why is it?

P.S. It fixed after i use type float instead of double.

Thanks x 1000
Jul 11 '07 #1
3 2103
Meetee
931 Expert Mod 512MB
Hi every c++ expert, today i've seen a strange case when i was debugging.
When I cast a double to an int, the value is altered but it doesn't look like neither round up nor round down.

Here is the code:

double temp = (double )(pQinQplOdds[iOddsCount-1]*100); //temp = 820.00000
int odds = static_cast<int>(temp);

odds should get 820 but it doesn't. It turns out getting a value of 819 !!

Anyone could tell me why is it?

P.S. It fixed after i use type float instead of double.

Thanks x 1000
Hi,

I did this way

Expand|Select|Wrap|Line Numbers
  1. double d = 820.00000;
  2. int i = static_cast<int>(d);
  3.  
and it gives i = 820. So I think (double )(pQinQplOdds[iOddsCount-1]*100) here is some problem? What are the values of pQinQplOdds and iOddsCount?

Regards
Jul 11 '07 #2
Thanks for replying, i believe that the problem isn't in
(double )(pQinQplOdds[iOddsCount-1]*100).

And pQinQplOdds is an array of double and

(pQinQplOdds[iOddsCount-1]*100) == 820.00000000 is true.

I believe that the problem is in the casting part.
Jul 11 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
What type is pQinQplOdds?

If it's a float, then it has only 6 significant digits. When the division is cast to a double, the float is converted to double, the 100 is converted to double, the calcaulation is done as a double and the result is a double.

A double has 15 significant digits. Only 6 have any meaning. The other digits are bogus. Now you cast and chop off the decimals.

You can't say (pQinQplOdds[iOddsCount-1]*100) == 820.00000000 is true.
If may test as true but you can't use the == operator with floating point. Actually, you can't use any of the operators like <,> <=, >=, !=, etc with floating point.

Floating point numbers only close in value may test equal. You need to do the compare based on absolute value by subtracting one float from the other an testing the ressult be within a specified sigma error tolerance.

Google floating point arithmetic.

As a general practice:
1) never mix floating point an integers.
2) avoid floating point at all costs. It's very slow. It's inaccurate. It has automatic rounding that can't be turned off.
3) if you have to use floating point, use only double. Do not use float unless you can write down on paper the precise technical reason you need a float.

Finally, do not cast in C++.
Jul 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
1
by: Kent Lewandowski | last post by:
Hi, I saw this post from 3 years ago regarding converting DOUBLE values to CHAR in ibm DB2. Now I'm having the same problem. No real solution was posted to that old thread. Has anyone got...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
14
by: Jason Sewall | last post by:
Hello, I'm using the intel C++ 8.0 compiler on windows and I'm working on some software that uses the following lines of code: gf->invdim = 1.0/(double)dim; printf("invdim: %f\n",...
5
by: Francois Malgreve | last post by:
in the following code: object obj = this.ViewState; string s = obj.GetType().ToString(); currentCreditLimit = (double) this.ViewState; ViewState is a StateBag object (dictionary type object)...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
11
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.