473,796 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting a float to int (Visual Studio 2003)

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_limit s<int>::max()) ; // 2147483647

float temp2=std::nume ric_limits<int> ::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 5514
* 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_limit s<int>::max()) ; // 2147483647
I think this may be computed at compile-time with 'double' precision.

float temp2=std::nume ric_limits<int> ::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_li mits<int>::max( );
return 0?0
: f > max? max
: f < -max? -max
: static_cast<int >( 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
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_limit s<int>::max()) ; // 2147483647


I think this may be computed at compile-time with 'double' precision.

float temp2=std::nume ric_limits<int> ::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_li mits<int>::max( );
return 0?0
: f > max? max
: f < -max? -max
: static_cast<int >( 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


return 0?0
: f > max? max
: f < -max? -max
: static_cast<int >( f );

means

if(0) {
return 0;
} else if(f>max) {
return max;
}else if(f<-max){
return -max;
}else{
return static_cast<int >( f );
}

Aug 7 '05 #4
On 6 Aug 2005 20:31:25 -0700, "__PPS__" <i-*********@yande x.ru> wrote:


return 0?0
: f > max? max
: f < -max? -max
: static_cast<int >( f );

means

if(0) {
return 0;
} else if(f>max) {
return max;
}else if(f<-max){
return -max;
}else{
return static_cast<int >( 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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2052
by: Dave Smithz | last post by:
Hi there, Summary: Installing Visual Studio 6 AFTER Visual Studio 2003 trial and Office 2003 - Is it safe? I know this has been asked many times before, but I have seen postings about problems so wanted to clear it up for sure. I have Windows XP Pro. Already have installed Office 2003 and Visual Studio .net 2003 trial.
4
2110
by: Hawkmoth | last post by:
Hi I hope you can help with what seems to be a simple problem that is driving me nuts! I am developing a C# application using Visual studio 2003 and I need to use Office 2003 automation. In the helpfile it says to install the .Net Programmability support from Office setup or install Office using the complete option. Then select New Project in the Visual Studio 2003 IDE and choose the Microsoft Office System
5
3753
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging session we found out, that our program was working correctly, but the floating point processing...
2
2030
by: Belee | last post by:
1. I am using sql server express and visual studio 2003. I have been able to create the connection to the database in server explorer but cannot update table definitions. It gives an error that I may need a patch or changes would not be saved. So I have to do the table changes in SQL Express Management studio. 2. When I modify a stored procedure in the management studio of sql express, it does not update but rather saves it the...
9
1749
by: TD | last post by:
Which versions of Windows operating systems can you build applications for with Visual Studio 2003, in particular VB? How about Visual Studio 2005, in paricular VB? Thanks, TD
4
2396
by: Skc | last post by:
We have a developer who has made an application in Visual Studio 2003 and this will not work in our version of Visual Studio 2002. Error message: Solution file loading error: The selected file is not a valid Visual Studio Solution file. When we try to load a project file, we get: Unable to read the project file. The project was created with a newer
3
1700
by: robin9876 | last post by:
Is it possible to install Visual Studio 2003 and 2005 on the same pc?
1
2294
by: Jason Huang | last post by:
Hi, We've been trying working the Crystal Reports XI on our Visual Studio 2003, however, the CR XI doesn't work on our VS2003 at all. So, I am thinking giving up the CR XI. Would someone tell us whcih version of the Crystal Reports will work best on our Visual Studio 2003? given that the built in Crystal Reports for Visual Studio 2003 is not included. Thanks for help.
7
1835
by: Chris Marsh | last post by:
All I've been asked run a VM on my development machine, with Windows Server 2003 installed. I've also been asked to then develop against this environment from the host machine, using Visual Studio 2003. Questions: 1. Is it even possible to use VS 2003 to develop when all files and IIS are on a remote machine or VM?
4
1730
by: Vadim | last post by:
Hello, I need to converting project that done on Visual Studio 2005 to Visual Studio 2003. What the best way to do this? Vadim Vilkhov
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10244
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10201
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.