473,394 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Casting float to int using (int)

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
Jul 19 '05 #1
6 42533
"Seabook" <ga********@hotmail.com> wrote in message
news:c6**************************@posting.google.c om...
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
Jul 19 '05 #2
Seabook wrote:
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.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #3
Probably you would want to have the number rounded, done by adding 0.5.

Fraser.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.532 / Virus Database: 326 - Release Date: 27/10/2003
Jul 19 '05 #4
Fraser Ross wrote:
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

Jul 19 '05 #5
Christian Brechbühler <c_************@yhaoo.com> wrote in message news:<Ox9pb.67133$ao4.171996@attbi_s51>...
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.
Jul 19 '05 #6
Christian Brechbühler wrote:

Fraser Ross wrote:
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).


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

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jakob Bieling | last post by:
Hi, I have a question about casting and using the casted pointer: Suppose I have a 'base' class and a 'derived' class (which is derived from 'base'). Now I have this scenario: base* p1 = new...
3
by: MAY | last post by:
hi, i have problem to convert from float to int, Point i_pt; PointF f_pt; ..... how to make i_pt=f_pt; ??
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
8
by: Russ | last post by:
Does it ever make sense to derive a class from a basic type such as float or int? Suppose, for example, that I want to create a class for physical scalars with units. I thought about deriving from...
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
4
by: chandanlinster | last post by:
I came to know that using format specifiers(in printf or scanf) to convert between float and int is a really bad idea. This is because the printf prints junk values when it encounters such a type...
3
by: docmur | last post by:
Okay heres my problem I have a problem called Calc which the user enters 2 numbers and a sign to be done with what is entered EX: ./calc 200 + 200 or ./calc 200.20 + 200 Okay thats easy...
6
by: punitshrivastava | last post by:
Hi to All, I am Punit Shrivastava .I am working on Asp.net with C#.i am new in C#.I want to assign null or blank to a variable which is of float or int type.Please suggest me what i do. Thanks &...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.