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. --- src/format/mod.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src') 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