Casting float to int using (int)

S

Seabook

Hello guys,
I just bumped into a page at nVidia.com that says that casting from
float to int using (int) is a bad idea. What is the problem with
(int)? Is it just speed or some other sublime bug that is present in
some compilers/cpu's?

Kal
SOEN Undergraduate, ConU
Montreal, QC, CA
 
M

Mike Wahler

Seabook said:
Hello guys,
I just bumped into a page at nVidia.com that says that casting from
float to int using (int) is a bad idea.

It depends upon what the desired result is. In some
contexts, there's nothing wrong with it at all.
What is the problem with
(int)?

No 'inherent' problems imo, but there is indeed a problem
when it's misused.
Is it just speed or some other sublime bug that is present in
some compilers/cpu's?

You need to provide more context about *why* the author
of whatever you read calls the cast 'bad'.

-Mike
 
A

Andrey Tarasevich

Seabook said:
Hello guys,
I just bumped into a page at nVidia.com that says that casting from
float to int using (int) is a bad idea. What is the problem with
(int)? Is it just speed or some other sublime bug that is present in
some compilers/cpu's?

If you take a look at the code that accompanies the message, you'll see
that this is nothing more than an advice to use 'frndint' instruction on
modern x86 processors to convert floating-point types to 'int'. This
advice might make some sense in performance-critical x86-specific
applications when using an older compiler (i.e. a compiler that is not
aware of 'frndint' instruction). Otherwise, the advice doesn't make much
sense. There's no problem with '(int)'. And, strictly speaking, this
doesn't have much to do with '(int)' and with C++ in general.
 
F

Fraser Ross

Probably you would want to have the number rounded, done by adding 0.5.

Fraser.
 
?

=?ISO-8859-1?Q?Christian_Brechb=FChler?=

Fraser said:
Probably you would want to have the number rounded, done by adding 0.5.

That works for non-negative numbers. Stroustrup, C.6.2.6: "the fractional part
is discarded. In other words, conversion from a floating-point type to an integer
type truncates".

BTW, (int)x is legacy from C; in C++ we prefer int(x).

Christian
 
E

Evan

Christian Brechbühler said:
BTW, (int)x is legacy from C; in C++ we prefer int(x).

Still, the first syntax is useful to know so you can understand it
when others use it. It's also the only way to cast to, say, a pointer:
char*(something) won't work, you need (char*) something.

And actually, some people will say that (int)x is legacy from C,
int(x) is legacy from earlier versions of C++, and in modern C++
static_cast<int>(x) is preferred. (Substituting dynamic_cast<>,
const_cast<>, and reinterpret_cast<> as appropriate.) This is one of
those style issues; typically I use the int(x) style casts when it is
clear what the meaning is (double to int for instance) and use the
"ugly" casts when the meaning isn't so clear and for dynamic casts.
 
P

Pete Becker

Christian said:
That works for non-negative numbers. Stroustrup, C.6.2.6: "the fractional part
is discarded. In other words, conversion from a floating-point type to an integer
type truncates".

BTW, (int)x is legacy from C; in C++ we prefer int(x).

Perhaps some of "we" prefer the latter. I, for one, generally use the
former.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top