Converting double (or float) to int - c++

This is a discussion on Converting double (or float) to int - c++ ; After a little experimentation I see that with the g++ compiler on x86, when a double is converted to an int, the fractional part is discarded. Examples: int(0.7) == 0 int(1.3) == 1 int(-1.3) == -1 Is this behaviour consistent ...

+ Reply to Thread
Results 1 to 2 of 2

Converting double (or float) to int

  1. Default Converting double (or float) to int

    After a little experimentation I see that with the g++ compiler on x86,
    when a double is converted to an int, the fractional part is discarded.

    Examples:

    int(0.7) == 0
    int(1.3) == 1
    int(-1.3) == -1

    Is this behaviour consistent between different platforms? Is the result
    of these operations defined by the standard, or should one use functions
    like ceil() and floor() (and check for negative values) instead?

  2. Default Re: Converting double (or float) to int

    Szabolcs wrote:
    > After a little experimentation I see that with the g++ compiler on
    > x86, when a double is converted to an int, the fractional part is
    > discarded.
    > Examples:
    >
    > int(0.7) == 0
    > int(1.3) == 1
    > int(-1.3) == -1
    >
    > Is this behaviour consistent between different platforms? Is the
    > result of these operations defined by the standard, or should one use
    > functions like ceil() and floor() (and check for negative values)
    > instead?


    Yes, the behaviour is standard, defined in the subclause 4.9.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask



+ Reply to Thread

Similar Threads

  1. double and float
    By Application Development in forum labview
    Replies: 0
    Last Post: 12-06-2007, 10:10 AM
  2. converting 'float (*)[4]' to 'const float**'
    By Application Development in forum c++
    Replies: 9
    Last Post: 09-15-2007, 04:10 AM
  3. why .Net Matrix use float, but not double
    By Application Development in forum DOTNET
    Replies: 2
    Last Post: 04-28-2007, 05:10 PM
  4. float v double, esp in JOGL
    By Application Development in forum Graphics
    Replies: 19
    Last Post: 10-04-2006, 02:44 PM
  5. UDP and float/double values
    By Application Development in forum Java-Games
    Replies: 2
    Last Post: 02-21-2005, 02:07 AM