473,385 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,385 software developers and data experts.

Rounding Error...Please help

I have written the following codes:

double mean = 313.4;
int fieldValue = 321;
double result = ((double)fieldValue) - mean;
MessageBox.Show(result.ToString());
MessageBox.Show(Math.Pow(result,2).ToString());

However, for the value of result, I got 7.6000000000000227 instead of 7.6
This made my calculation wrong...

Any idea to solve it?
Nov 17 '05 #1
2 2344
ywchan <yw****@gmail.com> wrote:
I have written the following codes:

double mean = 313.4;
int fieldValue = 321;
double result = ((double)fieldValue) - mean;
MessageBox.Show(result.ToString());
MessageBox.Show(Math.Pow(result,2).ToString());

However, for the value of result, I got 7.6000000000000227 instead of 7.6
This made my calculation wrong...

Any idea to solve it?


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

In short, the value stored as "mean" isn't 313.4 (which a double can't
represent) - it's

313.3999999999999772626324556767940521240234375

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
> I have written the following codes:

double mean = 313.4;
int fieldValue = 321;
double result = ((double)fieldValue) - mean;
MessageBox.Show(result.ToString());
MessageBox.Show(Math.Pow(result,2).ToString());

However, for the value of result, I got 7.6000000000000227 instead of 7.6
This made my calculation wrong...

Any idea to solve it?


0.1 can't be binary written with final number of digits. 0.4 = 4 * 0.1, so
here is your 'error'.
Nov 17 '05 #3

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

Similar topics

20
by: Raoul Watson | last post by:
By any chance, anyone got a rounding routine that does a work around the VB "round" bug? I still find it amazing that a company as large as Microsoft would put out a math package that is so...
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
9
by: Joe Attardi | last post by:
Hi all, Math is not my strongest area so forgive me if I use some of the wrong terminology. It seems that scientific notation is immune to rounding errors. For example: (4.98 * 100) + 5.51 ...
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...
4
by: tom | last post by:
Hello, I have a problem with logic of rounding function. For examp.: the result of expression =* is 6,825 and when I use round on 2 decimal places I get 6,82 instead of 6,83!? If I dont use...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
2
by: bagerbeiger | last post by:
I am relatively new to programming (3-5 months) and have to write a program that prompts users for input (number of items ordered at a restaurant), performs some monetary calculations, and then...
2
by: Steve Lloyd | last post by:
Hi, Can anyone please explain this rounding error on a windows forms project. I take an input and need to convert from a percentage to a decimal so i do: dim result as double =...
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
2
by: asutic | last post by:
Hi. I've encountered a problem with rounding which I cannot resolve elegantly and I has hoping that somebody could help me out. Here's the problem description: We are given 4 numbers a, b, c,...
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: 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
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.