diff options
author | Milan Markovic <[email protected]> | 2019-07-02 21:22:23 +0100 |
---|---|---|
committer | Milan Markovic <[email protected]> | 2019-07-02 21:22:23 +0100 |
commit | a01e3bd9896adcf28a89fbbd09235747319b8113 (patch) | |
tree | 412a90f5c6b39cb9a950c94c6e1e80c63a380c9a | |
parent | 580747e5f44ae8b42d565402db9829417ae2bc81 (diff) |
Fixing crash on large result
- Added depencency bignum
- Using num::BigNum in format::pprint to handle f64 numbers that are
larger than maximal i64 number (which was causing the bug)
If you want full feature parity with bc perhaps bignums can be used as
long as all operations are done on integers.
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/format/mod.rs | 16 |
2 files changed, 12 insertions, 5 deletions
@@ -18,3 +18,4 @@ radix_fmt = "1.0.0" | |||
18 | lazy_static = "1.3.0" | 18 | lazy_static = "1.3.0" |
19 | directories = "2.0.1" | 19 | directories = "2.0.1" |
20 | regex = "1.1.7" | 20 | regex = "1.1.7" |
21 | num = "0.2" | ||
diff --git a/src/format/mod.rs b/src/format/mod.rs index baae301..2ba7ad9 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs | |||
@@ -1,9 +1,13 @@ | |||
1 | extern crate num; | ||
2 | use num::{BigInt, FromPrimitive, ToPrimitive}; | ||
3 | |||
1 | use crate::CONFIGURATION; | 4 | use crate::CONFIGURATION; |
2 | use crate::error::{ | 5 | use crate::error::{ |
3 | CalcError, | 6 | CalcError, |
4 | Math | 7 | Math |
5 | }; | 8 | }; |
6 | 9 | ||
10 | |||
7 | pub fn autobalance_parens(input: &str) -> Result<String, CalcError> { | 11 | pub fn autobalance_parens(input: &str) -> Result<String, CalcError> { |
8 | let mut balanced = String::from(input); | 12 | let mut balanced = String::from(input); |
9 | let mut left_parens = 0; | 13 | let mut left_parens = 0; |
@@ -35,13 +39,15 @@ fn radix_fmt(number: f64, obase: usize) -> Result<String, CalcError> { | |||
35 | let table: Vec<char> = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect(); | 39 | let table: Vec<char> = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect(); |
36 | 40 | ||
37 | // format integral part of float | 41 | // format integral part of float |
38 | let mut integral = number.abs().trunc() as i64; | 42 | let mut integral = BigInt::from_f64(number.abs().trunc()).unwrap(); |
39 | let mut obase_int = String::new(); | 43 | let mut obase_int = String::new(); |
40 | while integral >= obase as i64 { | 44 | let obaseb = BigInt::from_usize(obase).unwrap(); |
41 | obase_int.push(table[(integral % obase as i64) as usize]); | 45 | |
42 | integral /= obase as i64; | 46 | while &integral >= &obaseb { |
47 | obase_int.push(table[(&integral % &obaseb).to_usize().unwrap()]); | ||
48 | integral /= &obaseb; | ||
43 | } | 49 | } |
44 | obase_int.push(table[integral as usize]); | 50 | obase_int.push(table[integral.to_usize().unwrap()]); |
45 | if number.is_sign_negative() { | 51 | if number.is_sign_negative() { |
46 | obase_int.push('-'); | 52 | obase_int.push('-'); |
47 | } | 53 | } |