Prevent warning: assignment to 'int' from 'double'

O

Otto Wyss

I have the following statements

int page.x = page.x * ppiScr.x / 25.4;
int page.y = page.y * ppiScr.y / 25.4;

While VC++ is happy GCC complains the above warning. How can I get rid
of these warnings?

O. Wyss
 
K

K.S.

Otto said:
I have the following statements

int page.x = page.x * ppiScr.x / 25.4;
int page.y = page.y * ppiScr.y / 25.4;

While VC++ is happy GCC complains the above warning. How can I get rid
of these warnings?

O. Wyss

Your 25.4 is a double! So the compiler uses the / operator for double and so
the result is double! If you want to assign a double to an int there is an
certain amount of data loss(everthing behind the ,). More advanced
compilers warn you about such things. The program should compile anyway!
If want to get rid of the warning(and make the statement more understandable
for others) you have to cast it!

int page.x = (int) (page.x * (ppiScr.x / 25.4));
or
int page.x = static_cast<int> (page.x * (ppiScr.x / 25.4));

besides are you shure that you want to use
int page.x = page.x * ppiScr.x / 25.4;
you are initializing an page.x with page.x * ...
page.x doesn't have any value yet so page.x * ... could be pretty much
anything!

Hope I could help!
DevH
 
H

Howard

Otto Wyss said:
I have the following statements

int page.x = page.x * ppiScr.x / 25.4;
int page.y = page.y * ppiScr.y / 25.4;

That shouldn't compile at all. You are defining "int page.x", and you
should get a syntax error ("; expected") where that period is.
-Howard
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top