当我在幂函数中使用负浮点数时出现错误

时间:2019-05-04 01:44:39

标签: floating-point ocaml pow

对于我的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);;

0 个答案:

没有答案