From a01e3bd9896adcf28a89fbbd09235747319b8113 Mon Sep 17 00:00:00 2001 From: Milan Markovic Date: Tue, 2 Jul 2019 20:22:23 +0000 Subject: 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. --- Cargo.toml | 1 + src/format/mod.rs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d1cf3be..72be383 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ radix_fmt = "1.0.0" lazy_static = "1.3.0" directories = "2.0.1" regex = "1.1.7" +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 @@ +extern crate num; +use num::{BigInt, FromPrimitive, ToPrimitive}; + use crate::CONFIGURATION; use crate::error::{ CalcError, Math }; + pub fn autobalance_parens(input: &str) -> Result { let mut balanced = String::from(input); let mut left_parens = 0; @@ -35,13 +39,15 @@ fn radix_fmt(number: f64, obase: usize) -> Result { let table: Vec = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect(); // format integral part of float - let mut integral = number.abs().trunc() as i64; + let mut integral = BigInt::from_f64(number.abs().trunc()).unwrap(); let mut obase_int = String::new(); - while integral >= obase as i64 { - obase_int.push(table[(integral % obase as i64) as usize]); - integral /= obase as i64; + let obaseb = BigInt::from_usize(obase).unwrap(); + + while &integral >= &obaseb { + obase_int.push(table[(&integral % &obaseb).to_usize().unwrap()]); + integral /= &obaseb; } - obase_int.push(table[integral as usize]); + obase_int.push(table[integral.to_usize().unwrap()]); if number.is_sign_negative() { obase_int.push('-'); } -- cgit v1.2.3