gpt4 book ai didi

operator-overloading - 如何求两个 float 的平均值

转载 作者:行者123 更新时间:2023-12-04 08:22:07 24 4
gpt4 key购买 nike

我想得到2个 float 的平均值。我的整数变体函数

let int_average x y = (x + y) / 2

工作正常,但是当我尝试为 float 编写它时

let float_average x y = (x +. y) / 2.

由于错误而失败

This expression has type float but an expression was expected of type int

最佳答案

您忘记“ float ”除法运算符。 / 应该是 /.,就像 +.+ 的浮点变体:

let float_average x y = (x +. y) /. 2.

关于operator-overloading - 如何求两个 float 的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57151936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com