FIRST PROBLEM
In C code I have this expression:
double completeExpression = x1 - h*exp(-lambda*t);
I have split it in two operations:
double value = h*exp(-lambda*t);
double subtraction = x1 - value;
The problem is that subtraction is different from completeExpression. What's the matter?
I have reproduced a strange results in my code with this lines:
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 & value = %1.4e",x1,value);
printf("\ncompleteExpression = %1.4e",completeExpression);
printf("\nsubtraction = %1.4e",subtraction);
Results:
x1 = 4.6285e-001 & value = 4.6285e-001
completeExpression = 8.2779e-017
subtraction = 5.5511e-017
SECOND PROBLEM:
I have to translate the completeExpression in Java, and I have returned always the bad result (subtraction) and not completeExpression value:
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) + "& value = " + String.format("%1.4e",x1) );
System.out.println("\ncompleteExpression = " + String.format("%1.4e",completeExpression));
System.out.println("\nsubtraction = " + String.format("%1.4e",subtraction));
#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.