【问题标题】:How to convert real to exact integer in Racket?如何在 Racket 中将实数转换为精确整数?
【发布时间】:2016-05-24 16:36:18
【问题描述】:

如何将20.2 形式的值转换为(random ...) 接受的值?

我试过这些:

;; x defined by some earlier maths and of form 20.2

(random (round x))
(random (floor x))

但两者都返回:

random: contract violation
expected: (or/c (integer-in 1 4294967087) pseudo-random-generator?)
given: 20.0

【问题讨论】:

    标签: scheme racket


    【解决方案1】:

    这篇文章似乎回答了 Scheme 的问题: http://computer-programming-forum.com/40-scheme/674db7a1706960d5.htm

    所以用这样的代码我得到了我想要的结果:

    (random (inexact->exact (round x)))
    (random (inexact->exact (floor x)))
    

    这是最简单的方法吗?

    【讨论】:

    • 在球拍中,球拍/数学中的exact-roundexact-floor 函数可以做到这一点。
    • 谢谢@AlexKnauth!
    【解决方案2】:

    这些也有效,根据documentation,它们只是您方法的简写:

    (random (exact-round x))
    (random (exact-floor x))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 2018-02-03
      • 2016-01-01
      • 2013-05-13
      • 2014-04-18
      • 2010-12-31
      • 1970-01-01
      相关资源
      最近更新 更多