计算机的错误计算(一百零八)

摘要  回复网友来信,接前一节本节再谈多项式的错误计算。

例1.  计算

946495\times7890^9-7467850000\times7890^8+35110000\times7890^7+3945000\times7890^6-133\times7890^2+8279529409.27\,.

       若在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 提供)。二者均是错误结果,并且不相同。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值