是否有可能获得带小数的模数 [英] Is it possible to get the modulo with decimals

查看:74
本文介绍了是否有可能获得带小数的模数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在方案中编译(模 10.5 5)时.它产生一个错误.在过程模中:位置 2 中的类型参数错误:10.5.

When i compile (modulo 10.5 5) in scheme. It produces an error. In procedure modulo: Wrong type argument in position 2: 10.5.

如果数字 10.5 可以被 5 整除,我如何检查这个和值.

How can i check this and the value if the number 10.5 is divisible by 5.

谢谢

推荐答案

数学函数 modulo 仅在整数上定义.将其应用于浮点数是没有意义的.

The mathematical function modulo is only defined on integers. It does not make sense to apply it to floating-point numbers.

正如在下面的评论中所讨论的,这是一个返回 #t#f 的谓词,用于确定除数 d 将一个数 n 无余数地整除:

As discussed in the comments below, here is a predicate returning either #t or #f that determines if a divisor d evenly divides a number n with no remainder:

(define (divides? n d)
  (let ((q (floor (/ n d))))
    (zero? (- n (* q d)))))

请注意,由于浮点近似,这可能会失败.

Note that this may fail due to floating-point approximation.

这篇关于是否有可能获得带小数的模数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆