Cleanup numeric entries
This commit is contained in:
parent
dab0333b31
commit
39e3c83abc
1246
src/calc.rs
1246
src/calc.rs
File diff suppressed because it is too large
Load Diff
@ -34,6 +34,7 @@ impl PartialEq for Number {
|
||||
// pub value: Vec<Number>,
|
||||
// }
|
||||
|
||||
// TODO: Remove the copy trait
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Entry {
|
||||
@ -194,61 +195,61 @@ impl CalculatorEntry for Number {
|
||||
}
|
||||
fn add(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match arg {
|
||||
Entry::Number(Number { value }) => Ok(Entry::Number(Number {
|
||||
Entry::Number(Self { value }) => Ok(Entry::Number(Self {
|
||||
value: value + self.value,
|
||||
})),
|
||||
}
|
||||
}
|
||||
fn sub(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match arg {
|
||||
Entry::Number(Number { value }) => Ok(Entry::Number(Number {
|
||||
Entry::Number(Self { value }) => Ok(Entry::Number(Self {
|
||||
value: value - self.value,
|
||||
})),
|
||||
}
|
||||
}
|
||||
fn mul(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match arg {
|
||||
Entry::Number(Number { value }) => Ok(Entry::Number(Number {
|
||||
Entry::Number(Self { value }) => Ok(Entry::Number(Self {
|
||||
value: value * self.value,
|
||||
})),
|
||||
}
|
||||
}
|
||||
fn div(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match arg {
|
||||
Entry::Number(Number { value }) => Ok(Entry::Number(Number {
|
||||
Entry::Number(Self { value }) => Ok(Entry::Number(Self {
|
||||
value: value / self.value,
|
||||
})),
|
||||
}
|
||||
}
|
||||
fn int_divide(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match arg {
|
||||
Entry::Number(Number { value }) => Ok(Entry::Number(Number {
|
||||
Entry::Number(Self { value }) => Ok(Entry::Number(Self {
|
||||
value: value.div_euclid(self.value),
|
||||
})),
|
||||
}
|
||||
}
|
||||
fn negate(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number { value: -self.value }))
|
||||
Ok(Entry::Number(Self { value: -self.value }))
|
||||
}
|
||||
fn abs(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: self.value.abs(),
|
||||
}))
|
||||
}
|
||||
fn inverse(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: self.value.recip(),
|
||||
}))
|
||||
}
|
||||
fn modulo(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match arg {
|
||||
Entry::Number(Number { value }) => Ok(Entry::Number(Number {
|
||||
Entry::Number(Self { value }) => Ok(Entry::Number(Self {
|
||||
value: value % self.value,
|
||||
})),
|
||||
}
|
||||
}
|
||||
fn sin(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: match angle_mode {
|
||||
CalculatorAngleMode::Degrees => self.value.to_radians().sin(),
|
||||
CalculatorAngleMode::Radians => self.value.sin(),
|
||||
@ -259,7 +260,7 @@ impl CalculatorEntry for Number {
|
||||
}))
|
||||
}
|
||||
fn cos(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: match angle_mode {
|
||||
CalculatorAngleMode::Degrees => self.value.to_radians().cos(),
|
||||
CalculatorAngleMode::Radians => self.value.cos(),
|
||||
@ -270,7 +271,7 @@ impl CalculatorEntry for Number {
|
||||
}))
|
||||
}
|
||||
fn tan(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: match angle_mode {
|
||||
CalculatorAngleMode::Degrees => self.value.to_radians().tan(),
|
||||
CalculatorAngleMode::Radians => self.value.tan(),
|
||||
@ -281,7 +282,7 @@ impl CalculatorEntry for Number {
|
||||
}))
|
||||
}
|
||||
fn asin(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: match angle_mode {
|
||||
CalculatorAngleMode::Degrees => self.value.asin().to_degrees(),
|
||||
CalculatorAngleMode::Radians => self.value.asin(),
|
||||
@ -292,7 +293,7 @@ impl CalculatorEntry for Number {
|
||||
}))
|
||||
}
|
||||
fn acos(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: match angle_mode {
|
||||
CalculatorAngleMode::Degrees => self.value.acos().to_degrees(),
|
||||
CalculatorAngleMode::Radians => self.value.acos(),
|
||||
@ -303,7 +304,7 @@ impl CalculatorEntry for Number {
|
||||
}))
|
||||
}
|
||||
fn atan(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: match angle_mode {
|
||||
CalculatorAngleMode::Degrees => self.value.atan().to_degrees(),
|
||||
CalculatorAngleMode::Radians => self.value.atan(),
|
||||
@ -314,23 +315,23 @@ impl CalculatorEntry for Number {
|
||||
}))
|
||||
}
|
||||
fn sqrt(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: self.value.sqrt(),
|
||||
}))
|
||||
}
|
||||
fn log(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: self.value.log10(),
|
||||
}))
|
||||
}
|
||||
fn ln(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
Ok(Entry::Number(Self {
|
||||
value: self.value.ln(),
|
||||
}))
|
||||
}
|
||||
fn pow(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match arg {
|
||||
Entry::Number(Number { value }) => Ok(Entry::Number(Number {
|
||||
Entry::Number(Self { value }) => Ok(Entry::Number(Self {
|
||||
value: value.powf(self.value),
|
||||
})),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user