V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
leopoe
V2EX  ›  C

[C 语言] 输入 345.78,乘以 100 后 ,输出 34577

  •  
  •   leopoe · 2017-10-21 11:53:00 +08:00 · 2749 次点击
    这是一个创建于 2378 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是代码
    代码内容
    这是运行的结果,输出了输入的值和乘 100 后的值
    运行结果
    麻烦各位大佬帮忙看看是啥原因!

    15 条回复    2017-10-22 22:01:53 +08:00
    thetydead
        1
    thetydead  
       2017-10-21 12:29:56 +08:00   ❤️ 1
    浮点数存储的问题,容易丢失精度。所以运算有专门的 decimal 类型
    Kirscheis
        2
    Kirscheis  
       2017-10-21 13:52:44 +08:00 via Android   ❤️ 1
    这是平台相关的。如果你用现代一点的平台和编译器的话,基本上会输出 34578 的。
    强制转换为 int 一般会采取向下取整,所以得到的结果可能是 34577 或者 34578,取决于你的平台实现。
    msg7086
        3
    msg7086  
       2017-10-21 14:54:02 +08:00   ❤️ 1
    345.78 不是一个有限位数的小数,因此不可能在有限存储空间里直接存储这个数的精确值。
    一个近似结果是 345.779999999999972715158946812152862548828125。
    乘上 100 以后取整,34577 的结果并不算意外。

    pry(main)> '%f' % a
    => "345.780000"
    pry(main)> '%.10f' % a
    => "345.7800000000"
    pry(main)> '%.15f' % a
    => "345.779999999999973"
    pry(main)> '%.45f' % a
    => "345.779999999999972715158946812152862548828125000"
    leopoe
        4
    leopoe  
    OP
       2017-10-21 15:15:06 +08:00
    emmm ……知道了,十分感谢!
    leopoe
        5
    leopoe  
    OP
       2017-10-21 15:16:16 +08:00
    @thetydead emmm ……好的,谢谢!
    leopoe
        6
    leopoe  
    OP
       2017-10-21 15:17:55 +08:00
    @Kirscheis 谢谢你的解答
    leopoe
        7
    leopoe  
    OP
       2017-10-21 15:18:12 +08:00
    @msg7086 谢谢你的解答
    zmj1316
        8
    zmj1316  
       2017-10-21 17:14:46 +08:00 via Android
    转整形之前把浮点 + 0.5 就可以四舍五入了
    AndiMckee
        9
    AndiMckee  
       2017-10-21 17:25:48 +08:00 via iPhone   ❤️ 1
    别用 void 定义主函数啊,别用谭浩强的书啊
    应该是 int main(void)
    yanchao7511461
        10
    yanchao7511461  
       2017-10-21 17:28:09 +08:00
    浮点数啊 兄弟
    gnaggnoyil
        11
    gnaggnoyil  
       2017-10-22 07:55:24 +08:00
    @thetydead 麻烦审题,LZ 说的是 C 语言,C 语言哪里来的 Decimal 类型?

    @Kirscheis C 中浮点数至整数的转换行为是固定的. ISO/IEC 9899:2011 6.3.1.4 Real floating and integer:

    When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded(i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
    spacewander
        12
    spacewander  
       2017-10-22 08:58:15 +08:00
    貌似跟编译器的因素还是有关?
    我这边输出的是 34578.

    下面是输出:
    ```
    345.77999999999997271516 // %.20f
    345.7799999999999727151589468121528625488281 // %.40f
    345.779999999999972715158946812152862548828125000000000000000000 // %.60f
    34578 // %d
    ```

    编译器是 gcc 6.3 和 clang 4.0.0,这两编译器输出结果是一样的。
    hantsy
        13
    hantsy  
       2017-10-22 15:50:55 +08:00
    @AndiMckee 读书的时候看谭的书,感觉好牛 B,发行量巨大。后来工作之后发现真是害死人,谭的书很多内容也是到处东拼西凑的,纯粹是满足国内的教学需要。
    Kirscheis
        14
    Kirscheis  
       2017-10-22 18:17:33 +08:00 via Android
    @gnaggnoyil 我说了,一般默认是向下取整。。然而你不妨多做做实验就知道事实上是怎样的了,尤其可以看看优化之后的汇编
    zhx1991
        15
    zhx1991  
       2017-10-22 22:01:53 +08:00
    原理是十进制是有限小数转换成二进制是无限小数
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5303 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:22 · PVG 16:22 · LAX 01:22 · JFK 04:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.