By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,652 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Converting double (or float) to int

P: n/a
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?
Jun 8 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jun 8 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.