Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,399 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,159 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



warning conversion double to int loss of data

 
Reply to this topicStart new topic

warning conversion double to int loss of data, warning conversion double to int loss of data

rtokars
post 18 Jul, 2008 - 01:52 PM
Post #1


New D.I.C Head

*
Joined: 18 Jul, 2008
Posts: 8

I'm trying to clean up the warnings in my code. I have the following warning:
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
The concerning code follows:
CODE

int row, col;
int center = 511;
double sinval = sin(angle*pi/1800);
double cosval = cos(angle*pi/1800);
for (int hcounter = -511; hcounter <=511; hcounter++)
{
row = -hcounter*sinval + 511; //<------WARNING HERE
col = hcounter*cosval + center; //<------WARNING HERE
if ((row >= 0) && (row < height) && (col >= 0) && (col < width))
{
pixloc = row*width + col;
shade = frame[pixloc]*255/MAXPIXELVAL;
SetPixel (drawing_context,col+50,row+100, RGB(shade,shade,shade));
}
}

When i run my code i dont see anything unpredictable with the results (the int rounding from double is just fine). But I'd be interested in using some kind of conversion to eliminate the warning. Thx in advance!
User is offlineProfile CardPM

Go to the top of the page


Cerolobo
post 18 Jul, 2008 - 01:58 PM
Post #2


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 30 times
My Contributions


The compiler will automatically do the conversion for you, but you must cast the results to get rid of the warnings. To do this, you just have to do this

CODE
row = (int)(-hcounter*sinval + 511);
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 18 Jul, 2008 - 02:04 PM
Post #3


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 2,258



Thanked 44 times

Dream Kudos: 1450

Expert In: C, C++

My Contributions


If you're using C++ you could use static_cast too:
cpp

row = static_cast <int> (-hcounter*sinval + 511);

smile.gif
User is offlineProfile CardPM

Go to the top of the page

rtokars
post 18 Jul, 2008 - 02:05 PM
Post #4


New D.I.C Head

*
Joined: 18 Jul, 2008
Posts: 8

that's exactly what i needed.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/28/08 04:33PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month