C++双精度为 1079574528 或 0
C++ double is either 1079574528 or 0
我的代码如下所示:
// in Player.cpp
Player::Player() {
hp = 100;
pos = vec(50, 50);
printf("created player with %d hp on %d:%dn", hp, pos.x, pos.y);
}
// in Game.h
Player *player;
// in Game.cpp
player = new Player();
并且输出始终是"创建播放器,在 1079574528:0 上具有 0 hp"但是,当游戏运行时,x 和 y 位置是正确的。
您正在获得输出,因为您在 printf()
中使用 %d
作为双精度。您应该使用%f
如果你使用%f
,你会得到正确的输出(假设pos = vec(50, 50);
不会导致任何问题)。
另外,由于您使用的是c ++,因此您可以使用
std::cout << "created player with " << hp << " hp on " << pos.x << " : " << pos.y << std::endl ;
由于您是用C++编写的,请使用C++。
Player::Player() {
hp = 100;
pos = vec(50, 50);
std::cout << "created player with hp=" << hp << " on X:" << pos.x << ",Y:" << pos.y << std::endl;
}
相关文章:
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 在 C++ 中将双精度变量写入二进制文件
- 如何从字符串转换为双精度*
- 为什么我的数组双精度函数不起作用?
- 高精度双精度的 Sprintf 格式化问题
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 将指针数组分配给双精度
- C++如何将字符串逐行转换为双精度
- C++双精度为 1079574528 或 0