如何从浮点数中获取精确的整数?
我需要使用带有整数参数的随机函数,该参数是从理论上可以返回双精度的操作中获得的。所以我想为此目的使用一些铸造/圆形。我已经尝试过
(random (round 10.0))
(random (floor 10.0))
,但它会抛出异常,就像
random: expects argument of type <exact integer in [1, 4294967087] or pseudo-random-generator>; given 10.0
我使用 DrRacket 作为解释器一样。
I need to use random
function with integer argument that I get from operations that theoretically can return double. So I want use some cast/round for this purpose. I've tried
(random (round 10.0))
(random (floor 10.0))
But it throws exceptions like
random: expects argument of type <exact integer in [1, 4294967087] or pseudo-random-generator>; given 10.0
I use DrRacket as interpreter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
(random (floor->exact 10.0))
或(random (inexact->exact (round 10.0))))
您可以使用任何舍入 (地板/圆形/截断/天花板)。
此处讨论:http://web. mit.edu/scheme_v9.0.1/doc/mit-scheme-ref/Numerical-operations.html
(搜索“不精确->精确”)
Try
(random (floor->exact 10.0))
or(random (inexact->exact (round 10.0))))
You can use any of the roundings (floor/round/truncate/ceiling).
This is discussed here: http://web.mit.edu/scheme_v9.0.1/doc/mit-scheme-ref/Numerical-operations.html
(search for "inexact->exact")