#include <iostream>
#include <iomanip> // 用于控制浮点数输出格式
int main() {
// 定义浮点数变量
float a = 3.1415926f;
double b = 2.718281828459045;
// 输出浮点数,设置精度
std::cout << "Float value of a: " << std::fixed << std::setprecision(6) << a << std::endl;
std::cout << "Double value of b: " << std::fixed << std::setprecision(15) << b << std::endl;
// 浮点数运算
float sum = a + b;
std::cout << "Sum of a and b: " << std::fixed << std::setprecision(6) << sum << std::endl;
// 比较浮点数
if (std::abs(a - b) < 0.0001) {
std::cout << "a and b are approximately equal." << std::endl;
} else {
std::cout << "a and b are not equal." << std::endl;
}
return 0;
}
#include <iostream>
和 #include <iomanip>
分别用于输入输出和格式化输出。float a
和 double b
分别定义了一个单精度浮点数和双精度浮点数。std::fixed
和 std::setprecision
控制浮点数的输出格式和精度。0.0001
)来判断两个浮点数是否接近相等。如果你需要更多关于浮点数的内容或有其他问题,请告诉我!
上一篇:c++ string split
下一篇:c++ algorithm库
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站