mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Fix int rounding
This commit is contained in:
1 parent
db01a1d653
commit
eafa0c0149
2 files changed
+7
-5
No files matched your search
@@ -12,7 +12,8 @@ use crate::{
|
||||
},
|
||||
convert::{IntoPyException, ToPyObject, ToPyResult},
|
||||
function::{
|
||||
ArgByteOrder, ArgIntoBool, FuncArgs, OptionalArg, PyArithmeticValue, PyComparisonValue,
|
||||
ArgByteOrder, ArgIntoBool, FuncArgs, OptionalArg, OptionalOption, PyArithmeticValue,
|
||||
PyComparisonValue,
|
||||
},
|
||||
protocol::{PyNumberMethods, handle_bytes_to_int_err},
|
||||
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp, Representable},
|
||||
@@ -381,10 +382,10 @@ impl PyInt {
|
||||
#[pymethod]
|
||||
fn __round__(
|
||||
zelf: PyRef<Self>,
|
||||
ndigits: OptionalArg<PyIntRef>,
|
||||
ndigits: OptionalOption<PyIntRef>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyRef<Self>> {
|
||||
if let OptionalArg::Present(ndigits) = ndigits {
|
||||
if let Some(ndigits) = ndigits.flatten() {
|
||||
let ndigits = ndigits.as_bigint();
|
||||
// round(12345, -2) == 12300
|
||||
// If precision >= 0, then any integer is already rounded correctly
|
||||
|
||||
@@ -318,8 +318,9 @@ assert isinstance((0).__round__(0), int)
|
||||
assert isinstance((1).__round__(0), int)
|
||||
assert (0).__round__(0) == 0
|
||||
assert (1).__round__(0) == 1
|
||||
assert_raises(TypeError, lambda: (0).__round__(None))
|
||||
assert_raises(TypeError, lambda: (1).__round__(None))
|
||||
# Python 3.14+: __round__(None) is now allowed, same as __round__()
|
||||
assert (0).__round__(None) == 0
|
||||
assert (1).__round__(None) == 1
|
||||
assert_raises(TypeError, lambda: (0).__round__(0.0))
|
||||
assert_raises(TypeError, lambda: (1).__round__(0.0))
|
||||
|
||||
|
||||
Reference in new issue
Block a user