摘要 回复网友来信,接前一节本节再谈多项式的错误计算。
例1. 计算
若在Visual Studio 2010中用C#编程计算:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{ static void Main()
{ long part1 = 946495 * (long)Math.Pow(7890, 9);
long part2 = -7467850000 * (long)Math.Pow(7890, 8);
long part3 = 35110000 * (long)Math.Pow(7890, 7);
long part4 = 3945000 * (long)Math.Pow(7890, 6);
long part5 = -133 * (long)Math.Pow(7890, 2);
double part6 = 8279529409.27;
long result = part1 + part2 + part3 + part4 + part5 + (long)part6;
Console.WriteLine(result);
}
}
则运行后输出 -9223372036854775699 . 它有19位整数。若将上面的 long 全部换成 double, 则输出为 -2.12455381201685E+25 .
若在线运行Fortran程序:
program calculate_expression
implicit none
real(kind=8), parameter :: base = 7890, exp1 = 9.0, exp2 = 8, exp3 = 7, exp4 = 6, exp5 = 2
real(kind=8), parameter :: c1 = 946495.0, c2 = -7467850000.0, c3 = 35110000.0, c4 = 3945000.0, c5 = -133, c6 = 8279529409.27
real(kind=8) :: result
result = (c1 * base**exp1) + &
(c2 * base**exp2) + &
(c3 * base**exp3) + &
(c4 * base**exp4) + &
(c5 * base**exp5) + &
c6
print *, result
end program calculate_expression
则运行后输出为 -3.6043526024768465E+033 .
然而,事实上,准确值是 109.27(ISRealsoft 提供)。二者均是错误结果,并且不相同。