0Pricing
C Academy · 课时

幂与根

使用 pow、sqrt 等函数。

幂与根 是 CoddyKit 上的免费 C Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C Academy 课程共包含 4 节课。

幂与根

math.h 中最实用的两个工具是用于求幂的 pow,以及用于求平方根的 sqrt。

两者都使用 double 值,并返回 double 结果。

pow 函数

pow(base, exponent) 用于计算底数的 exponent 次幂。

例如,pow(2, 3) 表示 2 x 2 x 2,结果为 8。

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

int main(void) {
    double result = pow(2.0, 3.0);
    printf("2^3 = %.0f\n", result);
    return 0;
}

使用 pow 求平方

要计算一个数的平方,请将它提升到 2 次方。

这里通过计算正方形房间边长的平方,求出房间的面积。

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

int main(void) {
    double side = 5.0;
    double area = pow(side, 2.0);
    printf("area = %.0f\n", area);
    return 0;
}

分数指数

pow 也接受分数指数。提升到 0.5 次方等同于开平方。

因此,pow(9.0, 0.5) 等于 3.0。

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

int main(void) {
    double r = pow(9.0, 0.5);
    printf("9^0.5 = %.1f\n", r);
    return 0;
}

sqrt 函数

sqrt(x) 返回 x 的平方根,也就是与自身相乘后得到 x 的数。

如果只需要求平方根,使用它比使用 pow(x, 0.5) 更清晰也更快。

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

int main(void) {
    double r = sqrt(144.0);
    printf("sqrt(144) = %.0f\n", r);
    return 0;
}

向 sqrt 传入负数

负数的平方根不是实数。

调用 sqrt(-1.0) 会返回一个称为 NaN(非数)的特殊值。请始终检查输入是否为零或正数。

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

int main(void) {
    double r = sqrt(-4.0);
    printf("result = %f\n", r);
    return 0;
}

使用 cbrt 求立方根

对于立方根,math.h 提供了 cbrt。

与 sqrt 不同,cbrt 可以正常处理负数,因为每个实数都有一个实数立方根。

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

int main(void) {
    printf("%.0f\n", cbrt(27.0));
    printf("%.0f\n", cbrt(-8.0));
    return 0;
}

勾股定理

幂和根在几何学中可以很好地结合使用。直角三角形最长边的长度,等于另外两条边的平方和的平方根。

这里计算一个 3-4-5 三角形的斜边。

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

int main(void) {
    double a = 3.0, b = 4.0;
    double c = sqrt(pow(a, 2.0) + pow(b, 2.0));
    printf("hypotenuse = %.1f\n", c);
    return 0;
}

hypot 辅助函数

上面的模式非常常见,因此 math.h 提供了 hypot(a, b),可以直接返回斜边长度。

对于非常大或非常小的数,它比手动计算平方和再开平方更准确。

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

int main(void) {
    double c = hypot(3.0, 4.0);
    printf("hypot = %.1f\n", c);
    return 0;
}

选择合适的工具

快速指南:

  • 平方根:使用 sqrt
  • 立方根:使用 cbrt
  • 任意次方:使用 pow
  • 斜边:使用 hypot

如果有专用函数,请选择它。这样代码更清晰,结果通常也更精确。

练习程序

这个程序先将一个数提升到指定次方,然后对结果开平方,展示这些函数如何串联使用。

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

int main(void) {
    double p = pow(10.0, 4.0);
    double r = sqrt(p);
    printf("%.0f -> %.0f\n", p, r);
    return 0;
}

快速检查

想一想每个函数会返回什么。

回顾

您学会了使用 pow 求幂、使用 sqrt 求平方根、使用 cbrt 求立方根,以及使用 hypot 求斜边。

请记住,对负数使用 sqrt 会得到 NaN。接下来我们将学习三角函数和对数。

常见问题解答

「幂与根」课时是免费的吗?

是的 — 「幂与根」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C Academy 课程的其余内容,请升级到 CoddyKit PRO。 C Academy 课程共包含 4 节课。

「幂与根」这节课中我会学到什么?

使用 pow、sqrt 等函数。 你通过在浏览器中直接运行的动手代码来练习 C Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 C Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 C Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「幂与根」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 C Academy 课中编写并运行代码吗?

能。每节 C Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 包含 math.h
  2. 幂与根
  3. 三角函数与对数
  4. 舍入与绝对值
← 返回 C Academy