aboutsummaryrefslogtreecommitdiff
path: root/src/format/mod.rs
diff options
context:
space:
mode:
authorMilan Markovic <[email protected]>2019-07-02 21:22:23 +0100
committerMilan Markovic <[email protected]>2019-07-02 21:22:23 +0100
commita01e3bd9896adcf28a89fbbd09235747319b8113 (patch)
tree412a90f5c6b39cb9a950c94c6e1e80c63a380c9a /src/format/mod.rs
parent580747e5f44ae8b42d565402db9829417ae2bc81 (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.
Diffstat (limited to 'src/format/mod.rs')
-rw-r--r--src/format/mod.rs16
1 files changed, 11 insertions, 5 deletions
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 @@
1extern crate num;
2use num::{BigInt, FromPrimitive, ToPrimitive};
3
1use crate::CONFIGURATION; 4use crate::CONFIGURATION;
2use crate::error::{ 5use crate::error::{
3 CalcError, 6 CalcError,
4 Math 7 Math
5}; 8};
6 9
10
7pub fn autobalance_parens(input: &str) -> Result<String, CalcError> { 11pub 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 }