On Thu, Oct 14, 2021 at 11:52:11AM +1300, Greg Ewing wrote:
> Scratch that, it seems __trunc__ also returns an int, at least
> for floats. Not sure what the logic behind that is.
I'm not sure about the logic either, but it is documented as returning
an Integral:
"Truncates the Real x to the nearest Integral toward 0."
so the option is there for third-party types to return some integral
type apart from int. For the stdlib, the only Integral type we have is
int. So I think we have the following intended behaviour.
* Round a numeric (Real) value to an Integral value:
- round to nearest (ties to even): __round__
- round down (towards negative infinity): __floor__
- round up (towards positive infinity): __ceil__
- round towards zero: __trunc__
* Convert a numeric Integral value to an actual int: (intended for
indexing of sequences): __index__
* Convert any arbitrary value to an actual int: __int__
Does that seem right?
--
Steve
Message archived at
https://mail.python.org/archives/list/pytho...@python.org/message/TFKQPKGUU3G2HI5AVIFFKSULU3YCLLRY/