← 從零開始學 C++
第 12 課,共 62 課 · 10 XP
數學:<cmath>
理論與題目說明
機器翻譯,尚未經母語人士審閱。
#include <cmath>
std::sqrt(16.0); // 4——平方根
std::pow(2, 10); // 1024——次方(結果是 double)
std::abs(-7); // 7——絕對值
std::round(2.5); // 3——四捨五入到最接近的值
std::floor(2.7); // 2——無條件捨去,std::ceil(2.1)——3,無條件進位
std::hypot(3, 4); // 5——斜邊:sqrt(3² + 4²)
std::max(3, 9); std::min(3, 9); // 來自 <algorithm>
浮點數只能近似地儲存:0.1 + 0.2 == 0.3 是假!請用容許誤差比較:
std::abs(a - b) < 1e-9。
題目。 讀入兩點的座標 x1 y1 x2 y2(整數)。顯示兩點間的距離,
到小數點後 2 位。