473,804 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem about converting "float" to "int"

I write below code in my program:
float f=0.371f;
int i=(int)(f*1000. 0f);

I think the result of "i" should be "371",but in fact it is "370",why?H ow to solve it?
Nov 17 '05 #1
2 1453
pango wrote:
I write below code in my program:
float f=0.371f;
int i=(int)(f*1000. 0f);

I think the result of "i" should be "371",but in fact it is "370",why?H ow to solve it?


If you look at f in your debugger you will see it is actually
0.37099999. This is the nature of floating point representation. The
other key thing to note is that float to integer conversions don't
round. If you want rounding, you write your own.
And, as an exercise in confusion, inspect this with your debugger:

int i2 = (0.371 * 1000.0);
doug.
Nov 17 '05 #2
Search for "what every programmer should know about floating point" using
your favorite search engine for the classic (and well written) article
describing this and other relevant issues.

Ronald Laeremans
Visual C++ team

"doug mansell" <do**********@h otmail.com> wrote in message
news:uS******** ******@TK2MSFTN GP12.phx.gbl...
pango wrote:
I write below code in my program:
float f=0.371f;
int i=(int)(f*1000. 0f);

I think the result of "i" should be "371",but in fact it is "370",why?H ow
to solve it?


If you look at f in your debugger you will see it is actually 0.37099999.
This is the nature of floating point representation. The other key thing
to note is that float to integer conversions don't round. If you want
rounding, you write your own.
And, as an exercise in confusion, inspect this with your debugger:

int i2 = (0.371 * 1000.0);
doug.

Nov 17 '05 #3

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

Similar topics

0
2192
by: python | last post by:
Hi- I'm getting a "DeprecationWarning: integer argument expected, got float" warning with mx.DateTime and I can't figure out why. Can anyone help me out? This code: import mx.DateTime
4
1629
by: Joe Peterson | last post by:
I could not find another example of this via internet searches, so here it is... I am wondering if this is a python bug or otherwise. The first example of this happened in a larger program of mine, and the traceback reports the problem at the start of a "for" loop (making no sense), but I cannot easily include the full code and instructions here. So I wrote a small test program containing the offending code, and in this case the...
8
8885
by: Manuel | last post by:
Anyone know why for openGL applications must be used GLfloat (and GLint, etc...) instead float, int, etc..? thx, Manuel
134
9112
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
16
11287
by: chandanlinster | last post by:
As far as I know floating point variables, that are declared as float follow IEEE format representation (which is 32-bit in size). But chapter1-page no 9 of the book "The C programming language" states that "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE USING". Does this mean "float" variables have different sizes on different machines?
3
2941
by: nineoo | last post by:
To all, Ok , now this may sound a little crazy, but does any one know if there is a way to increase the maximum value of a unsigned long int? I'm aware of the the max value of an unsigned long int but for I was trying to figure out if there was there a way to do what I was asking because the I am asked to increase the max value of of a particular value with in our code. The problem that Im running into has to deal with the way...
29
11126
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
9595
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
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9177
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
6870
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
5536
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...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.