无法统一 IEEEReal.rounding_mode 与真实

Can't unify IEEEReal.rounding_mode with real

当我尝试像这样将 real 转换为 int 时,为什么会出现错误?

fun stuff a  =
  Real.toInt a

错误:

Error-Can't unify IEEEReal.rounding_mode with real (Different type constructors) Found near stuff
(0.0)

也许您忽略了 Real.toInt : IEEEReal.rounding_mode -> real -> int,即采用额外的参数来指定 how rounding is made。如果您不想指定此额外参数,例如喜欢

fun round x = Real.toInt IEEEReal.TO_NEAREST x

您可以使用其中一种功能

  • Real.floor : real -> int 假定 IEEEReal.TO_NEGINF,
  • Real.ceil : real -> int 假定 IEEEReal.TO_POSINF,
  • Real.trunc : real -> int 假定 IEEEReal.TO_ZERO,或
  • Real.round : real -> int 假定 IEEEReal.TO_NEAREST.

有趣的事实:函数 Real.toIntactually defined in terms of these four functions