是否存在int()轮次(而不是截断)的编程语言?

时间:2018-01-11 08:31:02

标签: casting integer rounding

我最近参与了关于在计算机科学考试中使用伪代码的讨论。有一个问题是使用整数除法。我说一个DIV b INT(a / b)相同,但是另一个贡献者说INT()的效果取决于语言的实现和它有时可能会结束。

我的理解(和36年的经验)是int()总是截断 - 即向下舍入。您是否了解任何编程语言,但设计并非如此,例如:将int(1.7)给予2?

1 个答案:

答案 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 that x is truncated to the closest integer value not greater than x. E.g., int(1.7) will return 1.

相关问题