floating-point - 在幂函数中使用负浮点数时出现错误
问题描述
对于我的float_pow
函数,当我使用负 x 进行测试时,例如使用代码float_pow (-2.5) 2
,我收到以下错误消息:
“错误:此表达式的类型为 float,但表达式应为 int 类型”。
但如果我这样做float_pow (-.2.5) 2
了,那么代码实际上可以工作。如何更改我的代码,以便我不需要在破折号后加上句点?
let rec float_pow x n =
if n < 0 then
0.
else if n = 0 then
1.
else
x *. float_pow x (n - 1);;
解决方案
推荐阅读
- android - 应用程序关闭时工作管理器不工作?
- php - JQuery/Ajax html 只加载一次
- python - 如何将数据帧中的多个元组列表组合到一个字典中?
- android - 如果应用程序从最近的菜单中被杀死,如何强制停止任何插件
- java - java.sql.SQLException:字段“participant_one_fk_id”没有默认值
- javascript - React :: 上下滚动时部分变白
- reactjs - React-card-flip,通过数据映射并一次翻转一张卡片
- redis - Redisson 将 ArrayList 或 RList 放入 RMap 中。会发生什么?
- python - 围绕枢轴 Python 绘制旋转图像
- c# - 如何在 C# 中使用 XDocument 在根元素中生成具有多个命名空间的 XML 文件?