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 ...
-
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?
-
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
Similar Threads
-
By Application Development in forum labview
Replies: 0
Last Post: 12-06-2007, 10:10 AM
-
By Application Development in forum c++
Replies: 9
Last Post: 09-15-2007, 04:10 AM
-
By Application Development in forum DOTNET
Replies: 2
Last Post: 04-28-2007, 05:10 PM
-
By Application Development in forum Graphics
Replies: 19
Last Post: 10-04-2006, 02:44 PM
-
By Application Development in forum Java-Games
Replies: 2
Last Post: 02-21-2005, 02:07 AM