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

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 27512
-_-
args {Length=0} string[]
temp 3640 int
I get the above in the debugger

--
-_-

--
"Richard Thrasher" <rl*****@sbcglobal.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.9999999999995

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*****@sbcglobal.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*****@sbcglobal.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*****@sbcglobal.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*****@sbcglobal.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
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
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...
1
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
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,...
16
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
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
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...
1
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 (...
14
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
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 =...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.