带有浮点数和双精度数的数字很小的数

问题描述 投票:0回答:1

我编写此代码只是为了查看使用浮点数和双精度数时精度值的差异。基本上,我正在尝试计算减少的Plancks常数的值。精简的Plancks const = Plancks const /2π。

#include<stdio.h>
#include<math.h>

const float H = 6.62607015e-34;
const float Pi = 3.1415926;
int main()
{
    float a = H / (2 * Pi);
    double b = H / (2 * Pi);
    printf("float is %ld \ndouble is %ld", a, b);
    return 0;
}

但是输出完全没有意义。我不知道我在做什么错。

float is 536870912
double is 954303911

我什至试图将常量的数据类型更改为double,这并没有更好。

float is 0
double is 954303911

我减少了常数中的有效数字,但没有区别。有人可以告诉我我在做什么错吗?

c++ c double physics
1个回答
0
投票
printf("float is %ld \ndouble is %ld", a, b);

%d是整数的格式说明符,不是浮点数。改用%f,它将对double有效,并且由于自动参数提升,%f也对float有效。

大多数编译器会警告此类错误。确保在编译器中启用了警告。对于GCC,请使用-Wall -Wextra

© www.soinside.com 2019 - 2024. All rights reserved.