Given that a and b are positive, the calculation:
a:1.2345678e-30
b:5.5889944e-28
The real result is: 6.89999252062032 e-58
What do the following programs tell me?
#include <math.h>
#include <stdio.h>
int main(){
double a = 1.2345678e-30;
double b = 5.5889944e-28;
double c, d;
c = a * b;
d = exp(log(a)+log(b));
printf("a * b = %.20e \n", c);
printf("exp(log(a) + log(b)) = %.20e \n", d);
return 0;
};
a * b = 6.89999252062031977189e-58
exp(log(a) + log(b)) = 6.89999252062023345995e-58
The real result is: 6.89999252062032
import math a = 1.2345678e-30 b = 5.5889944e-28 print "----------math----------" print "a * b = %s" % repr(a*b) print "exp(log(a) + log(b)) = %s" % repr(math.exp(math.log(a)+math.log(b))) import mpmath mpmath.mp.dps = 20 a = mpmath.mpf(1.2345678e-30) b = mpmath.mpf(5.5889944e-28) print "---------mpmath---------" print "a * b = %s" % repr(a*b) print "exp(log(a) + log(b)) = %s" % repr(mpmath.exp(mpmath.log(a)+mpmath.log(b)))
----------math----------
a * b = 6.8999925206203198e-58
exp(log(a) + log(b)) = 6.8999925206202335e-58
---------mpmath---------
a * b = mpf('6.8999925206203201922352e-58')
exp(log(a) + log(b)) = mpf('6.8999925206203201919869e-58')
The real result is: 6.89999252062032
6.89999252062032e-58
6.899992520620233e-58
| Language | Results |
|---|---|
| c: |
6.89999252062031977189 e-58 6.89999252062023345995 e-58 |
| python (math): |
6.8999925206203198 e-58 6.8999925206202335 e-58 |
| python (mpmath): | 6.8999925206203201922352 e-58 6.8999925206203201919869 e-58 |
| scheme: |
6.89999252062032 e-58 6.899992520620233 e-58 |
| R |
6.89999252062032e-58 6.899992520620233e-58 |
| gcalculator and windows calculator: | 6.89999252062032 e-58 |
| the real result: | 6.89999252062032 e-58 |