gpt4 book ai didi

c - 数字的精度,乘以和除以100

转载 作者:太空宇宙 更新时间:2023-11-04 01:07:54 28 4
gpt4 key购买 nike

我有以下测试用例

#include <stdio.h>

int main() {

double x = 3.987;

printf("x = %lf\n", x);

printf("(double) (long) (x) = %lf\n", (double) (long) (x));

printf("(x*100)/100 = %lf\n", (x*100)/100);

printf("(double) (long) (x*100)/100 = %lf\n", (double) (long) (x*100)/100);

printf("(double) (long) (x*10)/10 = %lf\n", (double) (long) (x*10)/10);

return 0;
}

输出是:

x = 3.987000
(double) (long) (x) = 3.000000
(x*100)/100 = 3.987000
(double) (long) (x*100)/100 = 3.980000
(double) (long) (x*10)/10 = 3.900000

我觉得乘以100和除以100会相互抵消?但它实际上降低了精度。这究竟是如何工作的?

最佳答案

在某些地方,您要转换为“long”,它是整数类型。因此,例如在最后一种情况下,您将 3.987 乘以 10 得到 39.87。然后,您将其转换为“长”,因此它变为 39。将其除以 10 后得到 3.9。

你想通过(long)实现什么?

关于c - 数字的精度,乘以和除以100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350137/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com