//+------------------------------------------------------------------+
//| TestDoubleEq.mq5                                                 |
//| Tests the claim: "== on doubles in MQL5 allows ~1e-15 error".    |
//| Run as a script, read the Experts tab. Final line: VERDICT.      |
//| Values go through noinline-style function args and arrays so the |
//| compiler cannot constant-fold the comparisons.                   |
//+------------------------------------------------------------------+
#property script_show_inputs
#property strict

union DoubleBits { double d; ulong u; };

input int    RandomCases = 2000000;  // random pairs per ulp distance
input double BasePrice   = 1.12345;  // a typical price

int g_fail = 0;      // number of cases where == behaved with tolerance
int g_total = 0;

ulong  Bits(double x){ DoubleBits b; b.d = x; return b.u; }
double FromBits(ulong u){ DoubleBits b; b.u = u; return b.d; }

// returns result of == (value passed through params, not constants)
bool Eq(const double a, const double b){ return a == b; }

// Expectation: a==b true  <=>  bit patterns identical (for non-NaN, non +-0)
void Check(const string name, const double a, const double b)
{
   g_total++;
   bool eq   = Eq(a, b);
   bool same = (Bits(a) == Bits(b));
   if (eq != same)
   {
      g_fail++;
      if (g_fail <= 20)
         PrintFormat("MISMATCH %s: a=%.17g b=%.17g  ==:%s  bitsEqual:%s  absdiff=%.3e",
                     name, a, b, eq ? "true" : "false", same ? "true" : "false", MathAbs(a - b));
   }
}

void ReportCase(const string title, const double a, const double b)
{
   PrintFormat("%-34s a=%.17g b=%.17g | absdiff=%.3e | == %s | bits %s",
               title, a, b, MathAbs(a - b), Eq(a, b) ? "TRUE" : "false",
               Bits(a) == Bits(b) ? "same" : "differ");
   Check(title, a, b);
}

void OnStart()
{
   Print("=== MQL5 double == test, build ", __MQLBUILD__, " ===");

   // 1. Classic example from the MQL5 docs
   double first = 0.3, second = 3.0;
   double third = second - 2.7;
   ReportCase("docs: 0.3 vs 3.0-2.7", first, third);

   // 2. Typical price and neighbours at 1,2,3,4,8,16 ulp (all far below 1e-15 relative)
   double p = BasePrice;
   ulong  pb = Bits(p);
   ulong steps[] = {1, 2, 3, 4, 8, 16, 100};
   for (int i = 0; i < ArraySize(steps); i++)
      ReportCase("price vs +" + (string)steps[i] + " ulp", p, FromBits(pb + steps[i]));

   // 3. Explicit absolute offsets around 1e-15 (and below)
   double offs[] = {1e-17, 1e-16, 2.3e-16, 5e-16, 1e-15, 2e-15};
   for (int i = 0; i < ArraySize(offs); i++)
      ReportCase("price vs price+" + DoubleToString(offs[i], 17), p, p + offs[i]);

   // 4. Sweep: for many magnitudes, 1 ulp neighbours must be unequal
   double mags[] = {1e-10, 1e-5, 0.001, 0.1, 1.0, 1.1, 1.9, 10.0, 1234.5678, 1e6, 1e10, 1e15};
   for (int i = 0; i < ArraySize(mags); i++)
      ReportCase("mag " + DoubleToString(mags[i], 10) + " vs +1ulp", mags[i], FromBits(Bits(mags[i]) + 1));

   // 5. Random pairs: 0, 1, 2 ulp apart, compare == against bit equality
   MathSrand(12345);
   for (int k = 0; k < RandomCases; k++)
   {
      double v = (MathRand() + MathRand() / 32768.0 + 1.0) * 0.0001 * (1 + MathRand() % 20000);
      ulong  vb = Bits(v);
      Check("rand same",  v, FromBits(vb));
      Check("rand +1ulp", v, FromBits(vb + 1));
      Check("rand +2ulp", v, FromBits(vb + 2));
   }

   // 6. Known IEEE special cases (informational; not counted as tolerance)
   double pz = 0.0, nz = FromBits(0x8000000000000000);
   PrintFormat("+0.0 == -0.0 : %s (bits differ: %s)  <- IEEE says true, bitwise says different",
               Eq(pz, nz) ? "TRUE" : "false", Bits(pz) != Bits(nz) ? "yes" : "no");
   double nan = FromBits(0x7FF8000000000000);
   PrintFormat("NaN == NaN   : %s (bits equal: %s)  <- IEEE says false",
               Eq(nan, nan) ? "TRUE" : "false", Bits(nan) == Bits(nan) ? "yes" : "no");

   // 7. Verdict
   PrintFormat("Cases checked: %d, cases where == disagreed with bit equality: %d", g_total, g_fail);
   if (g_fail == 0)
      Print("VERDICT: assumption DISAPPROVED. == on doubles is exact (no ~1e-15 tolerance). "
            "Only difference vs bitwise: +0.0 == -0.0 is true and NaN != NaN.");
   else
      Print("VERDICT: assumption APPROVED. == treated different bit patterns as equal "
            "(a tolerance exists). See MISMATCH lines.");
}
