Skip to content

Commit

Permalink
Deprecate FBig::from_str_native
Browse files Browse the repository at this point in the history
  • Loading branch information
cmpute committed Feb 3, 2024
1 parent b06745d commit 86bdae4
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 146 deletions.
3 changes: 2 additions & 1 deletion base/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased

- Re-implement functions `next_up` and `next_down`, and expose them through the `utils`` module.
- Mark `AbsEq` as deprecated.
- Re-implement functions `next_up` and `next_down`, and expose them through the `utils` module.

## 0.4.0

Expand Down
14 changes: 8 additions & 6 deletions float/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,16 @@ impl<R: Round> Context<R> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(2);
/// let a = DBig::from_str_native("1.234")?;
/// let b = DBig::from_str_native("6.789")?;
/// assert_eq!(context.add(&a.repr(), &b.repr()), Inexact(DBig::from_str_native("8.0")?, NoOp));
/// let a = DBig::from_str("1.234")?;
/// let b = DBig::from_str("6.789")?;
/// assert_eq!(context.add(&a.repr(), &b.repr()), Inexact(DBig::from_str("8.0")?, NoOp));
/// # Ok::<(), ParseError>(())
/// ```
pub fn add<const B: Word>(&self, lhs: &Repr<B>, rhs: &Repr<B>) -> Rounded<FBig<R, B>> {
Expand All @@ -485,17 +486,18 @@ impl<R: Round> Context<R> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(2);
/// let a = DBig::from_str_native("1.234")?;
/// let b = DBig::from_str_native("6.789")?;
/// let a = DBig::from_str("1.234")?;
/// let b = DBig::from_str("6.789")?;
/// assert_eq!(
/// context.sub(&a.repr(), &b.repr()),
/// Inexact(DBig::from_str_native("-5.6")?, SubOne)
/// Inexact(DBig::from_str("-5.6")?, SubOne)
/// );
/// # Ok::<(), ParseError>(())
/// ```
Expand Down
72 changes: 41 additions & 31 deletions float/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ impl<R: Round> Context<R> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(2);
/// assert_eq!(context.convert_int::<10>((-12).into()), Exact(DBig::from_str_native("-12")?));
/// assert_eq!(context.convert_int::<10>((-12).into()), Exact(DBig::from_str("-12")?));
/// assert_eq!(
/// context.convert_int::<10>(5678.into()),
/// Inexact(DBig::from_str_native("5.7e3")?, AddOne)
/// Inexact(DBig::from_str("5.7e3")?, AddOne)
/// );
/// # Ok::<(), ParseError>(())
/// ```
Expand Down Expand Up @@ -104,6 +105,7 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::{FBig, DBig};
/// use dashu_base::Approximation::*;
Expand All @@ -112,16 +114,16 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// type Real = FBig;
///
/// assert_eq!(
/// Real::from_str_native("0x1234")?.to_decimal(),
/// Exact(DBig::from_str_native("4660")?)
/// Real::from_str("0x1234")?.to_decimal(),
/// Exact(DBig::from_str("4660")?)
/// );
/// assert_eq!(
/// Real::from_str_native("0x12.34")?.to_decimal(),
/// Inexact(DBig::from_str_native("18.20")?, NoOp)
/// Real::from_str("0x12.34")?.to_decimal(),
/// Inexact(DBig::from_str("18.20")?, NoOp)
/// );
/// assert_eq!(
/// Real::from_str_native("0x1.234p-4")?.to_decimal(),
/// Inexact(DBig::from_str_native("0.07111")?, AddOne)
/// Real::from_str("0x1.234p-4")?.to_decimal(),
/// Inexact(DBig::from_str("0.07111")?, AddOne)
/// );
/// # Ok::<(), ParseError>(())
/// ```
Expand All @@ -144,6 +146,7 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::{FBig, DBig};
/// use dashu_base::Approximation::*;
Expand All @@ -152,16 +155,16 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// type Real = FBig;
///
/// assert_eq!(
/// DBig::from_str_native("1234")?.to_binary(),
/// Exact(Real::from_str_native("0x4d2")?)
/// DBig::from_str("1234")?.to_binary(),
/// Exact(Real::from_str("0x4d2")?)
/// );
/// assert_eq!(
/// DBig::from_str_native("12.34")?.to_binary(),
/// Inexact(Real::from_str_native("0xc.57")?, NoOp)
/// DBig::from_str("12.34")?.to_binary(),
/// Inexact(Real::from_str("0xc.57")?, NoOp)
/// );
/// assert_eq!(
/// DBig::from_str_native("1.234e-1")?.to_binary(),
/// Inexact(Real::from_str_native("0x1.f97p-4")?, NoOp)
/// DBig::from_str("1.234e-1")?.to_binary(),
/// Inexact(Real::from_str("0x1.f97p-4")?, NoOp)
/// );
/// # Ok::<(), ParseError>(())
/// ```
Expand All @@ -183,20 +186,21 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```rust
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::{FBig, DBig};
/// use dashu_base::Approximation::*;
/// use dashu_float::round::{mode::HalfAway, Rounding::*};
///
/// let a = DBig::from_str_native("2.345")?;
/// let a = DBig::from_str("2.345")?;
/// assert_eq!(a.precision(), 4);
/// assert_eq!(
/// a.clone().with_precision(3),
/// Inexact(DBig::from_str_native("2.35")?, AddOne)
/// Inexact(DBig::from_str("2.35")?, AddOne)
/// );
/// assert_eq!(
/// a.clone().with_precision(5),
/// Exact(DBig::from_str_native("2.345")?)
/// Exact(DBig::from_str("2.345")?)
/// );
/// # Ok::<(), ParseError>(())
/// ```
Expand All @@ -223,6 +227,7 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```rust
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::{FBig, DBig};
/// use dashu_base::Approximation::*;
Expand All @@ -231,8 +236,8 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// type DBigHalfAway = DBig;
/// type DBigZero = FBig::<Zero, 10>;
///
/// let a = DBigHalfAway::from_str_native("2.345")?;
/// let b = DBigZero::from_str_native("2.345")?;
/// let a = DBigHalfAway::from_str("2.345")?;
/// let b = DBigZero::from_str("2.345")?;
/// assert_eq!(a.with_rounding::<Zero>(), b);
/// # Ok::<(), ParseError>(())
/// ```
Expand All @@ -259,6 +264,7 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```rust
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::{FBig, DBig};
/// use dashu_base::Approximation::*;
Expand All @@ -268,16 +274,16 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// type FDec = FBig<Zero, 10>;
/// type FHex = FBig<Zero, 16>;
///
/// let a = FBin::from_str_native("0x1.234")?; // 0x1234 * 2^-12
/// let a = FBin::from_str("0x1.234")?; // 0x1234 * 2^-12
/// assert_eq!(
/// a.clone().with_base::<10>(),
/// // 1.1376953125 rounded towards zero
/// Inexact(FDec::from_str_native("1.137")?, NoOp)
/// Inexact(FDec::from_str("1.137")?, NoOp)
/// );
/// assert_eq!(
/// a.clone().with_base::<16>(),
/// // conversion is exact when the new base is a power of the old base
/// Exact(FHex::from_str_native("1.234")?)
/// Exact(FHex::from_str("1.234")?)
/// );
/// # Ok::<(), ParseError>(())
/// ```
Expand Down Expand Up @@ -307,6 +313,7 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```rust
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::{FBig, DBig};
/// use dashu_base::Approximation::*;
Expand All @@ -316,21 +323,21 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// type FDec = FBig<Zero, 10>;
/// type FHex = FBig<Zero, 16>;
///
/// let a = FBin::from_str_native("0x1.234")?; // 0x1234 * 2^-12
/// let a = FBin::from_str("0x1.234")?; // 0x1234 * 2^-12
/// assert_eq!(
/// a.clone().with_base_and_precision::<10>(8),
/// // 1.1376953125 rounded towards zero
/// Inexact(FDec::from_str_native("1.1376953")?, NoOp)
/// Inexact(FDec::from_str("1.1376953")?, NoOp)
/// );
/// assert_eq!(
/// a.clone().with_base_and_precision::<16>(8),
/// // conversion can be exact when the new base is a power of the old base
/// Exact(FHex::from_str_native("1.234")?)
/// Exact(FHex::from_str("1.234")?)
/// );
/// assert_eq!(
/// a.clone().with_base_and_precision::<16>(2),
/// // but the conversion is still inexact if the target precision is smaller
/// Inexact(FHex::from_str_native("1.2")?, NoOp)
/// Inexact(FHex::from_str("1.2")?, NoOp)
/// );
/// # Ok::<(), ParseError>(())
/// ```
Expand Down Expand Up @@ -363,21 +370,22 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::{FBig, DBig};
/// use dashu_base::Approximation::*;
/// use dashu_float::round::Rounding::*;
///
/// assert_eq!(
/// DBig::from_str_native("1234")?.to_int(),
/// DBig::from_str("1234")?.to_int(),
/// Exact(1234.into())
/// );
/// assert_eq!(
/// DBig::from_str_native("1.234e6")?.to_int(),
/// DBig::from_str("1.234e6")?.to_int(),
/// Exact(1234000.into())
/// );
/// assert_eq!(
/// DBig::from_str_native("1.234")?.to_int(),
/// DBig::from_str("1.234")?.to_int(),
/// Inexact(1.into(), NoOp)
/// );
/// # Ok::<(), ParseError>(())
Expand Down Expand Up @@ -406,9 +414,10 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// assert_eq!(DBig::from_str_native("1.234")?.to_f32().value(), 1.234);
/// assert_eq!(DBig::from_str("1.234")?.to_f32().value(), 1.234);
/// assert_eq!(DBig::INFINITY.to_f32().value(), f32::INFINITY);
/// # Ok::<(), ParseError>(())
/// ```
Expand Down Expand Up @@ -436,9 +445,10 @@ impl<R: Round, const B: Word> FBig<R, B> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// assert_eq!(DBig::from_str_native("1.234")?.to_f64().value(), 1.234);
/// assert_eq!(DBig::from_str("1.234")?.to_f64().value(), 1.234);
/// assert_eq!(DBig::INFINITY.to_f64().value(), f64::INFINITY);
/// # Ok::<(), ParseError>(())
/// ```
Expand Down
19 changes: 11 additions & 8 deletions float/src/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,16 @@ impl<R: Round> Context<R> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(2);
/// let a = DBig::from_str_native("-1.234")?;
/// let b = DBig::from_str_native("6.789")?;
/// assert_eq!(context.div(&a.repr(), &b.repr()), Inexact(DBig::from_str_native("-0.18")?, NoOp));
/// let a = DBig::from_str("-1.234")?;
/// let b = DBig::from_str("6.789")?;
/// assert_eq!(context.div(&a.repr(), &b.repr()), Inexact(DBig::from_str("-0.18")?, NoOp));
/// # Ok::<(), ParseError>(())
/// ```
///
Expand Down Expand Up @@ -371,15 +372,16 @@ impl<R: Round> Context<R> {
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(3);
/// let a = DBig::from_str_native("6.789")?;
/// let b = DBig::from_str_native("-1.234")?;
/// assert_eq!(context.rem(&a.repr(), &b.repr()), Exact(DBig::from_str_native("-0.615")?));
/// let a = DBig::from_str("6.789")?;
/// let b = DBig::from_str("-1.234")?;
/// assert_eq!(context.rem(&a.repr(), &b.repr()), Exact(DBig::from_str("-0.615")?));
/// # Ok::<(), ParseError>(())
/// ```
pub fn rem<const B: Word>(&self, lhs: &Repr<B>, rhs: &Repr<B>) -> Rounded<FBig<R, B>> {
Expand All @@ -391,14 +393,15 @@ impl<R: Round> Context<R> {
/// Compute the multiplicative inverse of an `FBig`
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(2);
/// let a = DBig::from_str_native("-1.234")?;
/// assert_eq!(context.inv(&a.repr()), Inexact(DBig::from_str_native("-0.81")?, NoOp));
/// let a = DBig::from_str("-1.234")?;
/// assert_eq!(context.inv(&a.repr()), Inexact(DBig::from_str("-0.81")?, NoOp));
/// # Ok::<(), ParseError>(())
/// ```
#[inline]
Expand Down
Loading

0 comments on commit 86bdae4

Please sign in to comment.