0

I have some code that is behaving strangely and seems to be rounding the result of the addition of two double values. This is causing issues with my code.

Unfortunately I cannot fix this issue because my unit testing environment is working OK (not rounding) and my application is not OK (rounding).

In my test environment:

a = -3.7468700408935547
b = 525218.0
c = b + a
c = 525214.25312995911

Inside my application:

a = -3.7468700408935547
b = 525218.0
c = b + a
c = 525214.25

What can be causing this? Project config? (I'm using visual studio, btw)

Edit (from comments)
I'm stepping through the same code using the Visual Studio debugger, so it's the exact same piece of code.

I have more code but I narrowed the problem down to that particular sum.

The binary representations of each value are:

test environment:

System.BitConverter.ToString(System.BitConverter.GetBytes(a))   "00-00-00-00-97-F9-0D-C0"   string
System.BitConverter.ToString(System.BitConverter.GetBytes(b))   "00-00-00-00-44-07-20-41"   string
System.BitConverter.ToString(System.BitConverter.GetBytes(c))   "00-40-9A-81-3C-07-20-41"   string

inside application:

System.BitConverter.ToString(System.BitConverter.GetBytes(a))   "00-00-00-00-97-F9-0D-C0"   string
System.BitConverter.ToString(System.BitConverter.GetBytes(b))   "00-00-00-00-44-07-20-41"   string
System.BitConverter.ToString(System.BitConverter.GetBytes(c))   "00-00-00-80-3C-07-20-41"   string

Edit 2:

As Alexei Levenkov points out, this issue is caused by a library that changes the FPU config.

For anyone who is curious what this meant for me: I was able to mitigate this issue for my particular piece of code, by making some assumptions about my input values and doing some preemptive values rounding which in turn made my calculations consistent.

Community
  • 1
  • 1
João Portela
  • 6,338
  • 7
  • 38
  • 51

2 Answers2

1

Use decimal if one wants preciscion such as needed in financial calculations.

Edit See:

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • I thought of that, but i really need the speed provided by these data types, the only problem is that I cannot understand the difference between the environments to make the code work at all times. – João Portela Oct 30 '12 at 16:49
  • @JoãoPortela the lack of precision is why doubles are used as such and that is the trade off you face. – ΩmegaMan Oct 30 '12 at 17:10
  • I am aware of that, but I can never be sure if my fix is correct if I don't know what the problem is in the first place. That's what this question was for: understanding the problem. – João Portela Oct 30 '12 at 17:26
  • I didn't know your second link. Thanks for that. – João Portela Oct 30 '12 at 17:44
  • Wow. I'm learning C#/VS and my background comes from PHP where data types is not a huge issue. It blows my mind that 'Double' is screwing with my data and I [have] to use 'Decimal'... –  Aug 22 '14 at 02:09
1

Your application may be doing something strange with configuration of FPU. I.e. using some random library for math which reconfigures precision...

Direct3d is possible suspect, see for example Pow implementation for double.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179