diff --git a/src/bigint.rs b/src/bigint.rs index df504d9..15e629b 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -280,7 +280,7 @@ impl BigInt { self.parts.pop(); } } - + /// Add `rhs` to this number. pub fn inplace_add(&mut self, rhs: &Self) { self.inplace_add_slice(&rhs.parts[..]); diff --git a/src/string.rs b/src/string.rs index c535626..befc6e1 100644 --- a/src/string.rs +++ b/src/string.rs @@ -175,7 +175,7 @@ mod from { use core::fmt::{Debug, Display}; use std::error::Error; - use crate::{Float, BigInt, Semantics, FP64}; + use crate::{BigInt, Float, Semantics, FP64}; impl TryFrom<&str> for Float { type Error = ParseError; @@ -192,7 +192,8 @@ mod from { } let chars = value.as_bytes(); - let (sign, skip) = if chars[0] == '-' as u8 || chars[0] == '+' as u8 { + let (sign, skip) = if chars[0] == '-' as u8 || chars[0] == '+' as u8 + { (chars[0] == '-' as u8, 1) } else { (false, 0) @@ -208,14 +209,25 @@ mod from { let l_r = value.split_once('.'); if l_r.is_none() { - return parse_whole_num(&value[skip..], sign).map(|val| Ok(val)).unwrap_or(Err(ParseError(ParseErrorKind::Other))); + return parse_whole_num(&value[skip..], sign) + .map(|val| Ok(val)) + .unwrap_or(Err(ParseError(ParseErrorKind::Other))); } let (left, right) = l_r.unwrap(); let (left, right) = (&left[skip..], right); - if right.chars().map(|chr| chr as u32 - '0' as u32).sum::() == 0 { - return parse_whole_num(left, sign).map(|num| Ok(num)).unwrap_or(Err(ParseError(ParseErrorKind::Other))); + if right + .chars() + .map(|chr| chr as u32 - '0' as u32) + .sum::() + == 0 + { + return parse_whole_num(left, sign) + .map(|num| Ok(num)) + .unwrap_or(Err(ParseError(ParseErrorKind::Other))); } - let left_num = parse_big_int(left).map(|num| Ok(num)).unwrap_or(Err(ParseError(ParseErrorKind::Other)))?; + let left_num = parse_big_int(left) + .map(|num| Ok(num)) + .unwrap_or(Err(ParseError(ParseErrorKind::Other)))?; let ((right_num, right_num_digits), exp_num) = 'parse: { let mut val = right.split_once('e'); if val.is_none() { @@ -223,24 +235,49 @@ mod from { } if let Some((mantissa, exp)) = val { match exp.parse::() { - Ok(exp) => break 'parse (parse_big_int(mantissa).map(|num| Ok((num, mantissa.len()))).unwrap_or(Err(ParseError(ParseErrorKind::Other)))?, Some(exp)), - Err(_) => return Err(ParseError(ParseErrorKind::ExponentParseFailed)), + Ok(exp) => { + break 'parse ( + parse_big_int(mantissa) + .map(|num| Ok((num, mantissa.len()))) + .unwrap_or(Err(ParseError( + ParseErrorKind::Other, + )))?, + Some(exp), + ) + } + Err(_) => { + return Err(ParseError( + ParseErrorKind::ExponentParseFailed, + )) + } } } - (parse_big_int(right).map(|num| Ok((num, right.len()))).unwrap_or(Err(ParseError(ParseErrorKind::Other)))?, None) + ( + parse_big_int(right) + .map(|num| Ok((num, right.len()))) + .unwrap_or(Err(ParseError(ParseErrorKind::Other)))?, + None, + ) }; let num = { let exp = BigInt::from_u64(10).powi(right_num_digits as u64); let num = left_num * &exp + &right_num; - + // TODO: autodetect required semantics - let mut ret = Float::from_bigint(DEFAULT_SEM, num) / (Float::from_bigint(DEFAULT_SEM, exp)); + let mut ret = Float::from_bigint(DEFAULT_SEM, num) + / (Float::from_bigint(DEFAULT_SEM, exp)); if let Some(exp_num) = exp_num { if exp_num >= 0 { - ret *= Float::from_bigint(DEFAULT_SEM, BigInt::from_u64(10).powi(exp_num as u64)) + ret *= Float::from_bigint( + DEFAULT_SEM, + BigInt::from_u64(10).powi(exp_num as u64), + ) } else { - ret /= Float::from_bigint(DEFAULT_SEM, BigInt::from_u64(10).powi((-exp_num) as u64)) + ret /= Float::from_bigint( + DEFAULT_SEM, + BigInt::from_u64(10).powi((-exp_num) as u64), + ) } } ret @@ -306,7 +343,6 @@ mod from { Display::fmt(&self, f) } } - } #[cfg(feature = "std")]