444,190 Members | 1,653 Online
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

# Converting a float to int (Visual Studio 2003)

 P: n/a In the course of writing numerical code I needed to convert a float to an int with a defined behavior: if the float is great than INT_MAX, set the int to INT_MAX, otherwise assign directly. The problem I ran into is a float with value INT_MAX assigned to an int results in the value -2147483648 being assigned, but if the conversion takes place in an expression INT_MAX is assigned as I would expect: int temp1=float(std::numeric_limits::max()); // 2147483647 float temp2=std::numeric_limits::max(); //2.1474836e+009 int temp3=temp2; // -2147483648 My question: how do I write a function to trim a float to an int's limits? Aug 7 '05 #1
5 Replies

 P: n/a * Code4u: In the course of writing numerical code I needed to convert a float to an int with a defined behavior: if the float is great than INT_MAX, set the int to INT_MAX, otherwise assign directly. The problem I ran into is a float with value INT_MAX assigned to an int results in the value -2147483648 being assigned, but if the conversion takes place in an expression INT_MAX is assigned as I would expect: int temp1=float(std::numeric_limits::max()); // 2147483647 I think this may be computed at compile-time with 'double' precision. float temp2=std::numeric_limits::max(); //2.1474836e+009 int temp3=temp2; // -2147483648 Consider that in many current implementations an 'int' is 32 bits, and a 'float' is 32 bits. In the 'float' some bits are used for the exponent, so there are far fewer than 32 bits in the mantissa, i.e. not enough to represent an arbitrary 'int' value exactly. My question: how do I write a function to trim a float to an int's limits? Off the cuff, int intFromFloat( float f ) { static int const max = std::numeric_limits::max(); return 0?0 : f > max? max : f < -max? -max : static_cast( f ); } -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Aug 7 '05 #2

 P: n/a On Sun, 07 Aug 2005 00:41:27 GMT, al***@start.no (Alf P. Steinbach) wrote: * Code4u: In the course of writing numerical code I needed to convert a float to an int with a defined behavior: if the float is great than INT_MAX, set the int to INT_MAX, otherwise assign directly. The problem I ran into is a float with value INT_MAX assigned to an int results in the value -2147483648 being assigned, but if the conversion takes place in an expression INT_MAX is assigned as I would expect: int temp1=float(std::numeric_limits::max()); // 2147483647I think this may be computed at compile-time with 'double' precision. float temp2=std::numeric_limits::max(); //2.1474836e+009 int temp3=temp2; // -2147483648Consider that in many current implementations an 'int' is 32 bits, and a'float' is 32 bits. In the 'float' some bits are used for the exponent, sothere are far fewer than 32 bits in the mantissa, i.e. not enough torepresent an arbitrary 'int' value exactly. My question: how do I write a function to trim a float to an int's limits?Off the cuff,int intFromFloat( float f ){ static int const max = std::numeric_limits::max(); return 0?0 : f > max? max : f < -max? -max : static_cast( f );} Thanks, yep, I should have spotted the compile-time conversion to double. One question, I don't quite understand the syntax of the ternary conditional operator you used in the function, I've never seen 0?0 in code before, could you elucidate? Aug 7 '05 #3

 P: n/a return 0?0 : f > max? max : f < -max? -max : static_cast( f ); means if(0) { return 0; } else if(f>max) { return max; }else if(f<-max){ return -max; }else{ return static_cast( f ); } Aug 7 '05 #4

 P: n/a On 6 Aug 2005 20:31:25 -0700, "__PPS__" wrote: return 0?0 : f > max? max : f < -max? -max : static_cast( f );meansif(0) { return 0;} else if(f>max) { return max;}else if(f<-max){ return -max;}else{ return static_cast( f );} I understand how the conditional operator works. But what purpose does if (0) serve? It is always false and is therefore redundant. Obviously it must have a virtue, but I can't figure it out. Aug 7 '05 #5

 P: n/a * Code4u: I understand how the conditional operator works. But what purpose does if (0) serve? It is always false and is therefore redundant. Obviously it must have a virtue, but I can't figure it out. It's just a formatting issue. I prefer the colons lined up neatly on the left. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Aug 7 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.