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

Float parsing

The following instruction:

float myFloat = float.Parse("30.900665");

Assigns a value to myFloat of "30.9006653", how can this be? Where did the
extra 3 came from?

Thanks
Jun 25 '06 #1
2 2649
"Rene" <a@b.c> wrote:
The following instruction:

float myFloat = float.Parse("30.900665");

Assigns a value to myFloat of "30.9006653", how can this be? Where did the
extra 3 came from?


What Every Computer Scientist Should Know About Floating-Point
Arithmetic:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Basically, floating-point numbers are stored in the computer as floating
point binary numbers, not floating point decimal numbers; as such, the
parts after the decimal point, in order to match exactly, must be a sum
of negative powers of two. For example:

0.5 is exactly representable, because it's 2^-1
0.25 is exactly representable, because it's 2^-2
0.75 is exactly representable, because it's 2^-1 + 2^-2
0.1 is *not* exactly representable as a binary number without
introducing infinitely repeating digits, and so will differ from the
decimal number 0.1 by some small amount.

-- Barry

--
http://barrkel.blogspot.com/
Jun 25 '06 #2
"Rene" <a@b.c> wrote in message
news:Oz**************@TK2MSFTNGP02.phx.gbl...
The following instruction:

float myFloat = float.Parse("30.900665");

Assigns a value to myFloat of "30.9006653", how can this be? Where did the
extra 3 came from?


try using the decimal data type instead. If you're using anything that must
be completely accurate (eg money) then you should not use floating point in
any part of your app (eg, database or code) and you should be careful not to
convert from decimal to floating point and back. If you use floating points
you need to consider the results to not be completely accurate and that some
of the least significant digits can be incorrect or lost, eg this second
messagebox shows zero:

float x = 0.000001f;
MessageBox.Show(x.ToString());
x = x + 100;
x = x - 100;
MessageBox.Show(x.ToString());

Michael
Jun 26 '06 #3

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

Similar topics

19
by: Jon Shemitz | last post by:
Is there a difference between a constant like "12.34f" and "(float) 12.34"? In principle, at least, the latter is a double constant being cast to a float; while the two both generate actual...
3
by: c0uch | last post by:
the first and third methods are in a usercontrol object. txtValue is a TextBox the float.parse in the if statement on line 3 always works fine. the second float.parse in the third method is...
4
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
5
by: vivekaseeja | last post by:
Hi , Trying to convert a string value to a float value after reading the value from an XML file , but not sure what function to use. The following only works for integers Int32.Parse...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
43
by: Xancatal | last post by:
Hey everybody. I need help on this one. I need to verify that a number entered by a user is not either a negative number (-100.00), or an alphabet (a, b, c, X, Y) as well as other number other than...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
4
by: buntyindia | last post by:
Hi, I have a calculator with seven textBox to add Float numbers upto 2 decimal: I have created following function in js: function total_expenses() { // reteriving all the values from...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
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...

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.