纳瓦尔艺术免安装绿色中文版
2.82G · 2025-10-24
float、double 实数型(Floating-Point Types),也叫浮点型,用于存储带有小数部分的数字。
float: 单精度浮点数。通常占用4个字节(32位),精度较低(约7位有效数字),表示范围也较小。double: 双精度浮点数。通常占用8个字节(64位),是C++中默认的浮点类型。它的精度更高(约15-17位有效数字),表示范围更大。在信息学竞赛中,几乎总是使用 double,因为 float 的精度通常不够。double pi;double pi = 3.1415926535;double area = pi * r * r;浮点数在内存中的存储比整数复杂,遵循IEEE 754标准,分为三个部分:符号位(Sign)、指数位(Exponent)和尾数位(Mantissa/Fraction)。
double (8 字节 / 64位) 内存结构
== 直接比较两个浮点数。应该比较它们的差的绝对值是否小于一个极小的数(称为epsilon,如 1e-7)。#include <iostream>
#include <iomanip> // 用于控制输出格式,如 setprecision
#include <cmath> // 用于数学函数,如 fabs (取绝对值)
int main() {
// 1. 声明和初始化
float f_pi = 3.1415926f; // 'f'后缀表示 float
double d_pi = 3.141592653589793;
std::cout << std::fixed << std::setprecision(10); // 设置固定小数点格式,并显示10位小数
std::cout << "Float PI: " << f_pi << std::endl;
std::cout << "Double PI: " << d_pi << std::endl;
// 2. 演示精度问题
double a = 0.1;
double b = 0.2;
double c = 0.3;
if (a + b == c) {
std::cout << "0.1 + 0.2 == 0.3" << std::endl;
} else {
std::cout << "0.1 + 0.2 != 0.3" << std::endl; // 这行会被输出
std::cout << "a + b = " << a + b << std::endl;
}
// 3. 正确的比较方式
const double EPSILON = 1e-9; // 定义一个极小数
if (std::fabs((a + b) - c) < EPSILON) {
std::cout << "abs((a+b)-c) is small enough, they are considered equal." << std::endl;
}
return 0;
}
double: 在竞赛中,除非内存限制极其严格,否则始终使用 double 来避免不必要的精度问题。long double: 精度更高的浮点类型(通常10或16字节),在极少数需要超高精度的题目中使用。e 或 E 表示科学计数法,例如 double light_speed = 2.99792458e8; // 表示 2.99... × 10⁸练习1: 计算圆的面积
题目: 输入一个双精度浮点数 r 代表圆的半径,计算并输出圆的面积。π 取 3.1415926535。
答案:
#include <iostream>
#include <iomanip>
int main() {
double r;
const double PI = 3.1415926535;
std::cin >> r;
double area = PI * r * r;
std::cout << std::fixed << std::setprecision(7) << area << std::endl;
return 0;
}
练习2: 华氏度转摄氏度
题目: 输入一个华氏温度 F,根据公式 C = 5/9 * (F-32) 计算并输出摄氏温度 C。
答案:
#include <iostream>
#include <iomanip>
int main() {
double fahrenheit;
std::cin >> fahrenheit;
// 注意:5.0/9.0 确保是浮点数除法
double celsius = 5.0 / 9.0 * (fahrenheit - 32);
std::cout << std::fixed << std::setprecision(4) << celsius << std::endl;
return 0;
}
练习3: 计算平均分
题目: 输入三个浮点数,代表三门课的成绩,计算并输出平均分。
答案:
#include <iostream>
#include <iomanip>
int main() {
double score1, score2, score3;
std::cin >> score1 >> score2 >> score3;
double average = (score1 + score2 + score3) / 3.0;
std::cout << std::fixed << std::setprecision(2) << average << std::endl;
return 0;
}
练习4: 两点间距离
题目: 输入四个浮点数 x1, y1, x2, y2,代表平面上两个点的坐标,计算并输出这两点间的欧几里得距离。
答案:
#include <iostream>
#include <iomanip>
#include <cmath> // for sqrt
int main() {
double x1, y1, x2, y2;
std::cin >> x1 >> y1 >> x2 >> y2;
double dx = x1 - x2;
double dy = y1 - y2;
double distance = std::sqrt(dx * dx + dy * dy);
std::cout << std::fixed << std::setprecision(5) << distance << std::endl;
return 0;
}
练习5: 判断三角形
题目: 输入三个浮点数 a, b, c,判断它们是否能构成一个三角形。
答案: (提示:两边之和大于第三边,注意浮点数比较)
#include <iostream>
#include <cmath>
int main() {
double a, b, c;
std::cin >> a >> b >> c;
const double EPSILON = 1e-9;
if ((a + b > c + EPSILON) && (a + c > b + EPSILON) && (b + c > a + EPSILON)) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
return 0;
}
2025-10-24
网易《逆水寒》手游与宇树科技达成合作,虚拟世界将成机器人技术试验田
2025-10-24
谷歌间接承认 Tensor G5 芯片存在 GPU 问题,将推送更新优化 Pixel 10 系列手机