473,396 Members | 1,780 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,396 software developers and data experts.

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?How to solve it?
Nov 17 '05 #1
2 1431
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?How 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**********@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP12.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?How
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
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
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...
8
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
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
16
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"...
3
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...
29
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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.