title: C/C++ Arithmetic expression with double numbers - strange results - translating in java
body: <p>FIRST PROBLEM</p>
  
  <p>In C code I have this expression:</p>
  
  <pre><code>double completeExpression  = x1 - h*exp(-lambda*t);
  </code></pre>
  
  <p>I have split it in two operations:</p>
  
  <pre><code>double value = h*exp(-lambda*t);
  double subtraction = x1 - value;
  </code></pre>
  
  <p>The problem is that subtraction is different from completeExpression.
  What's the matter?</p>
  
  <p>I have reproduced a strange results in my code with this lines:</p>
  
  <pre><code>const double TOLERANCE = 1e-16;
  double h = 0.51152525298500628;
  double lambda =0.99999999999999978;
  double t=0.1;
  double x1 =0.4628471891711442 ;
  
  double completeExpression  = x1 - h*exp(-lambda*t);
  double value = h*exp(-lambda*t);
  double subtraction = x1 - value;
  
  printf("x1 = %1.4e &amp; value = %1.4e",x1,value);
  printf("\ncompleteExpression = %1.4e",completeExpression);
  printf("\nsubtraction = %1.4e",subtraction);
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>x1 = 4.6285e-001 &amp; value = 4.6285e-001
  completeExpression = 8.2779e-017
  subtraction = 5.5511e-017
  </code></pre>
  
  <hr>
  
  <p>SECOND PROBLEM:</p>
  
  <p>I have to translate the completeExpression in Java, and I have returned always the bad result (subtraction) and not completeExpression value:</p>
  
  <p>Code:</p>
  
  <pre><code> static double TOLERANCE = 1e-16;
     public static void main() {
  
          double h = 0.51152525298500628;
          double lambda =0.99999999999999978;
          double t=0.1;
          double x1 =0.4628471891711442 ;
  
          double completeExpression  = x1 - h*Math.exp(-lambda*t);
          double value = h*Math.exp(-lambda*t);
          double subtraction = x1 - value;
  
          System.out.println( "x1 = " + String.format("%1.4e", value) + "&amp; value = " + String.format("%1.4e",x1) );
          System.out.println("\ncompleteExpression = " + String.format("%1.4e",completeExpression));
          System.out.println("\nsubtraction = " + String.format("%1.4e",subtraction));
  </code></pre>
  
  <hr>
  
  <pre><code>#gcc --version
  
  My Gcc Version:
  $ gcc --version
  gcc.exe (GCC) 4.8.1
  Copyright (C) 2013 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  </code></pre>