• pow()函数的精度问题


    妈蛋这个坑了我大半个小时都想不出个原因。。后来看到pow的定义才想起,数据类型很重要啊。。


    1.底数用常量,指数用整型

     1 #include <stdio.h>
     2 #include <math.h>
     3 int main()
     4 {
     5     int i,j;
     6     for(i=0;i<9;i++)
     7     {
     8         j=pow(10,i);
     9         printf("%d
    ",j);
    10     }
    11     return 0;
    12 }

    运行结果:

    1
    10
    99
    1000
    9999
    100000
    1000000
    9999999
    99999999

    答案不是1000下去而是出现了9999999,一开始百撕不得其姐。。

    看到定义后改用如下

    2.底数用常量,指数用double

     1 #include <stdio.h>
     2 #include <math.h>
     3 int main()
     4 {
     5     double i,j;
     6     for(i=0;i<9;i++)
     7     {
     8         j=pow(10,i);
     9         printf("%.0lf
    ",j);
    10     }
    11     return 0;
    12 }

    运行结果:

    1
    10
    100
    1000
    10000
    100000
    1000000
    10000000
    100000000

    改用double就没事了

    那么指数用常量呢

    3.底数用常量,指数用常量

    1 #include <stdio.h>
    2 #include <math.h>
    3 int main()
    4 {
    5     double i;
    6     i=pow(10,3);printf("%.0lf
    ",i);
    7     i=pow(10,4);printf("%.0lf
    ",i);
    8     return 0;
    9 }

    运行结果:

    1000
    10000

    可见,指数用常量也是没问题的


    经过n次测试,原因找出来了

    再看另一组数据:

    1.指数用int,pow值赋给int,结果错误

     1 #include <stdio.h>
     2 #include <math.h>
     3 int main()
     4 {
     5     double d;
     6     int p,i=9;
     7     p=pow(10,i);
     8     printf("%d
    ",p);
     9     return 0;
    10 }

    运行结果:

    999999999

    2.指数用常量,pow值赋给int,结果正确

     1 #include <stdio.h>
     2 #include <math.h>
     3 int main()
     4 {
     5     double d;
     6     int p,i=9;
     7     p=pow(10,9);
     8     printf("%d
    ",p);
     9     return 0;
    10 }

    运行结果:

    1000000000

    3.指数用常量,pow值赋给double,结果正确

    1 #include <stdio.h>
    2 #include <math.h>
    3 int main()
    4 {
    5     double d,p;
    6     p=pow(10,9);
    7     printf("%.0lf
    ",p);
    8     return 0;
    9 }

    运行结果:

    1000000000

    4.指数用int,pow值赋给double,结果正确

     1 #include <stdio.h>
     2 #include <math.h>
     3 int main()
     4 {
     5     double d,p;
     6     int i=9;
     7     p=pow(10,i);
     8     printf("%.0lf
    ",p);
     9     return 0;
    10 }

    运行结果:

    1000000000

    5.指数用double,pow值赋给int,结果错误

     1 #include <stdio.h>
     2 #include <math.h>
     3 int main()
     4 {
     5     double d,i=9;
     6     int p;
     7     p=pow(10,i);
     8     printf("%d
    ",p);
     9     return 0;
    10 }

    运行结果:

    999999999


    从数据看出

    pow赋值给int的时候:指数用常量,结果正确;指数用int、double,结果错误

    pow赋值给double的时候,指数用常量、int、double,结果都正确


    结论,指数用什么类型无关紧要,重要的是pow的值要赋给一个double型变量。

  • 相关阅读:
    HDU 3853 LOOPS:期望dp【网格型】
    SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式
    BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】
    BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】
    BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】
    codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
    poj1964最大子矩阵 (单调栈加枚举)
    poj3111 选取物品(二分+贪心)
    codeforces-777E Hanoi Factory (栈+贪心)
    poj3040 发工资(贪心)
  • 原文地址:https://www.cnblogs.com/someblue/p/3397715.html
Copyright © 2020-2023  润新知