use clearer naming for Number variants
This commit is contained in:
parent
41bfea3cea
commit
369feb5c40
@ -366,16 +366,16 @@ impl Color {
|
|||||||
return Color::new_hsla(val.clone(), val.clone(), val, alpha, hsla, repr);
|
return Color::new_hsla(val.clone(), val.clone(), val, alpha, hsla, repr);
|
||||||
}
|
}
|
||||||
|
|
||||||
let temporary_1 = if luminance < Number::machine_ratio(1, 2) {
|
let temporary_1 = if luminance < Number::small_ratio(1, 2) {
|
||||||
luminance.clone() * (Number::one() + saturation)
|
luminance.clone() * (Number::one() + saturation)
|
||||||
} else {
|
} else {
|
||||||
luminance.clone() + saturation.clone() - luminance.clone() * saturation
|
luminance.clone() + saturation.clone() - luminance.clone() * saturation
|
||||||
};
|
};
|
||||||
let temporary_2 = Number::from(2) * luminance - temporary_1.clone();
|
let temporary_2 = Number::from(2) * luminance - temporary_1.clone();
|
||||||
hue /= Number::from(360);
|
hue /= Number::from(360);
|
||||||
let mut temporary_r = hue.clone() + Number::machine_ratio(1, 3);
|
let mut temporary_r = hue.clone() + Number::small_ratio(1, 3);
|
||||||
let mut temporary_g = hue.clone();
|
let mut temporary_g = hue.clone();
|
||||||
let mut temporary_b = hue - Number::machine_ratio(1, 3);
|
let mut temporary_b = hue - Number::small_ratio(1, 3);
|
||||||
|
|
||||||
macro_rules! clamp_temp {
|
macro_rules! clamp_temp {
|
||||||
($temp:ident) => {
|
($temp:ident) => {
|
||||||
@ -400,7 +400,7 @@ impl Color {
|
|||||||
} else if Number::from(3) * temp.clone() < Number::from(2) {
|
} else if Number::from(3) * temp.clone() < Number::from(2) {
|
||||||
temp2.clone()
|
temp2.clone()
|
||||||
+ (temp1.clone() - temp2.clone())
|
+ (temp1.clone() - temp2.clone())
|
||||||
* (Number::machine_ratio(2, 3) - temp)
|
* (Number::small_ratio(2, 3) - temp)
|
||||||
* Number::from(6)
|
* Number::from(6)
|
||||||
} else {
|
} else {
|
||||||
temp2.clone()
|
temp2.clone()
|
||||||
|
@ -338,7 +338,7 @@ impl<'a> Parser<'a> {
|
|||||||
if val.num.len() <= 18 && val.times_ten.is_empty() {
|
if val.num.len() <= 18 && val.times_ten.is_empty() {
|
||||||
let n = Rational64::new_raw(parse_i64(&val.num), 1);
|
let n = Rational64::new_raw(parse_i64(&val.num), 1);
|
||||||
return Some(Ok(IntermediateValue::Value(Value::Dimension(
|
return Some(Ok(IntermediateValue::Value(Value::Dimension(
|
||||||
Number::new_machine(n),
|
Number::new_small(n),
|
||||||
unit,
|
unit,
|
||||||
))
|
))
|
||||||
.span(span)));
|
.span(span)));
|
||||||
@ -348,7 +348,7 @@ impl<'a> Parser<'a> {
|
|||||||
if val.num.len() <= 18 && val.times_ten.is_empty() {
|
if val.num.len() <= 18 && val.times_ten.is_empty() {
|
||||||
let n = Rational64::new(parse_i64(&val.num), pow(10, val.dec_len));
|
let n = Rational64::new(parse_i64(&val.num), pow(10, val.dec_len));
|
||||||
return Some(Ok(IntermediateValue::Value(Value::Dimension(
|
return Some(Ok(IntermediateValue::Value(Value::Dimension(
|
||||||
Number::new_machine(n),
|
Number::new_small(n),
|
||||||
unit,
|
unit,
|
||||||
))
|
))
|
||||||
.span(span)));
|
.span(span)));
|
||||||
|
@ -15,18 +15,18 @@ pub(crate) static UNIT_CONVERSION_TABLE: Lazy<
|
|||||||
let mut from_in = HashMap::new();
|
let mut from_in = HashMap::new();
|
||||||
from_in.insert("in", Number::one());
|
from_in.insert("in", Number::one());
|
||||||
from_in.insert("cm", Number::one() / Number::from(2.54));
|
from_in.insert("cm", Number::one() / Number::from(2.54));
|
||||||
from_in.insert("pc", Number::machine_ratio(1, 6));
|
from_in.insert("pc", Number::small_ratio(1, 6));
|
||||||
from_in.insert("mm", Number::one() / Number::from(25.4));
|
from_in.insert("mm", Number::one() / Number::from(25.4));
|
||||||
from_in.insert("q", Number::one() / Number::from(101.6));
|
from_in.insert("q", Number::one() / Number::from(101.6));
|
||||||
from_in.insert("pt", Number::machine_ratio(1, 72));
|
from_in.insert("pt", Number::small_ratio(1, 72));
|
||||||
from_in.insert("px", Number::machine_ratio(1, 96));
|
from_in.insert("px", Number::small_ratio(1, 96));
|
||||||
|
|
||||||
let mut from_cm = HashMap::new();
|
let mut from_cm = HashMap::new();
|
||||||
from_cm.insert("in", Number::from(2.54));
|
from_cm.insert("in", Number::from(2.54));
|
||||||
from_cm.insert("cm", Number::one());
|
from_cm.insert("cm", Number::one());
|
||||||
from_cm.insert("pc", Number::from(2.54) / Number::from(6));
|
from_cm.insert("pc", Number::from(2.54) / Number::from(6));
|
||||||
from_cm.insert("mm", Number::machine_ratio(1, 10));
|
from_cm.insert("mm", Number::small_ratio(1, 10));
|
||||||
from_cm.insert("q", Number::machine_ratio(1, 40));
|
from_cm.insert("q", Number::small_ratio(1, 40));
|
||||||
from_cm.insert("pt", Number::from(2.54) / Number::from(72));
|
from_cm.insert("pt", Number::from(2.54) / Number::from(72));
|
||||||
from_cm.insert("px", Number::from(2.54) / Number::from(96));
|
from_cm.insert("px", Number::from(2.54) / Number::from(96));
|
||||||
|
|
||||||
@ -36,15 +36,15 @@ pub(crate) static UNIT_CONVERSION_TABLE: Lazy<
|
|||||||
from_pc.insert("pc", Number::one());
|
from_pc.insert("pc", Number::one());
|
||||||
from_pc.insert("mm", Number::from(6) / Number::from(25.4));
|
from_pc.insert("mm", Number::from(6) / Number::from(25.4));
|
||||||
from_pc.insert("q", Number::from(6) / Number::from(101.6));
|
from_pc.insert("q", Number::from(6) / Number::from(101.6));
|
||||||
from_pc.insert("pt", Number::machine_ratio(1, 12));
|
from_pc.insert("pt", Number::small_ratio(1, 12));
|
||||||
from_pc.insert("px", Number::machine_ratio(1, 16));
|
from_pc.insert("px", Number::small_ratio(1, 16));
|
||||||
|
|
||||||
let mut from_mm = HashMap::new();
|
let mut from_mm = HashMap::new();
|
||||||
from_mm.insert("in", Number::from(25.4));
|
from_mm.insert("in", Number::from(25.4));
|
||||||
from_mm.insert("cm", Number::from(10));
|
from_mm.insert("cm", Number::from(10));
|
||||||
from_mm.insert("pc", Number::from(25.4) / Number::from(6));
|
from_mm.insert("pc", Number::from(25.4) / Number::from(6));
|
||||||
from_mm.insert("mm", Number::one());
|
from_mm.insert("mm", Number::one());
|
||||||
from_mm.insert("q", Number::machine_ratio(1, 4));
|
from_mm.insert("q", Number::small_ratio(1, 4));
|
||||||
from_mm.insert("pt", Number::from(25.4) / Number::from(72));
|
from_mm.insert("pt", Number::from(25.4) / Number::from(72));
|
||||||
from_mm.insert("px", Number::from(25.4) / Number::from(96));
|
from_mm.insert("px", Number::from(25.4) / Number::from(96));
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ pub(crate) static UNIT_CONVERSION_TABLE: Lazy<
|
|||||||
from_pt.insert("mm", Number::from(72) / Number::from(25.4));
|
from_pt.insert("mm", Number::from(72) / Number::from(25.4));
|
||||||
from_pt.insert("q", Number::from(72) / Number::from(101.6));
|
from_pt.insert("q", Number::from(72) / Number::from(101.6));
|
||||||
from_pt.insert("pt", Number::one());
|
from_pt.insert("pt", Number::one());
|
||||||
from_pt.insert("px", Number::machine_ratio(3, 4));
|
from_pt.insert("px", Number::small_ratio(3, 4));
|
||||||
|
|
||||||
let mut from_px = HashMap::new();
|
let mut from_px = HashMap::new();
|
||||||
from_px.insert("in", Number::from(96));
|
from_px.insert("in", Number::from(96));
|
||||||
@ -72,17 +72,17 @@ pub(crate) static UNIT_CONVERSION_TABLE: Lazy<
|
|||||||
from_px.insert("pc", Number::from(16));
|
from_px.insert("pc", Number::from(16));
|
||||||
from_px.insert("mm", Number::from(96) / Number::from(25.4));
|
from_px.insert("mm", Number::from(96) / Number::from(25.4));
|
||||||
from_px.insert("q", Number::from(96) / Number::from(101.6));
|
from_px.insert("q", Number::from(96) / Number::from(101.6));
|
||||||
from_px.insert("pt", Number::machine_ratio(4, 3));
|
from_px.insert("pt", Number::small_ratio(4, 3));
|
||||||
from_px.insert("px", Number::one());
|
from_px.insert("px", Number::one());
|
||||||
|
|
||||||
let mut from_deg = HashMap::new();
|
let mut from_deg = HashMap::new();
|
||||||
from_deg.insert("deg", Number::one());
|
from_deg.insert("deg", Number::one());
|
||||||
from_deg.insert("grad", Number::machine_ratio(9, 10));
|
from_deg.insert("grad", Number::small_ratio(9, 10));
|
||||||
from_deg.insert("rad", Number::from(180) / Number::from(PI));
|
from_deg.insert("rad", Number::from(180) / Number::from(PI));
|
||||||
from_deg.insert("turn", Number::from(360));
|
from_deg.insert("turn", Number::from(360));
|
||||||
|
|
||||||
let mut from_grad = HashMap::new();
|
let mut from_grad = HashMap::new();
|
||||||
from_grad.insert("deg", Number::machine_ratio(10, 9));
|
from_grad.insert("deg", Number::small_ratio(10, 9));
|
||||||
from_grad.insert("grad", Number::one());
|
from_grad.insert("grad", Number::one());
|
||||||
from_grad.insert("rad", Number::from(200) / Number::from(PI));
|
from_grad.insert("rad", Number::from(200) / Number::from(PI));
|
||||||
from_grad.insert("turn", Number::from(400));
|
from_grad.insert("turn", Number::from(400));
|
||||||
@ -94,14 +94,14 @@ pub(crate) static UNIT_CONVERSION_TABLE: Lazy<
|
|||||||
from_rad.insert("turn", Number::from(2.0 * PI));
|
from_rad.insert("turn", Number::from(2.0 * PI));
|
||||||
|
|
||||||
let mut from_turn = HashMap::new();
|
let mut from_turn = HashMap::new();
|
||||||
from_turn.insert("deg", Number::machine_ratio(1, 360));
|
from_turn.insert("deg", Number::small_ratio(1, 360));
|
||||||
from_turn.insert("grad", Number::machine_ratio(1, 400));
|
from_turn.insert("grad", Number::small_ratio(1, 400));
|
||||||
from_turn.insert("rad", Number::one() / Number::from(2.0 * PI));
|
from_turn.insert("rad", Number::one() / Number::from(2.0 * PI));
|
||||||
from_turn.insert("turn", Number::one());
|
from_turn.insert("turn", Number::one());
|
||||||
|
|
||||||
let mut from_s = HashMap::new();
|
let mut from_s = HashMap::new();
|
||||||
from_s.insert("s", Number::one());
|
from_s.insert("s", Number::one());
|
||||||
from_s.insert("ms", Number::machine_ratio(1, 1000));
|
from_s.insert("ms", Number::small_ratio(1, 1000));
|
||||||
|
|
||||||
let mut from_ms = HashMap::new();
|
let mut from_ms = HashMap::new();
|
||||||
from_ms.insert("s", Number::from(1000));
|
from_ms.insert("s", Number::from(1000));
|
||||||
@ -112,7 +112,7 @@ pub(crate) static UNIT_CONVERSION_TABLE: Lazy<
|
|||||||
from_hz.insert("kHz", Number::from(1000));
|
from_hz.insert("kHz", Number::from(1000));
|
||||||
|
|
||||||
let mut from_khz = HashMap::new();
|
let mut from_khz = HashMap::new();
|
||||||
from_khz.insert("Hz", Number::machine_ratio(1, 1000));
|
from_khz.insert("Hz", Number::small_ratio(1, 1000));
|
||||||
from_khz.insert("kHz", Number::one());
|
from_khz.insert("kHz", Number::one());
|
||||||
|
|
||||||
let mut from_dpi = HashMap::new();
|
let mut from_dpi = HashMap::new();
|
||||||
@ -126,7 +126,7 @@ pub(crate) static UNIT_CONVERSION_TABLE: Lazy<
|
|||||||
from_dpcm.insert("dppx", Number::from(96) / Number::from(2.54));
|
from_dpcm.insert("dppx", Number::from(96) / Number::from(2.54));
|
||||||
|
|
||||||
let mut from_dppx = HashMap::new();
|
let mut from_dppx = HashMap::new();
|
||||||
from_dppx.insert("dpi", Number::machine_ratio(1, 96));
|
from_dppx.insert("dpi", Number::small_ratio(1, 96));
|
||||||
from_dppx.insert("dpcm", Number::from(2.54) / Number::from(96));
|
from_dppx.insert("dpcm", Number::from(2.54) / Number::from(96));
|
||||||
from_dppx.insert("dppx", Number::one());
|
from_dppx.insert("dppx", Number::one());
|
||||||
|
|
||||||
|
@ -9,22 +9,22 @@ use num_traits::{Signed, ToPrimitive, Zero};
|
|||||||
|
|
||||||
#[derive(Eq, PartialEq)]
|
#[derive(Eq, PartialEq)]
|
||||||
pub(crate) enum Integer {
|
pub(crate) enum Integer {
|
||||||
Machine(i64),
|
Small(i64),
|
||||||
Big(BigInt),
|
Big(BigInt),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Integer {
|
impl Integer {
|
||||||
pub fn abs(&self) -> Self {
|
pub fn abs(&self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => Self::Machine(v.abs()),
|
Self::Small(v) => Self::Small(v.abs()),
|
||||||
Self::Big(v) => Self::Big(v.abs()),
|
Self::Big(v) => Self::Big(v.abs()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_ten(&self) -> bool {
|
pub fn is_ten(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(10) => true,
|
Self::Small(10) => true,
|
||||||
Self::Machine(..) => false,
|
Self::Small(..) => false,
|
||||||
Self::Big(v) => v == &BigInt::from(10),
|
Self::Big(v) => v == &BigInt::from(10),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ impl Default for Integer {
|
|||||||
impl UpperHex for Integer {
|
impl UpperHex for Integer {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => write!(f, "{:02X}", v),
|
Self::Small(v) => write!(f, "{:02X}", v),
|
||||||
Self::Big(v) => write!(f, "{:02X}", v),
|
Self::Big(v) => write!(f, "{:02X}", v),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,16 +49,16 @@ impl Add for Integer {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn add(self, rhs: Self) -> Self::Output {
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => match rhs {
|
Self::Small(val1) => match rhs {
|
||||||
Self::Machine(val2) => match val1.checked_add(val2) {
|
Self::Small(val2) => match val1.checked_add(val2) {
|
||||||
Some(v) => Self::Machine(v),
|
Some(v) => Self::Small(v),
|
||||||
None => Self::Big(BigInt::from(val1) + val2),
|
None => Self::Big(BigInt::from(val1) + val2),
|
||||||
},
|
},
|
||||||
Self::Big(val2) => Self::Big(BigInt::from(val1) + val2),
|
Self::Big(val2) => Self::Big(BigInt::from(val1) + val2),
|
||||||
},
|
},
|
||||||
Self::Big(val1) => match rhs {
|
Self::Big(val1) => match rhs {
|
||||||
Self::Big(val2) => Self::Big(val1 + val2),
|
Self::Big(val2) => Self::Big(val1 + val2),
|
||||||
Self::Machine(val2) => Self::Big(val1 + BigInt::from(val2)),
|
Self::Small(val2) => Self::Big(val1 + BigInt::from(val2)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ impl Add<BigInt> for Integer {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn add(self, rhs: BigInt) -> Self::Output {
|
fn add(self, rhs: BigInt) -> Self::Output {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => Self::Big(BigInt::from(v) + rhs),
|
Self::Small(v) => Self::Big(BigInt::from(v) + rhs),
|
||||||
Self::Big(v) => Self::Big(v + rhs),
|
Self::Big(v) => Self::Big(v + rhs),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,8 +78,8 @@ impl Add<i32> for Integer {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn add(self, rhs: i32) -> Self::Output {
|
fn add(self, rhs: i32) -> Self::Output {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => match v.checked_add(i64::from(rhs)) {
|
Self::Small(v) => match v.checked_add(i64::from(rhs)) {
|
||||||
Some(v) => Self::Machine(v),
|
Some(v) => Self::Small(v),
|
||||||
None => Self::Big(BigInt::from(v) + rhs),
|
None => Self::Big(BigInt::from(v) + rhs),
|
||||||
},
|
},
|
||||||
Self::Big(v) => Self::Big(v + rhs),
|
Self::Big(v) => Self::Big(v + rhs),
|
||||||
@ -103,13 +103,13 @@ impl AddAssign for Integer {
|
|||||||
|
|
||||||
impl Zero for Integer {
|
impl Zero for Integer {
|
||||||
fn zero() -> Self {
|
fn zero() -> Self {
|
||||||
Self::Machine(0)
|
Self::Small(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_zero(&self) -> bool {
|
fn is_zero(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(0) => true,
|
Self::Small(0) => true,
|
||||||
Self::Machine(..) => false,
|
Self::Small(..) => false,
|
||||||
Self::Big(v) => v.is_zero(),
|
Self::Big(v) => v.is_zero(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,7 +122,7 @@ impl Zero for Integer {
|
|||||||
impl Display for Integer {
|
impl Display for Integer {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => write!(f, "{}", v),
|
Self::Small(v) => write!(f, "{}", v),
|
||||||
Self::Big(v) => write!(f, "{}", v),
|
Self::Big(v) => write!(f, "{}", v),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,21 +131,21 @@ impl Display for Integer {
|
|||||||
impl ToPrimitive for Integer {
|
impl ToPrimitive for Integer {
|
||||||
fn to_u8(&self) -> Option<u8> {
|
fn to_u8(&self) -> Option<u8> {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => v.to_u8(),
|
Self::Small(v) => v.to_u8(),
|
||||||
Self::Big(v) => v.to_u8(),
|
Self::Big(v) => v.to_u8(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_u64(&self) -> Option<u64> {
|
fn to_u64(&self) -> Option<u64> {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => v.to_u64(),
|
Self::Small(v) => v.to_u64(),
|
||||||
Self::Big(v) => v.to_u64(),
|
Self::Big(v) => v.to_u64(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_i64(&self) -> std::option::Option<i64> {
|
fn to_i64(&self) -> std::option::Option<i64> {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => Some(*v),
|
Self::Small(v) => Some(*v),
|
||||||
Self::Big(v) => v.to_i64(),
|
Self::Big(v) => v.to_i64(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,13 @@ const PRECISION: usize = 10;
|
|||||||
|
|
||||||
#[derive(Clone, Eq, PartialEq, Ord)]
|
#[derive(Clone, Eq, PartialEq, Ord)]
|
||||||
pub(crate) enum Number {
|
pub(crate) enum Number {
|
||||||
Machine(Rational64),
|
Small(Rational64),
|
||||||
Big(Box<BigRational>),
|
Big(Box<BigRational>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Number {
|
impl Number {
|
||||||
pub const fn new_machine(val: Rational64) -> Number {
|
pub const fn new_small(val: Rational64) -> Number {
|
||||||
Number::Machine(val)
|
Number::Small(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_big(val: BigRational) -> Number {
|
pub fn new_big(val: BigRational) -> Number {
|
||||||
@ -33,13 +33,13 @@ impl Number {
|
|||||||
|
|
||||||
pub fn to_integer(&self) -> Integer {
|
pub fn to_integer(&self) -> Integer {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val) => Integer::Machine(val.to_integer()),
|
Self::Small(val) => Integer::Small(val.to_integer()),
|
||||||
Self::Big(val) => Integer::Big(val.to_integer()),
|
Self::Big(val) => Integer::Big(val.to_integer()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn machine_ratio<A: Into<i64>, B: Into<i64>>(a: A, b: B) -> Self {
|
pub fn small_ratio<A: Into<i64>, B: Into<i64>>(a: A, b: B) -> Self {
|
||||||
Number::new_machine(Rational64::new(a.into(), b.into()))
|
Number::new_small(Rational64::new(a.into(), b.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -49,42 +49,42 @@ impl Number {
|
|||||||
|
|
||||||
pub fn round(&self) -> Self {
|
pub fn round(&self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val) => Self::Machine(val.round()),
|
Self::Small(val) => Self::Small(val.round()),
|
||||||
Self::Big(val) => Self::Big(Box::new(val.round())),
|
Self::Big(val) => Self::Big(Box::new(val.round())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ceil(&self) -> Self {
|
pub fn ceil(&self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val) => Self::Machine(val.ceil()),
|
Self::Small(val) => Self::Small(val.ceil()),
|
||||||
Self::Big(val) => Self::Big(Box::new(val.ceil())),
|
Self::Big(val) => Self::Big(Box::new(val.ceil())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn floor(&self) -> Self {
|
pub fn floor(&self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val) => Self::Machine(val.floor()),
|
Self::Small(val) => Self::Small(val.floor()),
|
||||||
Self::Big(val) => Self::Big(Box::new(val.floor())),
|
Self::Big(val) => Self::Big(Box::new(val.floor())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn abs(&self) -> Self {
|
pub fn abs(&self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val) => Self::Machine(val.abs()),
|
Self::Small(val) => Self::Small(val.abs()),
|
||||||
Self::Big(val) => Self::Big(Box::new(val.abs())),
|
Self::Big(val) => Self::Big(Box::new(val.abs())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_decimal(&self) -> bool {
|
pub fn is_decimal(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => !v.is_integer(),
|
Self::Small(v) => !v.is_integer(),
|
||||||
Self::Big(v) => !v.is_integer(),
|
Self::Big(v) => !v.is_integer(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fract(&mut self) -> Number {
|
pub fn fract(&mut self) -> Number {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => Number::new_machine(v.fract()),
|
Self::Small(v) => Number::new_small(v.fract()),
|
||||||
Self::Big(v) => Number::new_big(v.fract()),
|
Self::Big(v) => Number::new_big(v.fract()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,12 +116,12 @@ impl Default for Number {
|
|||||||
|
|
||||||
impl Zero for Number {
|
impl Zero for Number {
|
||||||
fn zero() -> Self {
|
fn zero() -> Self {
|
||||||
Number::new_machine(Rational64::from_integer(0))
|
Number::new_small(Rational64::from_integer(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_zero(&self) -> bool {
|
fn is_zero(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => v.is_zero(),
|
Self::Small(v) => v.is_zero(),
|
||||||
Self::Big(v) => v.is_zero(),
|
Self::Big(v) => v.is_zero(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,12 +129,12 @@ impl Zero for Number {
|
|||||||
|
|
||||||
impl One for Number {
|
impl One for Number {
|
||||||
fn one() -> Self {
|
fn one() -> Self {
|
||||||
Number::new_machine(Rational64::from_integer(1))
|
Number::new_small(Rational64::from_integer(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_one(&self) -> bool {
|
fn is_one(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => v.is_one(),
|
Self::Small(v) => v.is_one(),
|
||||||
Self::Big(v) => v.is_one(),
|
Self::Big(v) => v.is_one(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -171,14 +171,14 @@ impl Signed for Number {
|
|||||||
|
|
||||||
fn is_positive(&self) -> bool {
|
fn is_positive(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => v.is_positive(),
|
Self::Small(v) => v.is_positive(),
|
||||||
Self::Big(v) => v.is_positive(),
|
Self::Big(v) => v.is_positive(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_negative(&self) -> bool {
|
fn is_negative(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => v.is_negative(),
|
Self::Small(v) => v.is_negative(),
|
||||||
Self::Big(v) => v.is_negative(),
|
Self::Big(v) => v.is_negative(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -189,7 +189,7 @@ macro_rules! from_integer {
|
|||||||
impl From<$ty> for Number {
|
impl From<$ty> for Number {
|
||||||
fn from(b: $ty) -> Self {
|
fn from(b: $ty) -> Self {
|
||||||
if let Ok(v) = i64::try_from(b) {
|
if let Ok(v) = i64::try_from(b) {
|
||||||
Number::Machine(Rational64::from_integer(v))
|
Number::Small(Rational64::from_integer(v))
|
||||||
} else {
|
} else {
|
||||||
Number::Big(Box::new(BigRational::from_integer(BigInt::from(b))))
|
Number::Big(Box::new(BigRational::from_integer(BigInt::from(b))))
|
||||||
}
|
}
|
||||||
@ -202,7 +202,7 @@ macro_rules! from_smaller_integer {
|
|||||||
($ty:ty) => {
|
($ty:ty) => {
|
||||||
impl From<$ty> for Number {
|
impl From<$ty> for Number {
|
||||||
fn from(val: $ty) -> Self {
|
fn from(val: $ty) -> Self {
|
||||||
Number::new_machine(Rational64::from_integer(val as i64))
|
Number::new_small(Rational64::from_integer(val as i64))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -210,7 +210,7 @@ macro_rules! from_smaller_integer {
|
|||||||
|
|
||||||
impl From<i64> for Number {
|
impl From<i64> for Number {
|
||||||
fn from(val: i64) -> Self {
|
fn from(val: i64) -> Self {
|
||||||
Number::new_machine(Rational64::from_integer(val))
|
Number::new_small(Rational64::from_integer(val))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,7 +230,7 @@ from_smaller_integer!(u8);
|
|||||||
impl fmt::Debug for Number {
|
impl fmt::Debug for Number {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(..) => write!(f, "Number::Machine( {} )", self),
|
Self::Small(..) => write!(f, "Number::Small( {} )", self),
|
||||||
Self::Big(..) => write!(f, "Number::Big( {} )", self),
|
Self::Big(..) => write!(f, "Number::Big( {} )", self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -299,8 +299,8 @@ impl Display for Number {
|
|||||||
impl PartialOrd for Number {
|
impl PartialOrd for Number {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => match other {
|
Self::Small(val1) => match other {
|
||||||
Self::Machine(val2) => val1.partial_cmp(val2),
|
Self::Small(val2) => val1.partial_cmp(val2),
|
||||||
Self::Big(val2) => {
|
Self::Big(val2) => {
|
||||||
let tuple: (i64, i64) = (*val1).into();
|
let tuple: (i64, i64) = (*val1).into();
|
||||||
BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1))
|
BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1))
|
||||||
@ -308,7 +308,7 @@ impl PartialOrd for Number {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Self::Big(val1) => match other {
|
Self::Big(val1) => match other {
|
||||||
Self::Machine(val2) => {
|
Self::Small(val2) => {
|
||||||
let tuple: (i64, i64) = (*val2).into();
|
let tuple: (i64, i64) = (*val2).into();
|
||||||
(**val1).partial_cmp(&BigRational::new_raw(
|
(**val1).partial_cmp(&BigRational::new_raw(
|
||||||
BigInt::from(tuple.0),
|
BigInt::from(tuple.0),
|
||||||
@ -326,9 +326,9 @@ impl Add for Number {
|
|||||||
|
|
||||||
fn add(self, other: Self) -> Self {
|
fn add(self, other: Self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => match other {
|
Self::Small(val1) => match other {
|
||||||
Self::Machine(val2) => match val1.checked_add(&val2) {
|
Self::Small(val2) => match val1.checked_add(&val2) {
|
||||||
Some(v) => Self::Machine(v),
|
Some(v) => Self::Small(v),
|
||||||
None => {
|
None => {
|
||||||
let tuple1: (i64, i64) = val1.into();
|
let tuple1: (i64, i64) = val1.into();
|
||||||
let tuple2: (i64, i64) = val2.into();
|
let tuple2: (i64, i64) = val2.into();
|
||||||
@ -350,7 +350,7 @@ impl Add for Number {
|
|||||||
},
|
},
|
||||||
Self::Big(val1) => match other {
|
Self::Big(val1) => match other {
|
||||||
Self::Big(val2) => Self::Big(Box::new(*val1 + *val2)),
|
Self::Big(val2) => Self::Big(Box::new(*val1 + *val2)),
|
||||||
Self::Machine(val2) => {
|
Self::Small(val2) => {
|
||||||
let tuple: (i64, i64) = val2.into();
|
let tuple: (i64, i64) = val2.into();
|
||||||
Self::Big(Box::new(
|
Self::Big(Box::new(
|
||||||
(*val1)
|
(*val1)
|
||||||
@ -367,9 +367,9 @@ impl Add<&Self> for Number {
|
|||||||
|
|
||||||
fn add(self, other: &Self) -> Self {
|
fn add(self, other: &Self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => match other {
|
Self::Small(val1) => match other {
|
||||||
Self::Machine(val2) => match val1.checked_add(val2) {
|
Self::Small(val2) => match val1.checked_add(val2) {
|
||||||
Some(v) => Self::Machine(v),
|
Some(v) => Self::Small(v),
|
||||||
None => {
|
None => {
|
||||||
let tuple1: (i64, i64) = val1.into();
|
let tuple1: (i64, i64) = val1.into();
|
||||||
let tuple2: (i64, i64) = (*val2).into();
|
let tuple2: (i64, i64) = (*val2).into();
|
||||||
@ -392,7 +392,7 @@ impl Add<&Self> for Number {
|
|||||||
},
|
},
|
||||||
Self::Big(val1) => match other {
|
Self::Big(val1) => match other {
|
||||||
Self::Big(val2) => Self::Big(Box::new(*val1 + *val2.clone())),
|
Self::Big(val2) => Self::Big(Box::new(*val1 + *val2.clone())),
|
||||||
Self::Machine(val2) => {
|
Self::Small(val2) => {
|
||||||
let tuple: (i64, i64) = (*val2).into();
|
let tuple: (i64, i64) = (*val2).into();
|
||||||
Self::Big(Box::new(
|
Self::Big(Box::new(
|
||||||
*val1 + BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
*val1 + BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
||||||
@ -415,9 +415,9 @@ impl Sub for Number {
|
|||||||
|
|
||||||
fn sub(self, other: Self) -> Self {
|
fn sub(self, other: Self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => match other {
|
Self::Small(val1) => match other {
|
||||||
Self::Machine(val2) => match val1.checked_sub(&val2) {
|
Self::Small(val2) => match val1.checked_sub(&val2) {
|
||||||
Some(v) => Self::Machine(v),
|
Some(v) => Self::Small(v),
|
||||||
None => {
|
None => {
|
||||||
let tuple1: (i64, i64) = val1.into();
|
let tuple1: (i64, i64) = val1.into();
|
||||||
let tuple2: (i64, i64) = val2.into();
|
let tuple2: (i64, i64) = val2.into();
|
||||||
@ -439,7 +439,7 @@ impl Sub for Number {
|
|||||||
},
|
},
|
||||||
Self::Big(val1) => match other {
|
Self::Big(val1) => match other {
|
||||||
Self::Big(val2) => Self::Big(Box::new(*val1 - *val2)),
|
Self::Big(val2) => Self::Big(Box::new(*val1 - *val2)),
|
||||||
Self::Machine(val2) => {
|
Self::Small(val2) => {
|
||||||
let tuple: (i64, i64) = val2.into();
|
let tuple: (i64, i64) = val2.into();
|
||||||
Self::Big(Box::new(
|
Self::Big(Box::new(
|
||||||
*val1 - BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
*val1 - BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
||||||
@ -462,9 +462,9 @@ impl Mul for Number {
|
|||||||
|
|
||||||
fn mul(self, other: Self) -> Self {
|
fn mul(self, other: Self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => match other {
|
Self::Small(val1) => match other {
|
||||||
Self::Machine(val2) => match val1.checked_mul(&val2) {
|
Self::Small(val2) => match val1.checked_mul(&val2) {
|
||||||
Some(v) => Self::Machine(v),
|
Some(v) => Self::Small(v),
|
||||||
None => {
|
None => {
|
||||||
let tuple1: (i64, i64) = val1.into();
|
let tuple1: (i64, i64) = val1.into();
|
||||||
let tuple2: (i64, i64) = val2.into();
|
let tuple2: (i64, i64) = val2.into();
|
||||||
@ -486,7 +486,7 @@ impl Mul for Number {
|
|||||||
},
|
},
|
||||||
Self::Big(val1) => match other {
|
Self::Big(val1) => match other {
|
||||||
Self::Big(val2) => Self::Big(Box::new(*val1 * *val2)),
|
Self::Big(val2) => Self::Big(Box::new(*val1 * *val2)),
|
||||||
Self::Machine(val2) => {
|
Self::Small(val2) => {
|
||||||
let tuple: (i64, i64) = val2.into();
|
let tuple: (i64, i64) = val2.into();
|
||||||
Self::Big(Box::new(
|
Self::Big(Box::new(
|
||||||
*val1 * BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
*val1 * BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
||||||
@ -502,7 +502,7 @@ impl Mul<i64> for Number {
|
|||||||
|
|
||||||
fn mul(self, other: i64) -> Self {
|
fn mul(self, other: i64) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => Self::Machine(val1 * other),
|
Self::Small(val1) => Self::Small(val1 * other),
|
||||||
Self::Big(val1) => Self::Big(Box::new(*val1 * BigInt::from(other))),
|
Self::Big(val1) => Self::Big(Box::new(*val1 * BigInt::from(other))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -527,9 +527,9 @@ impl Div for Number {
|
|||||||
|
|
||||||
fn div(self, other: Self) -> Self {
|
fn div(self, other: Self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => match other {
|
Self::Small(val1) => match other {
|
||||||
Self::Machine(val2) => match val1.checked_div(&val2) {
|
Self::Small(val2) => match val1.checked_div(&val2) {
|
||||||
Some(v) => Self::Machine(v),
|
Some(v) => Self::Small(v),
|
||||||
None => {
|
None => {
|
||||||
let tuple1: (i64, i64) = val1.into();
|
let tuple1: (i64, i64) = val1.into();
|
||||||
let tuple2: (i64, i64) = val2.into();
|
let tuple2: (i64, i64) = val2.into();
|
||||||
@ -551,7 +551,7 @@ impl Div for Number {
|
|||||||
},
|
},
|
||||||
Self::Big(val1) => match other {
|
Self::Big(val1) => match other {
|
||||||
Self::Big(val2) => Self::Big(Box::new(*val1 / *val2)),
|
Self::Big(val2) => Self::Big(Box::new(*val1 / *val2)),
|
||||||
Self::Machine(val2) => {
|
Self::Small(val2) => {
|
||||||
let tuple: (i64, i64) = val2.into();
|
let tuple: (i64, i64) = val2.into();
|
||||||
Self::Big(Box::new(
|
Self::Big(Box::new(
|
||||||
*val1 / BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
*val1 / BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
||||||
@ -574,9 +574,9 @@ impl Rem for Number {
|
|||||||
|
|
||||||
fn rem(self, other: Self) -> Self {
|
fn rem(self, other: Self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(val1) => match other {
|
Self::Small(val1) => match other {
|
||||||
// todo: checked_rem for ratio?
|
// todo: checked_rem for ratio?
|
||||||
Self::Machine(val2) => {
|
Self::Small(val2) => {
|
||||||
let tuple1: (i64, i64) = val1.into();
|
let tuple1: (i64, i64) = val1.into();
|
||||||
let tuple2: (i64, i64) = val2.into();
|
let tuple2: (i64, i64) = val2.into();
|
||||||
Self::Big(Box::new(
|
Self::Big(Box::new(
|
||||||
@ -593,7 +593,7 @@ impl Rem for Number {
|
|||||||
},
|
},
|
||||||
Self::Big(val1) => match other {
|
Self::Big(val1) => match other {
|
||||||
Self::Big(val2) => Self::Big(Box::new(*val1 % *val2)),
|
Self::Big(val2) => Self::Big(Box::new(*val1 % *val2)),
|
||||||
Self::Machine(val2) => {
|
Self::Small(val2) => {
|
||||||
let tuple: (i64, i64) = val2.into();
|
let tuple: (i64, i64) = val2.into();
|
||||||
Self::Big(Box::new(
|
Self::Big(Box::new(
|
||||||
*val1 % BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
*val1 % BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1)),
|
||||||
@ -616,7 +616,7 @@ impl Neg for Number {
|
|||||||
|
|
||||||
fn neg(self) -> Self {
|
fn neg(self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::Machine(v) => Self::Machine(-v),
|
Self::Small(v) => Self::Small(-v),
|
||||||
Self::Big(v) => Self::Big(Box::new(-*v)),
|
Self::Big(v) => Self::Big(Box::new(-*v)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user