Exact problems with Visual Studio 2010

I have an application written in Microsoft Visual C ++ 6.0. Now I rewrote the application in Visual Studio 2010 using C #, but the results do not match due to dot issues. One such accuracy problem is as follows:

float a = 1.0f; float b = 3.0f; float c = a / b; 

This is C # code when launched in Visual Studio 2010 gives c value = 0.333333343

But the same code that removes f after the value in the definition of the value, when run on Visual C ++ 6.0, gives c value = 0.333333 .

Can someone figure it out and explain how to have the same value for c in Visual Studio as well as in Visual C ++ 6.0 ??


In fact, the values โ€‹โ€‹are taken from the viewport. I found out that different versions of the visual studio may differ in the presentation of the floating point format. Therefore, the values โ€‹โ€‹in hours may not be practical. It is for this reason that I printed the values โ€‹โ€‹in both versions of the visual studio, and the results are as follows. with visual studio 6.0 using the visual language C ++ it's 0.333333 (six 3)

but with visual studio 10 using C # language, it's 0.3333333 (seven 3)

So can anyone help me make my C # program to get the same result as visual C ++ ??? (for example, how to do floating operations to get the same results for both versions?)

+4
source share
4 answers

Given that the exact value is 0.3, none of them are โ€œcorrectโ€, and if you are trying to match the exact results of double-floating point calculations, then usually a bad idea starts with how they work, (See My article on binary floating point in .NET for more information.)

Perhaps you should not use binary floating point in the first place (for example, if your values โ€‹โ€‹are exact, artificial sums, such as money). Alternatively, it is possible that you should make comparisons of comparisons with a certain tolerance.

It is also possible that C # and C produce exactly the same bit pattern - but you see different results due to how these values โ€‹โ€‹are formatted. Again, I would not use the textual representation of numbers for comparison.

+15
source

C# just shows fewer decimal places. 0.333333343 rounded to six significant digits 0.333333 . The base value of c remains unchanged.

Of course, if you want more precision, you can always use double variables.

+5
source

C # floating point can handle float and double types. A float has an accuracy of 7 digits and double 16.

I believe that the standard C ++ precision is about 16 digits (15.9 on average!).

In any case, not a single representation is arithmetically correct, since 1/3 is 0.3333.

I think this is just a representation of the value you see (remember that the debugger converts the value to a string for display. If you check the memory cells for each, you will probably find that the values โ€‹โ€‹are the same.

+3
source

A quick check shows that yes, the numbers are exactly the same. So the answer to your question is that if you want the same result, all you have to do is make sure that the display methods are compatible.
For example, printf("%9.7f", result) in C and string.Format("{0:0.0000000}", result) in C #. It's all.

+1
source

Source: https://habr.com/ru/post/1386505/


All Articles