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

Discussion in 'C++' started by Otto Wyss, Aug 19, 2003.

  1. Otto Wyss

    Otto Wyss Guest

    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

    --
    See "http://wxguide.sourceforge.net/" for ideas how to design your app.
     
    Otto Wyss, Aug 19, 2003
    #1
    1. Advertising

  2. Otto Wyss

    K.S. Guest

    Otto Wyss wrote:

    > 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
     
    K.S., Aug 19, 2003
    #2
    1. Advertising

  3. Otto Wyss

    Howard Guest

    "Otto Wyss" <> wrote in message
    news:1fzykdf.qko6vt1x1d1dvN%...
    > 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
     
    Howard, Aug 19, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,505
    Victor Bazarov
    Feb 17, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,220
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  4. er
    Replies:
    7
    Views:
    2,984
    Michael Tsang
    Jan 19, 2010
  5. Shriramana Sharma
    Replies:
    8
    Views:
    275
    Gerhard Fiedler
    Jun 18, 2013
Loading...

Share This Page