473,657 Members | 2,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Float to int conversion error

Writing my very first C# program, I found what appears to
be a compiler error. The statement
int temp = (int)(100 * 36.41);
assigns the value 3640 to temp. I've tried this code with
various numbers in place of 36.41, but 36.41 is the only
number I've found that produces the error.

My questions are:
(1) Can anybody else reproduce this error?
(2) How does one submit bug reports to MS?
Nov 15 '05 #1
5 27522
-_-
args {Length=0} string[]
temp 3640 int
I get the above in the debugger

--
-_-

--
"Richard Thrasher" <rl*****@sbcglo bal.net> wrote in message
news:03******** *************** *****@phx.gbl.. .
Writing my very first C# program, I found what appears to
be a compiler error. The statement
int temp = (int)(100 * 36.41);
assigns the value 3640 to temp. I've tried this code with
various numbers in place of 36.41, but 36.41 is the only
number I've found that produces the error.

My questions are:
(1) Can anybody else reproduce this error?
(2) How does one submit bug reports to MS?

Nov 15 '05 #2
If you don't cast to int, and try this:

double temp = 100 * 36.41;

you'll find that temp is 3640.9999999999 995

As Jon Skeet pointed out to me earlier, the cast to int rounds down to 0,
while a Convert.ToInt32 will round to the nearest integer. If you try

int temp = Convert.ToInt32 (100 * 36.41);

you'll find that temp is 3641

HTH,
Jeff

"Richard Thrasher" <rl*****@sbcglo bal.net> wrote in message
news:03******** *************** *****@phx.gbl.. .
Writing my very first C# program, I found what appears to
be a compiler error. The statement
int temp = (int)(100 * 36.41);
assigns the value 3640 to temp. I've tried this code with
various numbers in place of 36.41, but 36.41 is the only
number I've found that produces the error.

My questions are:
(1) Can anybody else reproduce this error?
(2) How does one submit bug reports to MS?

Nov 15 '05 #3
This is a common problem with floating point values and is really a limitation instead of a bug. This problem is common to all
languages that use floating point values. It is even a limitation in the maths co processor in your cpu. When C# converts the value
to an integer it must just round down, so if the value is stored as 3640.99999 then it becomes 3640. You can use Convert.ToInt32 to
get the correct value.

--
Michael Culley
"Richard Thrasher" <rl*****@sbcglo bal.net> wrote in message news:03******** *************** *****@phx.gbl.. .
Writing my very first C# program, I found what appears to
be a compiler error. The statement
int temp = (int)(100 * 36.41);
assigns the value 3640 to temp. I've tried this code with
various numbers in place of 36.41, but 36.41 is the only
number I've found that produces the error.

My questions are:
(1) Can anybody else reproduce this error?
(2) How does one submit bug reports to MS?

Nov 15 '05 #4
Whenever you do an (int), just add 0.5; i.e (int) (100 * 36.41 + 0.5)

Hilton
"Richard Thrasher" <rl*****@sbcglo bal.net> wrote in message
news:03******** *************** *****@phx.gbl.. .
Writing my very first C# program, I found what appears to
be a compiler error. The statement
int temp = (int)(100 * 36.41);
assigns the value 3640 to temp. I've tried this code with
various numbers in place of 36.41, but 36.41 is the only
number I've found that produces the error.

My questions are:
(1) Can anybody else reproduce this error?
(2) How does one submit bug reports to MS?

Nov 15 '05 #5

"Richard Thrasher" <rl*****@sbcglo bal.net> wrote in message
news:03******** *************** *****@phx.gbl.. .
Writing my very first C# program, I found what appears to
be a compiler error. The statement
int temp = (int)(100 * 36.41);
assigns the value 3640 to temp. I've tried this code with
various numbers in place of 36.41, but 36.41 is the only
number I've found that produces the error.

My questions are:
(1) Can anybody else reproduce this error?
(2) How does one submit bug reports to MS?


Well... You may have blundered into one of the big "gotchas" of computer
programming as we know it.

36.41 is a number that cannot be expressed exactly in binary, for the same
reason that 1/3 cannot be expressed exactly in decimal. (It would take an
infinite number of decimal places or binary places respectively.)

So when you write "36.41" you actually get a binary number very slightly
less than 36.41.

Then you multiply it by 100 and get 3640.9999999 or something like that.

Then you convert -- and if the int conversion is done by truncation, you get
3640.

At least that's my educated guess about what's happening.

The Microsoft "Decimal" numeric type overcomes this; it's actually
represented in decimal, so 36.41 is always exactly 36.41.
--

Michael A. Covington - Associate Director
Artificial Intelligence Center, The University of Georgia
http://www.ai.uga.edu/~mc
Nov 15 '05 #6

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

Similar topics

4
5024
by: Jim West | last post by:
Both int a; std::complex<float> b; std::complex<float> c = static_cast<float>(a)*b; and int a; std::complex<float> b;
54
8345
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This creates a big hole in a 32-bit timer routine I wrote. Questions. 1. Why does this happen? 2. Is there C macros/functions I can call to tell me when two non-zero numbers are multiplied and the
1
1622
by: Bruno van Dooren | last post by:
Hi, i have a simple question that has bothered me for some time now. if i have a function foo( double a) and i do something like float b = 2.0; foo(b);
6
7606
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
16
12785
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 }
27
2857
by: galt_57 | last post by:
I need to do just a few multiplies of long integers and have a divide by ten. Can I avoid using floats? Can I use two longs as a 64 bit value somehow? Thanks.
13
6179
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
1
3502
by: malick | last post by:
Hello it developers, I'm using PHPExcelReader which I downloaded from sourceforge (http://sourceforge.net/projects/phpexcelreader) To read excel xls files in php. In the file reader.php ( http://scripts.ringsworld.com/database-tools/xls2mysql/inc/Excel/reader.php.html ) Somewhere there happens a exponent to float conversion so that strings as 1E3 2E9 are converted to their floating point values. I'm trying to put this functionality off...
14
5534
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
8
14124
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val = 3.14159; u_long nw_val = *((u_long *) &val); than the nw_val equal to 1078530000, I made such conversion: float d_val = *((float*)&nw_val);
0
8302
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
8820
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
8718
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
8499
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
8601
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
7314
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
5630
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();...
1
2726
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
1937
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.