Skip to content

Commit

Permalink
Prepare the next release 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dnsl48 committed Oct 13, 2022
1 parent ea53eb4 commit 55f9760
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 259 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Change Log

## [0.12.0]
## [0.12.0] - 2022-10-13

### Changed
- `num` version `0.4` is now required (`0.2`, `0.3` are no longer supported)
- Multiple functions made const in GenericFraction, GenericDecimal and fraction::display::Format
Special thanks to Stijn Frishert (aka stijnfrishert).

### Deprecated
- GenericDecimal::apply_ref has been deprecated
- fn `decimal::GenericDecimal::apply_ref` is deprecated.

### Removed
- Removed deprecated fn `decimal::GenericDecimal::from_decimal_str`. Use `FromStr::from_str` instead.
- Removed deprecated fn `fraction::GenericFraction::from_decimal_str`. Use `FromStr::from_str` instead.
- Removed deprecated fn `fraction::GenericFraction::format_as_decimal`. Use `format!(\"{:.1$}\", fraction, precision)` instead.
- Removed deprecated fn `fraction::GenericFraction::new_raw_neg`. Use `new_raw_signed` instead.

## [0.11.2] - 2022-09-18
- `DynaInt` now implements serde `Serialize & Unserialize` (Thanks to Richard Davies aka @optevo for the contribution!)
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fraction"
version = "0.11.2"
version = "0.12.0"
authors = ["dnsl48 <dnsl48@gmail.com>"]

description = "Lossless fractions and decimals; drop-in float replacement"
Expand All @@ -24,7 +24,7 @@ all-features = true

[dependencies]

num = { version = ">=0.2,<0.5", default-features = false }
num = { version = "0.4", default-features = false }

byteorder = { version = "1", optional = true }
bytes = { version = "1", optional = true }
Expand All @@ -50,7 +50,7 @@ with-postgres-support = ["postgres-types", "byteorder", "bytes"]
with-serde-support = ["serde", "serde_derive", "num/serde"]

[dev-dependencies]
criterion = "0.3"
criterion = "0.4"

[[bench]]
name = "bench_fraction"
Expand Down
39 changes: 0 additions & 39 deletions src/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,45 +1066,6 @@ where
}
}

#[deprecated(note = "Use `FromStr::from_str` instead")]
pub fn from_decimal_str(val: &str) -> Result<Self, error::ParseError>
where
T: CheckedAdd + CheckedMul + CheckedSub,
P: From<u8> + CheckedAdd,
{
if val == "NaN" {
Ok(Self::nan())
} else if val == "-inf" {
Ok(Self::neg_infinity())
} else if val == "+inf" || val == "inf" {
Ok(Self::infinity())
} else {
let precision: P = if let Some(split_idx) = val.find('.') {
let mut prec_iter = val.len() - split_idx - 1;
let mut precision: P = P::zero();

loop {
if prec_iter == 0 {
break;
}
prec_iter -= 1;

if let Some(p) = precision.checked_add(&P::one()) {
precision = p;
} else {
break;
}
}

precision
} else {
P::zero()
};

Ok(GenericDecimal::from_str_radix(val, 10)?.set_precision(precision))
}
}

/// Convert from a GenericFraction
///
/// Automatically calculates precision, so for "bad" numbers
Expand Down
215 changes: 0 additions & 215 deletions src/fraction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,6 @@ where
GenericFraction::Rational(Sign::Plus, Ratio::new_raw(num, den))
}

/// The same as [fn new_raw](enum.GenericFraction.html#method.new_raw), but produces negative fractions.
///
/// DEPRECATED! Use [fn new_raw_signed](enum.GenericFraction.html#method.new_raw_signed) instead
///
/// # Examples
///
/// ```
/// use fraction::GenericFraction;
/// type F = GenericFraction<u8>;
///
/// let _f = F::new_raw_neg (1u8, 2u8);
/// ```
#[deprecated(note = "Use `new_raw_signed` instead")]
pub const fn new_raw_neg(num: T, den: T) -> GenericFraction<T> {
GenericFraction::Rational(Sign::Minus, Ratio::new_raw(num, den))
}

/// The same as [fn new_raw](enum.GenericFraction.html#method.new_raw), but allows explicitly set sign.
///
/// # Examples
Expand Down Expand Up @@ -519,116 +502,6 @@ where
}
}
}

/// Returns a decimal representation of the fraction
///
/// DEPRECATED! Use `format!("{:.1$}", fraction, precision)` instead
///
/// If you have a fraction "1/2", in decimal it should be "0.5".
///
/// Returns None in case we couldn't write the result into a string,
/// e.g. not enough RAM.
///
/// # Examples
///
/// ```
/// use fraction::GenericFraction;
/// type F = GenericFraction<u8>;
///
/// assert_eq! ("0.5", &F::new (1u8, 2u8).format_as_decimal (1).unwrap ());
/// assert_eq! ("0.8", &F::new (8u8, 10u8).format_as_decimal (2).unwrap ());
/// assert_eq! (&F::new (1u8, 3u8).format_as_decimal(32).unwrap(), "0.33333333333333333333333333333333");
/// ```
#[deprecated(note = "Use `format!(\"{:.1$}\", fraction, precision)`")]
pub fn format_as_decimal(&self, precision: usize) -> Option<String>
where
T: Clone + GenericInteger,
{
Some(format!("{:.1$}", &self, precision))
}

/// Parse a decimal string into a fraction and return the result.
/// Returns ParseError::OverflowError if there's not enough space in T to represent the decimal (use BigFraction in such a case)
/// Returns ParseError::ParseIntError if the string contains incorrect junk data (e.g. non-numeric characters).
/// May return ParseIntError if there is not enough volume in T to read whole part of the number into it.
///
/// # Examples
///
/// ```
/// use fraction::Fraction;
///
/// let f = Fraction::from_decimal_str ("1.5");
/// assert_eq! (f, Ok (Fraction::new(3u8, 2u8)));
/// ```
#[deprecated(note = "Use `FromStr::from_str` instead")]
pub fn from_decimal_str(src: &str) -> Result<Self, ParseError>
where
T: CheckedAdd + CheckedMul,
{
let (sign, start) = if src.starts_with('-') {
(Sign::Minus, 1)
} else if src.starts_with('+') {
(Sign::Plus, 1)
} else {
(Sign::Plus, 0)
};

let dot = src.find('.');
let who = match dot {
Some(dot) => &src[start..dot],
None => &src[start..],
};

let mut num = match T::from_str_radix(who, 10) {
Err(_) => return Err(ParseError::ParseIntError),
Ok(value) => value,
};

let (fra, len) = if let Some(dot) = dot {
(T::from_str_radix(&src[dot + 1..], 10), src.len() - dot - 1)
} else {
(Ok(T::zero()), 0)
};

let fra = match fra {
Err(_) => return Err(ParseError::ParseIntError),
Ok(value) => value,
};

let mut den = T::one();

if len > 0 {
let mut t10 = T::one();
for _ in 0..9 {
t10 = if let Some(t10) = t10.checked_add(&den) {
t10
} else {
return Err(ParseError::OverflowError);
};
}

for _ in 0..len {
num = if let Some(num) = num.checked_mul(&t10) {
num
} else {
return Err(ParseError::OverflowError);
};
den = if let Some(den) = den.checked_mul(&t10) {
den
} else {
return Err(ParseError::OverflowError);
};
}
}

let num = if let Some(num) = num.checked_add(&fra) {
num
} else {
return Err(ParseError::OverflowError);
};

Ok(GenericFraction::Rational(sign, Ratio::new(num, den)))
}
}

impl<T: Bounded + Clone + Integer> Bounded for GenericFraction<T> {
Expand Down Expand Up @@ -2990,94 +2863,6 @@ mod tests {
assert!(Frac::zero() < Frac::new(1u8, 2u8));
}

#[test]
fn from_decimal_str() {
#![allow(deprecated)]

assert_eq!(Ok(Frac::zero()), Frac::from_decimal_str("0"));
assert_eq!(Ok(Frac::zero()), Frac::from_decimal_str("-0"));
assert_eq!(Ok(Frac::zero()), Frac::from_decimal_str("+0"));

assert_eq!(Ok(Frac::zero()), Frac::from_decimal_str("0.0"));
assert_eq!(Ok(Frac::zero()), Frac::from_decimal_str("-0.0"));
assert_eq!(Ok(Frac::zero()), Frac::from_decimal_str("+0.0"));

assert_eq!(Ok(Fraction::zero()), Fraction::from_decimal_str("0.000000"));
assert_eq!(
Ok(Fraction::zero()),
Fraction::from_decimal_str("-0.000000")
);
assert_eq!(
Ok(Fraction::zero()),
Fraction::from_decimal_str("+0.000000")
);

#[cfg(feature = "with-bigint")]
{
assert_eq!(
Ok(BigFraction::zero()),
BigFraction::from_decimal_str(
"00000000000000000000000000.0000000000000000000000000000000000000000000"
)
);
assert_eq!(
Ok(BigFraction::zero()),
BigFraction::from_decimal_str(
"-0000000000000000000000000.0000000000000000000000000000000000000000000"
)
);
assert_eq!(
Ok(BigFraction::zero()),
BigFraction::from_decimal_str(
"+0000000000000000000000000.0000000000000000000000000000000000000000000"
)
);
}

assert_eq!(Ok(Frac::one()), Frac::from_decimal_str("1"));
assert_eq!(Ok(Frac::new_neg(1, 1)), Frac::from_decimal_str("-1"));
assert_eq!(Ok(Frac::one()), Frac::from_decimal_str("+1"));

assert_eq!(Ok(Frac::one()), Frac::from_decimal_str("1.0"));
assert_eq!(Ok(Frac::new_neg(1, 1)), Frac::from_decimal_str("-1.0"));
assert_eq!(Ok(Frac::one()), Frac::from_decimal_str("+1.00"));

assert_eq!(Ok(Frac::new(1, 2)), Frac::from_decimal_str("0.5"));

assert_eq!(
Ok(Fraction::new(3333u64, 5000u64)),
Fraction::from_decimal_str("0.6666")
);

assert_eq!(
Err(ParseError::ParseIntError),
Frac::from_decimal_str("test")
);
assert_eq!(
Err(ParseError::ParseIntError),
Frac::from_decimal_str("1test")
);
assert_eq!(
Err(ParseError::ParseIntError),
Frac::from_decimal_str("1.26t8")
);

// this is due to the std library which issues ParseIntError on the whole part overflow
assert_eq!(
Err(ParseError::ParseIntError),
Frac::from_decimal_str("120202040")
);
assert_eq!(
Err(ParseError::ParseIntError),
Frac::from_decimal_str("1.20602604")
);

assert_eq!(
Err(ParseError::OverflowError),
Frac::from_decimal_str("255.255")
);
}

#[test]
fn from_str() {
assert_eq!(Ok(Frac::zero()), Frac::from_str("0"));
Expand Down

0 comments on commit 55f9760

Please sign in to comment.