科学计算器幂与根:square(a,b)=a^b、sqrt(a,b)=a^(1/b) 多参数用法
# 科学计算器幂与根:square(a,b)=a^b、sqrt(a,b)=a^(1/b) 多参数用法
> 接口 免费科学计算器(apiCode 1699)· 接入点 科学计算器(1699-1)· 免费 · POST/GET · 返回 JSON · 适合 学生 / 开发者 / 工程计算 · 阅读约 4 分钟
## 核心要点
- `square`(幂次方)= `a^b`:`num` 传「底数,指数」两个值,如 `square(2,3)=8`。
- `sqrt`(开根)= `a^(1/b)`:`num` 传「被开方数,根次数」,如 `sqrt(2,9)=2^(1/9)≈1.08006`(9 次根号下 2)。
- ⚠️ **`sqrt` 必须传 2 个参数**:单参数 `sqrt(9)` 会退化返回 `9`(原值),不是 3。
## Why:为什么幂和根容易写错
文档对 `square`/`sqrt` 只写了「幂次方 / 开根」,没说要几个参数、参数顺序。实测发现:**这两个都是双参数函数**,而且 `sqrt` 单参数时不报错却返回原值(退化行为),最容易让人误以为「接口算错了」。本篇用实测把参数顺序和坑点讲死。
## What:接口速览
| 项目 | 说明 |
|------|------|
| 请求地址 | `https://route.showapi.com/1699-1?appKey={your_appKey}` |
| 相关操作名 | `square`(幂次方)、`sqrt`(开根) |
| 计费 | 免费,[使用档次限制](https://www.showapi.com/free-api) |
## How:正确写法
### 幂次方 square(a, b) = a^b
```python
import requests
APP_KEY = "YOUR_APPKEY"
def calc(num, op):
r = requests.post(
"https://route.showapi.com/1699-1",
params={"appKey": APP_KEY},
data={"num": num, "operation": op, "rad_or_ang": ""},
timeout=10
)
b = r.json()["showapi_res_body"]
return b["result"] if str(b.get("ret_code")) == "0" else f"失败: {b.get('remark')}"
print(calc("2,3", "square")) # 8.0 (2^3)
print(calc("10,2", "square")) # 100.0 (10^2)
```
### 开根 sqrt(a, b) = a^(1/b)(b 次根号下 a)
```bash
# 9 次根号下 2 ≈ 1.08006
curl -X POST "https://route.showapi.com/1699-1?appKey=YOUR_APPKEY" \
-H "content-type: application/x-www-form-urlencoded" \
-d "num=2,9&operation=sqrt&rad_or_ang="
```
### Node.js
```javascript
const APP_KEY = "YOUR_APPKEY";
const body = new URLSearchParams({ num: "2,9", operation: "sqrt", rad_or_ang: "" });
fetch(`https://route.showapi.com/1699-1?appKey=${APP_KEY}`, {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" }, body
}).then(r => r.json()).then(res => {
const b = res.showapi_res_body;
console.log(String(b.ret_code) === "0" ? b.result : b.remark);
});
```
## 返回示例与解析
| 调用 | 真实结果 | 数学含义 |
|------|---------|---------|
| `square(2,3)` | `8.0` | 2³ = 8 |
| `square(10,2)` | `100.0` | 10² = 100 |
| `sqrt(2,9)` | `1.08006` | 2^(1/9),9 次根号下 2 |
| `sqrt(2,4)` | `1.189207` | 2^(1/4),4 次根号下 2 |
| `sqrt(9)`(单参) | `9.0` ⚠️ | **退化:返回原值,不是 3** |
## 进阶 / 边界
- **`sqrt` 单参数退化**:`sqrt(9)` 返回 `9.0`,既不报错也不开方。务必传 2 个参数。这是官方文档未说明的坑,已列入[已知问题](https://www.showapi.com/guides/calculator-known-issues-1699)。
- **想算平方根 `√x`**:写成 `sqrt(x,2)`,如 `sqrt(9,2)=3.0`。
- **参数顺序是「值,次数」**:`sqrt(被开方数, 根次数)`,别写反。
## FAQ
**Q1:怎么算平方根 √9?**
写 `num=9,2&operation=sqrt`,结果 `3.0`。
**Q2:为什么 `sqrt(9)` 返回 9?**
单参数时接口退化返回原值。`sqrt` 需要「被开方数,根次数」两个参数。
**Q3:`square` 单参数会怎样?**
单参数时指数默认按 1 处理,如 `square(3)=3.0`(即 3¹),也得不到平方,记得传两个参数。
**Q4:`sqrt(2,9)` 这个 1.08 怎么理解?**
它是「9 次根号下 2」= 2^(1/9)≈1.08006,不是 2/9。
**Q5:能算分数指数吗?**
`num` 支持小数,如 `num=2,0.5&operation=square` 即 2^0.5=√2。
## 相关能力 / 下一步阅读
- [科学计算器已知问题与使用避坑](https://www.showapi.com/guides/calculator-known-issues-1699) —— `sqrt` 单参数退化等坑点汇总
- [科学计算器操作名速查](https://www.showapi.com/guides/calculator-operations-reference-1699) —— 32 个操作名全表
- [科学计算器多参数公式](https://www.showapi.com/guides/calculator-batch-formulas-1699) —— `num` 逗号分隔多参数
- **本系列共 11 篇**:查看[科学计算器开发指南总目录](https://www.showapi.com/guides/calculator-guides-1699)