39
39
//!
40
40
//! println!("Input ({}) with 10 decimals: {} vs {})", input, dec, float);
41
41
//! ```
42
+ #![ allow( clippy:: unreadable_literal) ]
43
+ #![ allow( clippy:: needless_return) ]
44
+ #![ allow( clippy:: suspicious_arithmetic_impl) ]
45
+ #![ allow( clippy:: suspicious_op_assign_impl) ]
46
+ #![ allow( clippy:: redundant_field_names) ]
42
47
43
48
extern crate num_bigint;
44
49
extern crate num_integer;
@@ -61,6 +66,8 @@ use num_bigint::{BigInt, ParseBigIntError, Sign, ToBigInt};
61
66
use num_integer:: Integer ;
62
67
pub use traits:: { FromPrimitive , Num , One , Signed , ToPrimitive , Zero } ;
63
68
69
+ const LOG2_10 : f64 = 3.321928094887362_f64 ;
70
+
64
71
#[ macro_use]
65
72
mod macros;
66
73
@@ -93,7 +100,7 @@ fn count_decimal_digits(int: &BigInt) -> u64 {
93
100
// guess number of digits based on number of bits in UInt
94
101
let mut digits = ( int. bits ( ) as f64 / 3.3219280949 ) as u64 ;
95
102
let mut num = ten_to_the ( digits) ;
96
- while int >= & num {
103
+ while * int >= num {
97
104
num *= 10u8 ;
98
105
digits += 1 ;
99
106
}
@@ -118,11 +125,11 @@ fn get_rounding_term(num: &BigInt) -> u8 {
118
125
119
126
// loop-method
120
127
loop {
121
- if num < & n {
128
+ if * num < n {
122
129
return 1 ;
123
130
}
124
131
n *= 5 ;
125
- if num < & n {
132
+ if * num < n {
126
133
return 0 ;
127
134
}
128
135
n *= 2 ;
@@ -389,15 +396,14 @@ impl BigDecimal {
389
396
390
397
// make guess
391
398
let guess = {
392
- let log2_10 = 3.32192809488736234787031942948939018_f64 ;
393
399
let magic_guess_scale = 1.1951678538495576_f64 ;
394
- let initial_guess = ( self . int_val . bits ( ) as f64 - self . scale as f64 * log2_10 ) / 2.0 ;
400
+ let initial_guess = ( self . int_val . bits ( ) as f64 - self . scale as f64 * LOG2_10 ) / 2.0 ;
395
401
let res = magic_guess_scale * initial_guess. exp2 ( ) ;
396
402
if res. is_normal ( ) {
397
403
BigDecimal :: try_from ( res) . unwrap ( )
398
404
} else {
399
405
// can't guess with float - just guess magnitude
400
- let scale = ( self . int_val . bits ( ) as f64 / -log2_10 + self . scale as f64 ) . round ( ) as i64 ;
406
+ let scale = ( self . int_val . bits ( ) as f64 / -LOG2_10 + self . scale as f64 ) . round ( ) as i64 ;
401
407
BigDecimal :: new ( BigInt :: from ( 1 ) , scale / 2 )
402
408
}
403
409
} ;
@@ -462,15 +468,14 @@ impl BigDecimal {
462
468
463
469
// make guess
464
470
let guess = {
465
- let log2_10 = 3.32192809488736234787031942948939018_f64 ;
466
471
let magic_guess_scale = 1.124960491619939_f64 ;
467
- let initial_guess = ( self . int_val . bits ( ) as f64 - self . scale as f64 * log2_10 ) / 3.0 ;
472
+ let initial_guess = ( self . int_val . bits ( ) as f64 - self . scale as f64 * LOG2_10 ) / 3.0 ;
468
473
let res = magic_guess_scale * initial_guess. exp2 ( ) ;
469
474
if res. is_normal ( ) {
470
475
BigDecimal :: try_from ( res) . unwrap ( )
471
476
} else {
472
477
// can't guess with float - just guess magnitude
473
- let scale = ( self . int_val . bits ( ) as f64 / log2_10 - self . scale as f64 ) . round ( ) as i64 ;
478
+ let scale = ( self . int_val . bits ( ) as f64 / LOG2_10 - self . scale as f64 ) . round ( ) as i64 ;
474
479
BigDecimal :: new ( BigInt :: from ( 1 ) , -scale / 3 )
475
480
}
476
481
} ;
@@ -530,16 +535,15 @@ impl BigDecimal {
530
535
let bits = self . int_val . bits ( ) as f64 ;
531
536
let scale = self . scale as f64 ;
532
537
533
- let log2_10 = 3.32192809488736234787031942948939018_f64 ;
534
538
let magic_factor = 0.721507597259061_f64 ;
535
- let initial_guess = scale * log2_10 - bits;
539
+ let initial_guess = scale * LOG2_10 - bits;
536
540
let res = magic_factor * initial_guess. exp2 ( ) ;
537
541
538
542
if res. is_normal ( ) {
539
543
BigDecimal :: try_from ( res) . unwrap ( )
540
544
} else {
541
545
// can't guess with float - just guess magnitude
542
- let scale = ( bits / log2_10 + scale) . round ( ) as i64 ;
546
+ let scale = ( bits / LOG2_10 + scale) . round ( ) as i64 ;
543
547
BigDecimal :: new ( BigInt :: from ( 1 ) , -scale)
544
548
}
545
549
} ;
@@ -1271,7 +1275,7 @@ fn impl_division(mut num: BigInt, den: &BigInt, mut scale: i64, max_precision: u
1271
1275
}
1272
1276
1273
1277
// shift digits until numerator is larger than denominator (set scale appropriately)
1274
- while & num < den {
1278
+ while num < * den {
1275
1279
scale += 1 ;
1276
1280
num *= 10 ;
1277
1281
}
@@ -1325,7 +1329,7 @@ impl Div<BigDecimal> for BigDecimal {
1325
1329
1326
1330
let scale = self . scale - other. scale ;
1327
1331
1328
- if & self . int_val == & other. int_val {
1332
+ if self . int_val == other. int_val {
1329
1333
return BigDecimal {
1330
1334
int_val : 1 . into ( ) ,
1331
1335
scale : scale,
@@ -1351,7 +1355,7 @@ impl<'a> Div<&'a BigDecimal> for BigDecimal {
1351
1355
1352
1356
let scale = self . scale - other. scale ;
1353
1357
1354
- if & self . int_val == & other. int_val {
1358
+ if self . int_val == other. int_val {
1355
1359
return BigDecimal {
1356
1360
int_val : 1 . into ( ) ,
1357
1361
scale : scale,
@@ -1739,6 +1743,7 @@ macro_rules! impl_from_type {
1739
1743
( $FromType: ty, $AsType: ty) => {
1740
1744
impl From <$FromType> for BigDecimal {
1741
1745
#[ inline]
1746
+ #[ allow( clippy:: cast_lossless) ]
1742
1747
fn from( n: $FromType) -> Self {
1743
1748
BigDecimal :: from( n as $AsType)
1744
1749
}
@@ -1972,7 +1977,7 @@ mod bigdecimal_serde {
1972
1977
}
1973
1978
}
1974
1979
1975
- #[ cfg_attr ( rustfmt, rustfmt_skip ) ]
1980
+ #[ rustfmt:: skip ]
1976
1981
#[ cfg( test) ]
1977
1982
mod bigdecimal_tests {
1978
1983
use BigDecimal ;
0 commit comments