-
Notifications
You must be signed in to change notification settings - Fork 0
Numeric #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xiugaze
wants to merge
12
commits into
main
Choose a base branch
from
numeric
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Numeric #3
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
89030e6
rough start, constant functions
xiugaze 88fd1bf
workin on it
xiugaze f475102
possible convert_to_dec_bignum
xiugaze d27f064
chop precision and round
xiugaze f26ec2b
slices
xiugaze 563b85e
enums
xiugaze 3c75038
Change function return to Result
xiugaze 9bac8e8
implemented arithmetic
xiugaze ab5adfb
starting tests
xiugaze 23484c8
more tests
xiugaze 75dc441
done, working as far as i can tell
xiugaze 6cdd605
error messages
xiugaze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub mod strings; | ||
| pub mod strings; | ||
| pub mod numeric; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| use std::ops::{ Add, Sub, Mul, Div }; | ||
| use std::fmt; | ||
| use regex::*; | ||
| use lazy_static::lazy_static; | ||
| use crate::core::errors::SecretError; | ||
|
|
||
| /* | ||
| The goal of this is to be able to use the Dec type with normal operators. | ||
| TODO: Figure out how to do that, what do the rust operators call | ||
| */ | ||
|
|
||
| const DEC_NUM_DIGITS: i128 = 18; | ||
| const DEC_ONE: i128 = 10_i128.pow(18_u32); | ||
|
|
||
| pub enum Number { | ||
| Str(&'static str), | ||
| Int(i32), | ||
| Float(f32), | ||
| } | ||
|
|
||
| pub fn convert_to_dec_bignum(arg: Number) -> Result<i128, SecretError> { | ||
| // TODO: Better Error messages | ||
| match arg { | ||
| Number::Str(str) => { | ||
| return match from_str(str) { | ||
| Some(BigInt) => Ok(BigInt), | ||
| None => Err(SecretError::Error("Error: Invalid String Input".to_string())) | ||
| } | ||
| }, | ||
| Number::Float(float) => { | ||
| let float_string = float.to_string(); | ||
| return match from_str(&float_string) { | ||
| Some(BigInt) => Ok(BigInt), | ||
| None => Err(SecretError::Error("Error: Invalid Float Input".to_string())) | ||
| } | ||
| } | ||
| Number::Int(int) => { return Ok(int as i128 * DEC_ONE) } | ||
| } | ||
|
|
||
| fn from_str(arg: &str) -> Option<i128> { | ||
| lazy_static! { | ||
| static ref RE: Regex = Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); | ||
| } | ||
| let parts = RE.captures(arg)?; | ||
| let mut result: i128 = parts.get(2)? | ||
| .as_str() | ||
| .trim() | ||
| .parse::<i128>() | ||
| .expect("Invalid String: NAN") * DEC_ONE; | ||
| if let Some(_) = parts.get(3) { | ||
| let fraction: i128 = parts.get(4)? | ||
| .as_str() | ||
| .trim() // TODO: slice | ||
| .parse::<i128>() | ||
| .expect("Invalid String: NAN"); | ||
| result += fraction; | ||
| } | ||
| if let Some(_) = parts.get(1) { | ||
| result *= -1; | ||
| } | ||
| return Some(result as i128); | ||
| } | ||
| } | ||
|
|
||
| fn chop_precision_and_round(d: i128) -> i128 { | ||
| if d < 0 { | ||
| return -1 * chop_precision_and_round(d * -1); | ||
| } | ||
|
|
||
| let quo: i128 = d / DEC_ONE; | ||
| let rem: i128 = d % DEC_ONE; | ||
|
|
||
| if rem == 0 { | ||
| return quo; | ||
| } | ||
|
|
||
| if rem < DEC_ONE / 2 { | ||
| return quo; | ||
| } else if rem > DEC_ONE / 2 { | ||
| return quo + 1; | ||
| } else { | ||
| if quo % 2 == 0 { | ||
| return quo; | ||
| } | ||
| return quo; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| #[derive(Default)] | ||
| pub struct Dec { | ||
| i: i128, | ||
| } | ||
|
|
||
| impl Dec { | ||
|
|
||
| pub fn from(arg: Number) -> Result<Dec, SecretError> { | ||
| Ok(Dec { i: convert_to_dec_bignum(arg)?, }) | ||
| } | ||
|
|
||
| pub fn zero() -> Result<Dec, SecretError> { Dec::from(Number::Int(0)) } | ||
|
|
||
| pub fn one() -> Result<Dec, SecretError> { Dec::from(Number::Int(1)) } | ||
|
|
||
| pub fn whole(&self) -> String { | ||
| format!("{}", self.i.abs() / DEC_ONE) | ||
| } | ||
|
|
||
| pub fn frac(&self) -> String { | ||
| format!("{}", self.i.abs() % DEC_ONE).trim().to_string() | ||
| } | ||
|
|
||
| pub fn parity(&self) -> i32 { | ||
| if self.i < 0 { -1 } else { 1 } | ||
| } | ||
|
|
||
| pub fn add_dec(&self, addend: Dec) -> i128 { | ||
| self.i + addend.i | ||
| } | ||
|
|
||
| pub fn sub_dec(&self, subtrahend: Dec) -> i128 { | ||
| self.i - subtrahend.i | ||
| } | ||
|
|
||
| pub fn mul_dec(&self, multiplier: Dec) -> i128 { | ||
| let x = self.i; | ||
| let y = multiplier.i; | ||
| chop_precision_and_round(x * y) | ||
| } | ||
|
|
||
| pub fn div_dec(&self, divisor: Dec) -> i128 { | ||
| if divisor.i == 0 { | ||
| panic!("Error: Tried to divide by 0 for {} / {}", self.i, divisor.i); | ||
| } else { | ||
| chop_precision_and_round((self.i * DEC_ONE * DEC_ONE) / divisor.i) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // traits to implement: Add, Sub, Mul, Div | ||
| } | ||
|
|
||
| impl fmt::Display for Dec { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| if self.i == 0 { | ||
| write!(f, "{}", "0.".to_owned() + &"0".repeat(DEC_NUM_DIGITS as usize)); | ||
| } | ||
| let parity = if self.i > 0 { "-" } else { "" }; | ||
| write!(f, "{}{}.{}", parity, self.whole(), self.frac()) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| impl Add for Dec { | ||
| type Output = Self; | ||
|
|
||
| fn add(self, addend: Self) -> Self { | ||
| Self { | ||
| i: self.add_dec(addend), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Sub for Dec { | ||
| type Output = Self; | ||
|
|
||
| fn sub(self, addend: Self) -> Self { | ||
| Self { | ||
| i: self.sub_dec(addend), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Mul for Dec { | ||
| type Output = Self; | ||
|
|
||
| fn mul(self, multiplier: Self) -> Self { | ||
| Self { | ||
| i: self.mul_dec(multiplier), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Div for Dec { | ||
| type Output = Self; | ||
|
|
||
| fn div(self, multiplier: Self) -> Self { | ||
| Self { | ||
| i: self.div_dec(multiplier), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| #[derive(Debug)] | ||
| pub enum SecretError { | ||
| Bech32Error(String), | ||
| Error(String), | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a string slice according to the python library, but I can't figure out a way to index an &str. See if you can figure it out