@@ -2,6 +2,7 @@ use super::{
22 try_bigint_to_f64, PyByteArray , PyBytes , PyInt , PyIntRef , PyStr , PyStrRef , PyType , PyTypeRef ,
33} ;
44use crate :: {
5+ atomic_func,
56 class:: PyClassImpl ,
67 common:: { float_ops, hash} ,
78 convert:: { ToPyObject , ToPyResult } ,
@@ -57,16 +58,6 @@ impl From<f64> for PyFloat {
5758 }
5859}
5960
60- impl PyObject {
61- pub fn try_float_opt ( & self , vm : & VirtualMachine ) -> PyResult < Option < PyRef < PyFloat > > > {
62- PyNumber :: new ( self , vm) . float_opt ( vm)
63- }
64-
65- pub fn try_float ( & self , vm : & VirtualMachine ) -> PyResult < PyRef < PyFloat > > {
66- PyNumber :: new ( self , vm) . float ( vm)
67- }
68- }
69-
7061pub ( crate ) fn to_op_float ( obj : & PyObject , vm : & VirtualMachine ) -> PyResult < Option < f64 > > {
7162 let v = if let Some ( float) = obj. payload_if_subclass :: < PyFloat > ( vm) {
7263 Some ( float. value )
@@ -553,36 +544,63 @@ impl Hashable for PyFloat {
553544}
554545
555546impl AsNumber for PyFloat {
556- const AS_NUMBER : PyNumberMethods = PyNumberMethods {
557- add : Some ( |number, other, vm| Self :: number_float_op ( number, other, |a, b| a + b, vm) ) ,
558- subtract : Some ( |number, other, vm| Self :: number_float_op ( number, other, |a, b| a - b, vm) ) ,
559- multiply : Some ( |number, other, vm| Self :: number_float_op ( number, other, |a, b| a * b, vm) ) ,
560- remainder : Some ( |number, other, vm| Self :: number_general_op ( number, other, inner_mod, vm) ) ,
561- divmod : Some ( |number, other, vm| Self :: number_general_op ( number, other, inner_divmod, vm) ) ,
562- power : Some ( |number, other, vm| Self :: number_general_op ( number, other, float_pow, vm) ) ,
563- negative : Some ( |number, vm| {
564- let value = Self :: number_downcast ( number) . value ;
565- ( -value) . to_pyresult ( vm)
566- } ) ,
567- positive : Some ( |number, vm| Self :: number_float ( number, vm) . to_pyresult ( vm) ) ,
568- absolute : Some ( |number, vm| {
569- let value = Self :: number_downcast ( number) . value ;
570- value. abs ( ) . to_pyresult ( vm)
571- } ) ,
572- boolean : Some ( |number, _vm| Ok ( Self :: number_downcast ( number) . value . is_zero ( ) ) ) ,
573- int : Some ( |number, vm| {
574- let value = Self :: number_downcast ( number) . value ;
575- try_to_bigint ( value, vm) . map ( |x| vm. ctx . new_int ( x) )
576- } ) ,
577- float : Some ( |number, vm| Ok ( Self :: number_float ( number, vm) ) ) ,
578- floor_divide : Some ( |number, other, vm| {
579- Self :: number_general_op ( number, other, inner_floordiv, vm)
580- } ) ,
581- true_divide : Some ( |number, other, vm| {
582- Self :: number_general_op ( number, other, inner_div, vm)
583- } ) ,
584- ..PyNumberMethods :: NOT_IMPLEMENTED
585- } ;
547+ fn as_number ( ) -> & ' static PyNumberMethods {
548+ static AS_NUMBER : PyNumberMethods = PyNumberMethods {
549+ add : atomic_func ! ( |num, other, vm| PyFloat :: number_float_op(
550+ num,
551+ other,
552+ |a, b| a + b,
553+ vm
554+ ) ) ,
555+ subtract : atomic_func ! ( |num, other, vm| PyFloat :: number_float_op(
556+ num,
557+ other,
558+ |a, b| a - b,
559+ vm
560+ ) ) ,
561+ multiply : atomic_func ! ( |num, other, vm| PyFloat :: number_float_op(
562+ num,
563+ other,
564+ |a, b| a * b,
565+ vm
566+ ) ) ,
567+ remainder : atomic_func ! ( |num, other, vm| PyFloat :: number_general_op(
568+ num, other, inner_mod, vm
569+ ) ) ,
570+ divmod : atomic_func ! ( |num, other, vm| PyFloat :: number_general_op(
571+ num,
572+ other,
573+ inner_divmod,
574+ vm
575+ ) ) ,
576+ power : atomic_func ! ( |num, other, vm| PyFloat :: number_general_op(
577+ num, other, float_pow, vm
578+ ) ) ,
579+ negative : atomic_func ! ( |num, vm| {
580+ let value = PyFloat :: number_downcast( num) . value;
581+ ( -value) . to_pyresult( vm)
582+ } ) ,
583+ positive : atomic_func ! ( |num, vm| PyFloat :: number_float( num, vm) . to_pyresult( vm) ) ,
584+ absolute : atomic_func ! ( |num, vm| {
585+ let value = PyFloat :: number_downcast( num) . value;
586+ value. abs( ) . to_pyresult( vm)
587+ } ) ,
588+ boolean : atomic_func ! ( |num, _vm| Ok ( PyFloat :: number_downcast( num) . value. is_zero( ) ) ) ,
589+ int : atomic_func ! ( |num, vm| {
590+ let value = PyFloat :: number_downcast( num) . value;
591+ try_to_bigint( value, vm) . map( |x| vm. ctx. new_int( x) )
592+ } ) ,
593+ float : atomic_func ! ( |num, vm| Ok ( PyFloat :: number_float( num, vm) ) ) ,
594+ floor_divide : atomic_func ! ( |num, other, vm| {
595+ PyFloat :: number_general_op( num, other, inner_floordiv, vm)
596+ } ) ,
597+ true_divide : atomic_func ! ( |num, other, vm| {
598+ PyFloat :: number_general_op( num, other, inner_div, vm)
599+ } ) ,
600+ ..PyNumberMethods :: NOT_IMPLEMENTED
601+ } ;
602+ & AS_NUMBER
603+ }
586604}
587605
588606impl PyFloat {
0 commit comments