Converting double (or float) to int

S

Szabolcs

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?
 
V

Victor Bazarov

Szabolcs said:
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
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top