I understand that floating point calculations have accuracy issues and there are plenty of questions explaining why. My question is if I run the same calculation twice, can
This is not a full answer to your question, but here is an example demonstrating that double calculations in C# are non-deterministic. I don't know why, but seemingly unrelated code can apparently affect the outcome of a downstream double calculation.
Replace the contents of MainWindow.xaml.cs with this:
using System;
using System.Windows;
namespace WpfApplication1
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Content = FooConverter.Convert(new Point(950, 500), new Point(850, 500));
}
}
public static class FooConverter
{
public static string Convert(Point curIPJos, Point oppIJPos)
{
var ij = " Insulated Joint";
var deltaX = oppIJPos.X - curIPJos.X;
var deltaY = oppIJPos.Y - curIPJos.Y;
var teta = Math.Atan2(deltaY, deltaX);
string result;
if (-Math.PI / 4 <= teta && teta <= Math.PI / 4)
result = "Left" + ij;
else if (Math.PI / 4 < teta && teta <= Math.PI * 3 / 4)
result = "Top" + ij;
else if (Math.PI * 3 / 4 < teta && teta <= Math.PI || -Math.PI <= teta && teta <= -Math.PI * 3 / 4)
result = "Right" + ij;
else
result = "Bottom" + ij;
return result;
}
}
}
Set build configuration to "Release" and build, but do not run in Visual Studio.
Now add this line just before "string result":
string debug = teta.ToString();
Repeat steps 3 and 4.
This behavior was confirmed on a colleague's machine. Note that the window consistently shows "Right Insulated Joint" if any of the following are true: the exe is run from within Visual Studio, the exe was built using the Debug configuration, or "Prefer 32-bit" is unchecked in project properties.
It's quite difficult to figure out what's going on, since any attempt to observe the process appears to change the result.