472,328 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Float/Double arithmetic precision error

Hi all,
I had problem regarding float/double arithmetic only with + and -
operations, which gives inaccurate precisions. I would like to know how the
arithmetic operations are internally handled by C# or they are hardware
(processor) dependent. Basic addition operation errors, for ex:
3.6 - 2.4 = 1.19999999999 or 1.20000000003
There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please
reply me how these operations are handled internally.
Thanks in Advance,
Madan.
Nov 15 '05 #1
3 23529
Floating point representations are inherently imprecise for many numbers.
This paper does a good job explaining it:

http://www.physics.ohio-state.edu/~d...point_math.pdf

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Madan" <s0******@jntu.ac.in> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl...
Hi all,
I had problem regarding float/double arithmetic only with + and -
operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled by C# or they are hardware
(processor) dependent. Basic addition operation errors, for ex:
3.6 - 2.4 = 1.19999999999 or 1.20000000003
There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please
reply me how these operations are handled internally.
Thanks in Advance,
Madan.

Nov 15 '05 #2
Madan <s0******@jntu.ac.in> wrote:
I had problem regarding float/double arithmetic only with + and -
operations, which gives inaccurate precisions. I would like to know how the
arithmetic operations are internally handled by C# or they are hardware
(processor) dependent. Basic addition operation errors, for ex:
3.6 - 2.4 = 1.19999999999 or 1.20000000003
There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please
reply me how these operations are handled internally.


See http://www.pobox.com/~skeet/csharp/floatingpoint.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Madan,
I had problem regarding float/double arithmetic only with + and -
operations, which gives inaccurate precisions.
It may be symantics, but to me the representation of floating-point
precision numbers in a "binary" machine is a the problem, not necessarily
the precision of the number, unless you want to specify exactly how many
decimal places are significant for every floating point number and any
calculation that involves floating point numbers.
I would like to know how the
arithmetic operations are internally handled by C# or they are hardware
(processor) dependent.
The problems with floating-point number representation are not specific to a
particular language. They are specific to representing floating-point
number on binary computers. Essentially, you are representing a continuous
number in a discrete environment. The effective approach is to break up the
range of values that a floating-point number can have into tiny intervals
(the precision the machine you are running the code on.).
Basic addition operation errors, for ex:
3.6 - 2.4 = 1.19999999999 or 1.20000000003
There are the erroneous values I'm getting.
Before declaring these erroneous, you should specify what precision you are
looking for. They are correct to 10 decimal places. If you want higher
precision, use a double. If you want exact precision, use the
System.Decimal class and specifiy the number of decimal places.
I'm using C#.Net v1.1 Please
reply me how these operations are handled internally.


It is bad programming form to compare floating-point numbers and results
using the traditional "==" operator. You should calculate the machine
precision and then check to see if the difference of two floating-point
numbers is smaller than some small value based on the machine precision.

Hope that helps.

Regards,

Randy
Nov 15 '05 #4

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

Similar topics

9
by: Marc Schellens | last post by:
My compiler warns about assignment to int form float/double. Will it nevertheless do what I expect? Are there caveeats/pits? (Apart from the...
26
by: Alexander Block | last post by:
Hello newsgroup, let's say I have a function like template<class Type> inline bool areEqual(const Type &a, const Type &b) { return ( a == b...
4
by: JKop | last post by:
Does the Standard specify any minimum range or minimum precision for the float, double and long double. I haven't found anything in the Standard...
4
by: Saso Zagoranski | last post by:
Hi! I have created an SqlCommand object and I have set some parameters to it... One of the parameters is of the DateTime type; here is the...
4
by: illegal.prime | last post by:
Hi all, I have a class that I would like to use to hide the data type I'm using to store decimal point numbers. Failing all else, I will just use...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when...
1
by: veremue | last post by:
I want to store a float/double as String. I have an input file with a column with float values i.e. have the form 1.4E15. I want to store these...
3
by: Wander | last post by:
currently there are those functions is_float, is_double and is_real, which do exactly the same as far as i can see what surprises me is that $a =...
13
by: =?Utf-8?B?U3RlZmFuRw==?= | last post by:
With the test I've made on my WinXP/VS2005/C#2.0, I get a precision of 42bit. Is this specified in C# or dependent on what? double dLeft =...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.