【发布时间】:2015-10-09 20:53:08
【问题描述】:
如果它大于或等于 0,我想将它四舍五入到更大的数字,如果它小于 0,我想将它四舍五入到之前的数字。例如:如果数字是 2.5,则显示 3,如果数字是 -2.5,则显示 -3。我该怎么写这个? 我写道:
let round x = if (x >= 0) then int_of_float x
else int_of_float ( x -. 1.0);;
或
let round x = if ( x>=0) then truncate (x +. 0.5)
else truncate ( x -. 0.5);;
对于这两者,它给了我同样的错误:
Error: This expression has type int but an expression was expected of type
float
我应该怎么写?
【问题讨论】:
-
多年后,有人偶然发现了它^ ^ 错误消息来自与
0的比较,而不是0.0或0.。