tags:

views:

247

answers:

6
double deg=90;
double two= 2* System.Math.PI;
double rad=(two)*(deg/360);

the original value when calculating manually ,rad is 1.5707963267948966 but rad shows when debugging is 1.5707963705062866 what is reason for this and how do i fix it.but correct answer is manual calculation answer only......

Here are the numbers for easier comparison:

1.5707963267948966
1.5707963705062866
         --------- <-- differences

during the debug i put the pointer in right side that means calculation side it shows correct answer but the error happen when storing that value to rad.

is anybody help for me.i need it.

+6  A: 

Rounding errors happen; it isn't infinite precision. You will have to test whether values are suitably close - don't just test for equality. In some cases you might also see subtle differences by applying the operators in a different sequence, so you don't swamp small numbers with magnitude.

Marc Gravell
i need exact value for my further calculation so only
ratty
You need the *exact* value of maths involving Pi? I hope you've got plenty of RAM... You'll need to use specialized libraries to do that; `double` (IEEE 754) simply can't do it.
Marc Gravell
no i got same result for this also
ratty
thank for u comment mr.marc what is that specialized libraries for this
ratty
@Marc Gravell: Do you mean "I hope you've got infinite RAM"?
Residuum
@Residuum: Pi is a number that has, as far as humanity knows, an infinite number of places after the decimal place.
R. Bemrose
@R. Bemrose - not quite: it is a number that humanity **knows** has an infinite number of places after the decimal point. It is a *relatively* simple proof, IIRC.
Marc Gravell
@Marc, the proof of irrationality is pretty straightforward, here's a nice one: http://www.mathlesstraveled.com/?p=548. The proof that pi is not merely irrational but transcendental is trickier; begin by proving that e is transcendental and then the result for pi follows from Euler's identity.
Eric Lippert
@Eric Lippert: Or the Lindemann-Weierstrass theorem gets you both `e` and `π`.
Jason
+2  A: 

Use a decimal type rather than double. It's due to floating point precision.

Ian
decimel type means
ratty
decimal is also only approximate... but it is approximated in different ways (and has a few more bits to play with). It is also noticeably slower.
Marc Gravell
The Decimal type should be used with things like currency calculations. When doing scientific / mathematical calculations, Double should be used, unless you need better precision, then a specialised library is the way to go.
Graham Clark
A: 

You should use a decimal value, which is more accurate than double.

Shimrod
no i cant plz give other suggestions
ratty
A: 

Double, float and decimal are really your only standard options. If they are not precise enough for your needs, you will need to create your own type and provide it a means of storing these high precision values and its own operators for performing mathematical functions.

You'll notice the double type is accurate to 7 places, the decimal type will be accurate for slightly more. If you want greater precision than that, you will need to come up with an algorithm for computing it and storing it.

Joel Etherton
The double type is not accurate to only 7 places, more like 16. The problem in the question is not because of imprecise double data type being used. What you notices is that his question seems to indicate that the precision is only 7 digits, but that is wrong.
Lasse V. Karlsen
I know, but in his example it was only accurate to his calculation to 7 digits likely because of the differences in PI and order of operations. Point I was making was that the standard types should be good enough to accomplish the task, if they're not a new one just needs to be made up to achieve the result.
Joel Etherton
A: 

Using your code I can't reproduce what you're seeing.

I did this:

using System;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            double deg = 90;
            double two = 2 * System.Math.PI;
            double rad = (two) * (deg / 360); 

            Console.Out.WriteLine(rad);
        }
    }
}

And I got this output:

1.5707963267949

When hovering over the rad variable when on a breakpoint on the WriteLine line, I saw the following value in the tooltip

1.5707963267948966

Where/how did you see that other value?

The difference above is probably due to me not specifying the precision in my write-call, so perhaps I could get all the digits there as well. I'm certainly not seeing the second value you mention.

Lasse V. Karlsen
i did this in windows form apllication
ratty
Is it possible for you to create a short, but complete, program that shows us the problem? Since I can't reconstruct the problem I'm willing to bet the code in question isn't really as simple as the one you've placed in the question. In other words, I am willing to bet there is something you're not telling us. My guess is that you have fallen into the normal trap of "simplifying" the code so that we can understand it, and in the process, you've "simplified" away the problem.
Lasse V. Karlsen
A: 

Here's a pretty good description of floating-point arithmetic:

What Every Computer Scientist Should Know About Floating-Point Arithmetic

Richard Morgan