Skip to content

Commit

Permalink
Apply cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
terrarier2111 committed Jan 14, 2024
1 parent cdb93f9 commit 2c14dd2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..]);
Expand Down
64 changes: 50 additions & 14 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -208,39 +209,75 @@ 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::<u32>() == 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::<u32>()
== 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() {
val = right.split_once('E');
}
if let Some((mantissa, exp)) = val {
match exp.parse::<i64>() {
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
Expand Down Expand Up @@ -306,7 +343,6 @@ mod from {
Display::fmt(&self, f)
}
}

}

#[cfg(feature = "std")]
Expand Down

0 comments on commit 2c14dd2

Please sign in to comment.