0

I have migrated from (classic) .NET 4.8 to .NET 6 (Core) lately. I am seeing differences in the precision of datatypes (float, double). It is really hard to change the behavior manually (by using Math.Round() or something else) as the project is significantly big. Is there any way I can overcome this issue?

Ex: a multiplication operation in .NET 4.8 is returning 3748.9 where as the same operation is returning 3748.899999999996 in .NET 6.

Here is the line of code

Math.Abs(quantity * Price).ToString(Culture.CurrentCulture);

where quantity and price are of type double

12
  • 6
    Are you sure it's actually different and not just a difference in display format? Have you looked at the binary values (BitConverter.GetBytes() for instance) to see if there's a true change in values? Commented Jan 12, 2024 at 6:25
  • 1
    In .NET 6, there have been improvements in the handling of floating-point operations, and the behavior might be more accurate, adhering more closely to the IEEE 754 standard. You can try somethig like Math.Round(double/float value, decimals you need to get); Commented Jan 12, 2024 at 7:30
  • 5
    If your code expected a floating-point number to be exactly anything (except perhaps integers within a reasonable range) then the problem is your code; that's not how floating point works, or has ever worked. I agree this is most likely a display change, not a value change, but fundamentally: it sounds like you should have been using decimal, which works more like humans expect (but which is slower and larger) Commented Jan 12, 2024 at 7:34
  • 3
    a multiplication operation in .NET 4.8 is returning 3748.9 what multiplication? How did you inspect the result? Are you sure that the result wasn't a result of text formatting changes? The real change in .NET Core was in formatting, not multiplication Commented Jan 12, 2024 at 8:08
  • 1
    Project > Properties > Build tab, "Platform target" setting. Change it from AnyCPU to x86, repeat for the Release build, now your code produces floating point results the way it did in 4.8. Computing with 80-bit intermediate results as provided by the FPU instead of the consistent 64-bit results as provided by the SSE2 instruction set. This is not a problem you should solve. stackoverflow.com/questions/588004/… Commented Jan 12, 2024 at 8:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.