Add test snippets, fix rounding for very small numbers

This commit is contained in:
janrg
2019-10-14 16:38:28 +02:00
parent 122aa96887
commit 4fd38e6220
2 changed files with 12 additions and 1 deletions

View File

@@ -165,6 +165,15 @@ assert isinstance(0.5.__round__(None), int)
assert isinstance(1.5.__round__(None), int)
assert 0.5.__round__(None) == 0
assert 1.5.__round__(None) == 2
assert 1.234.__round__(1) == 1.2
assert 1.23456.__round__(4) == 1.2346
assert 1.00000000001.__round__(10) == 1.0
assert 1234.5.__round__(-2) == 1200
assert 1.234.__round__(-1) == 0
assert 1.23456789.__round__(15) == 1.23456789
assert 1.2e300.__round__(-500) == 0
assert 1.234.__round__(500) == 1.234
assert 1.2e-300.__round__(299) == 0
assert_raises(TypeError, lambda: 0.5.__round__(0.0))
assert_raises(TypeError, lambda: 1.5.__round__(0.0))
assert_raises(OverflowError, float('inf').__round__)

View File

@@ -496,7 +496,9 @@ impl PyFloat {
ndigits if *ndigits < i32::min_value().to_bigint().unwrap() => i32::min_value(),
_ => ndigits.to_i32().unwrap(),
};
if (self.value > 1e+16_f64 && ndigits >= 0i32) || ndigits > 16i32 {
if (self.value > 1e+16_f64 && ndigits >= 0i32)
|| (ndigits + self.value.log10().floor() as i32 > 16i32)
{
return Ok(vm.ctx.new_float(self.value));
}
if ndigits >= 0i32 {