English 中文(简体)
Double variable keeping wrong value
原标题:

Simimilar problem to Math.Atan2 or class instance problem in C# and add two double given wrong result

It is something that simple lines:

public static String DegreeToRadianStr(Double degree)
    {
        Double piBy180 = (Math.PI / 180);

        Double Val = (piBy180 * degree);  // Here it is giving wrong value
    }

piBy180 * degree = -3.1415926535897931 but Val = -3.1415927410125732

alt text http://www.imagechicken.com/uploads/1262936639009840000.jpg I really have no clue what to do .. Please ask some questions regarding this, so that I can point out where it is going wrong.

It is amazing that piBy180 is keeping correct value.

Same thing is happening with other functions too, But some how I manipulated to get correct values.

I am using MS Visual Studio s C#.net SP1.

问题回答

It would help if you told us the value of degree then we could try to reproduce the problem...

Three things:

  • You should expect a certain amount of error in floating point arithmetic. I wouldn t expect this much, but don t expect to see an exact value.
  • String conversions generally won t show you the exact value. I have a file - DoubleConverter.cs - with a ToExactString method which can help diagnose this sort of thing.
  • Is your app using DirectX at all? That can change the processor mode to effectively use 32 bit arithmetic even when you ve got doubles in your code. That seems to me to be the most likely cause of the issue, but it s still just a guess really.

EDIT: Okay, so it sounds like I guessed right, and DirectX is probably the cause. You can pass CreateFlags.FpuPreserve to the Device constructor to avoid it doing this. That will reduce the performance of DirectX, admittedly - but that s a tradeoff you ll need to consider for yourself.

It could just be a string formatting issue. Try adding the line:

Debug.Assert(Val == (piBy180 * degree), "WTF?");

after the assignment. It shouldn t fail, since they are both doubles, and arithmetic operations on them should produce the same binary value.

This doesn t seem possible.

The value of piBy180 * degree is computed almost correctly, off by less than 2 units in the 17th significant digit. The CPU has to perform a multiplication to do this, and it did that in double precision, correctly, even if someone tried to mess up the precision by abusing DirectX calls.

The value of Val is wrong already at the 8th significant digit. Val is declared as double, and it gets assigned the double value that was computed a microsecond ago, with no further computations in the way.

Is this a release build? I wonder if C# might have omitted storing the latest value into Val, the way C++ can do. What happens in a debug build?

It could just be a rounding error, but I can t be sure.

Check out this article on floating point arithmetic. It s written for fortran, but it s still useful. http://www.lahey.com/float.htm





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签