@@ -2,9 +2,11 @@ use super::objbytes;
2
2
use super :: objint;
3
3
use super :: objstr;
4
4
use super :: objtype;
5
+ use crate :: function:: OptionalArg ;
5
6
use crate :: obj:: objtype:: PyClassRef ;
6
7
use crate :: pyobject:: {
7
- IntoPyObject , PyClassImpl , PyContext , PyObjectRef , PyRef , PyResult , PyValue , TypeProtocol ,
8
+ IdProtocol , IntoPyObject , PyClassImpl , PyContext , PyObjectRef , PyRef , PyResult , PyValue ,
9
+ TypeProtocol ,
8
10
} ;
9
11
use crate :: vm:: VirtualMachine ;
10
12
use num_bigint:: { BigInt , ToBigInt } ;
@@ -357,6 +359,36 @@ impl PyFloat {
357
359
self . value . to_bigint ( ) . unwrap ( )
358
360
}
359
361
362
+ #[ pymethod( name = "__round__" ) ]
363
+ fn round ( & self , ndigits : OptionalArg < PyObjectRef > , vm : & VirtualMachine ) -> PyObjectRef {
364
+ let ndigits = match ndigits {
365
+ OptionalArg :: Missing => None ,
366
+ OptionalArg :: Present ( ref value) => {
367
+ if !vm. get_none ( ) . is ( value) {
368
+ // retrive int to implement it
369
+ Some ( vm. ctx . not_implemented ( ) )
370
+ } else {
371
+ None
372
+ }
373
+ }
374
+ } ;
375
+ if ndigits. is_none ( ) {
376
+ let fract = self . value . fract ( ) ;
377
+ let value = if fract. abs ( ) == 0.5 {
378
+ if self . value . trunc ( ) % 2.0 == 0.0 {
379
+ self . value - fract
380
+ } else {
381
+ self . value + fract
382
+ }
383
+ } else {
384
+ self . value . round ( )
385
+ } ;
386
+ vm. ctx . new_int ( value. to_bigint ( ) . unwrap ( ) )
387
+ } else {
388
+ vm. ctx . not_implemented ( )
389
+ }
390
+ }
391
+
360
392
#[ pymethod( name = "__int__" ) ]
361
393
fn int ( & self , vm : & VirtualMachine ) -> BigInt {
362
394
self . trunc ( vm)
0 commit comments