File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change 114
114
assert math .trunc (1.2 ) == 1
115
115
assert_raises (OverflowError , float ('inf' ).__trunc__ )
116
116
assert_raises (ValueError , float ('nan' ).__trunc__ )
117
+ assert 0.5 .__round__ () == 0.0
118
+ assert 1.5 .__round__ () == 2.0
119
+ assert 0.5 .__round__ (0 ) == 0.0
120
+ assert 1.5 .__round__ (0 ) == 2.0
121
+ assert 0.5 .__round__ (None ) == 0.0
122
+ assert 1.5 .__round__ (None ) == 2.0
123
+ assert_raises (OverflowError , float ('inf' ).__round__ )
124
+ assert_raises (ValueError , float ('nan' ).__round__ )
117
125
118
126
assert (1.7 ).real == 1.7
119
127
assert (1.3 ).is_integer () == False
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ use crate::pyobject::{
11
11
use crate :: vm:: VirtualMachine ;
12
12
use num_bigint:: { BigInt , ToBigInt } ;
13
13
use num_rational:: Ratio ;
14
- use num_traits:: ToPrimitive ;
14
+ use num_traits:: { ToPrimitive , Zero } ;
15
15
16
16
#[ pyclass( name = "float" ) ]
17
17
#[ derive( Debug , Copy , Clone , PartialEq ) ]
@@ -387,8 +387,19 @@ impl PyFloat {
387
387
OptionalArg :: Missing => None ,
388
388
OptionalArg :: Present ( ref value) => {
389
389
if !vm. get_none ( ) . is ( value) {
390
- // retrive int to implement it
391
- Some ( vm. ctx . not_implemented ( ) )
390
+ let ndigits = if objtype:: isinstance ( value, & vm. ctx . int_type ( ) ) {
391
+ objint:: get_value ( value)
392
+ } else {
393
+ return Err ( vm. new_type_error ( format ! (
394
+ "TypeError: '{}' object cannot be interpreted as an integer" ,
395
+ value. class( ) . name
396
+ ) ) ) ;
397
+ } ;
398
+ if ndigits. is_zero ( ) {
399
+ None
400
+ } else {
401
+ Some ( ndigits)
402
+ }
392
403
} else {
393
404
None
394
405
}
You can’t perform that action at this time.
0 commit comments