我最近参与了关于在计算机科学考试中使用伪代码的讨论。有一个问题是使用整数除法。我说一个DIV b 与 INT(a / b)相同,但是另一个贡献者说INT()的效果取决于语言的实现和它有时可能会结束。
我的理解(和36年的经验)是int()总是截断 - 即向下舍入。您是否了解任何编程语言,但设计并非如此,例如:将int(1.7)给予2?
答案 0 :(得分:0)
I don't think this question could be formally answered for the simple reason that on any give day a developer may come out with a hipstercool new language where int
rounds instead of truncating.
Having said that, in any common programming language I've ever used or even heard about, int
truncates. To be on the safe side, when designing such a question, you could just explicitly state what int
means in its context. E.g.:
int(x)
means thatx
is truncated to the closest integer value not greater thanx
. E.g.,int(1.7)
will return1
.