File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -279,6 +279,17 @@ impl Number {
279279 }
280280 }
281281
282+ pub ( crate ) fn as_f32 ( & self ) -> Option < f32 > {
283+ #[ cfg( not( feature = "arbitrary_precision" ) ) ]
284+ match self . n {
285+ N :: PosInt ( n) => Some ( n as f32 ) ,
286+ N :: NegInt ( n) => Some ( n as f32 ) ,
287+ N :: Float ( n) => Some ( n as f32 ) ,
288+ }
289+ #[ cfg( feature = "arbitrary_precision" ) ]
290+ self . n . parse :: < f32 > ( ) . ok ( ) . filter ( |float| float. is_finite ( ) )
291+ }
292+
282293 pub ( crate ) fn from_f32 ( f : f32 ) -> Option < Number > {
283294 if f. is_finite ( ) {
284295 let n = {
Original file line number Diff line number Diff line change @@ -9,6 +9,13 @@ fn eq_u64(value: &Value, other: u64) -> bool {
99 value. as_u64 ( ) . map_or ( false , |i| i == other)
1010}
1111
12+ fn eq_f32 ( value : & Value , other : f32 ) -> bool {
13+ match value {
14+ Value :: Number ( n) => n. as_f32 ( ) . map_or ( false , |i| i == other) ,
15+ _ => false ,
16+ }
17+ }
18+
1219fn eq_f64 ( value : & Value , other : f64 ) -> bool {
1320 value. as_f64 ( ) . map_or ( false , |i| i == other)
1421}
@@ -90,6 +97,7 @@ macro_rules! partialeq_numeric {
9097partialeq_numeric ! {
9198 eq_i64[ i8 i16 i32 i64 isize ]
9299 eq_u64[ u8 u16 u32 u64 usize ]
93- eq_f64[ f32 f64 ]
100+ eq_f32[ f32 ]
101+ eq_f64[ f64 ]
94102 eq_bool[ bool ]
95103}
You can’t perform that action at this time.
0 commit comments