Skip to content

Commit

Permalink
Temporarily remove incomplete features to support releasing a version…
Browse files Browse the repository at this point in the history
… with num_traits support.
  • Loading branch information
ajtribick committed Apr 21, 2022
1 parent 1c25545 commit 0c94681
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 323 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Breaking change: use `serde` as the feature flag name.
* Breaking change: use helper struct for Serde serialization.
* Integrate with `num_traits` crate.
* Add `FromStr` implementation.
* Internal: use hexf to specify constants.

## Version 0.4.1

Expand Down
14 changes: 1 addition & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,15 @@ license = "BSD-3-Clause"
description = "Double-double arithmetic functionality."
repository = "https://github.com/ajtribick/twofloat"

[lib]
name = "twofloat"
test = true

[features]
default = ["math_funcs", "string_convert"]
default = ["math_funcs"]
math_funcs = ["num-traits/std"]
string_convert = ["lazy_static", "num-bigint", "num-rational"]

[dependencies]
hexf = "0.2"
lazy_static = { version = "1.4", optional = true }
num-bigint = { version = "0.3", optional = true }
num-rational = { version = "0.3", optional = true }
num-traits = { version = "0.2.14", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
rand = "0.8"
serde_test = "1.0"

[workspace]
members = ["twofloat_macro"]
default-members = [".", "twofloat_macro"]
139 changes: 137 additions & 2 deletions src/base.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::cmp::Ordering;
use core::num::FpCategory;
use core::{cmp::Ordering, fmt, num::FpCategory};

use hexf::hexf64;

Expand Down Expand Up @@ -265,6 +264,109 @@ impl TwoFloat {
}
}

impl fmt::Display for TwoFloat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let sign_char = if self.lo().is_sign_positive() {
'+'
} else {
'-'
};
if f.sign_plus() {
match f.precision() {
Some(p) => write!(
f,
"{:+.*} {} {:.*}",
p,
self.hi,
sign_char,
p,
self.lo.abs()
),
None => write!(f, "{:+} {} {}", self.hi, sign_char, self.lo.abs()),
}
} else {
match f.precision() {
Some(p) => write!(f, "{:.*} {} {:.*}", p, self.hi, sign_char, p, self.lo.abs()),
None => write!(f, "{} {} {}", self.hi, sign_char, self.lo.abs()),
}
}
}
}

impl fmt::LowerExp for TwoFloat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let sign_char = if self.lo().is_sign_positive() {
'+'
} else {
'-'
};
if f.sign_plus() {
match f.precision() {
Some(p) => write!(
f,
"{:+.*e} {} {:.*e}",
p,
self.hi,
sign_char,
p,
self.lo.abs()
),
None => write!(f, "{:+e} {} {:e}", self.hi, sign_char, self.lo.abs()),
}
} else {
match f.precision() {
Some(p) => write!(
f,
"{:.*e} {} {:.*e}",
p,
self.hi,
sign_char,
p,
self.lo.abs()
),
None => write!(f, "{:e} {} {:e}", self.hi, sign_char, self.lo.abs()),
}
}
}
}

impl fmt::UpperExp for TwoFloat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let sign_char = if self.lo().is_sign_positive() {
'+'
} else {
'-'
};
if f.sign_plus() {
match f.precision() {
Some(p) => write!(
f,
"{:+.*E} {} {:.*E}",
p,
self.hi,
sign_char,
p,
self.lo.abs()
),
None => write!(f, "{:+E} {} {:E}", self.hi, sign_char, self.lo.abs()),
}
} else {
match f.precision() {
Some(p) => write!(
f,
"{:.*E} {} {:.*E}",
p,
self.hi,
sign_char,
p,
self.lo.abs()
),
None => write!(f, "{:E} {} {:E}", self.hi, sign_char, self.lo.abs()),
}
}
}
}

impl PartialEq<f64> for TwoFloat {
fn eq(&self, other: &f64) -> bool {
self.hi.eq(other) && self.lo == 0.0
Expand Down Expand Up @@ -370,6 +472,39 @@ mod tests {
assert!(no_overlap(0.0, 0.0));
}

#[test]
fn display_test() {
let value = TwoFloat { hi: 1.0, lo: 0.3 };
assert_eq!(format!("{}", value), "1 + 0.3");
assert_eq!(format!("{}", -value), "-1 - 0.3");
assert_eq!(format!("{:+}", value), "+1 + 0.3");
assert_eq!(format!("{:.2}", value), "1.00 + 0.30");
assert_eq!(format!("{:.2}", -value), "-1.00 - 0.30");
assert_eq!(format!("{:+.2}", value), "+1.00 + 0.30");
}

#[test]
fn lowerexp_test() {
let value = TwoFloat { hi: 1.0, lo: -0.3 };
assert_eq!(format!("{:e}", value), "1e0 - 3e-1");
assert_eq!(format!("{:e}", -value), "-1e0 + 3e-1");
assert_eq!(format!("{:+e}", value), "+1e0 - 3e-1");
assert_eq!(format!("{:.2e}", value), "1.00e0 - 3.00e-1");
assert_eq!(format!("{:.2e}", -value), "-1.00e0 + 3.00e-1");
assert_eq!(format!("{:+.2e}", value), "+1.00e0 - 3.00e-1");
}

#[test]
fn upperexp_test() {
let value = TwoFloat { hi: 1.0, lo: 0.3 };
assert_eq!(format!("{:E}", value), "1E0 + 3E-1");
assert_eq!(format!("{:E}", -value), "-1E0 - 3E-1");
assert_eq!(format!("{:+E}", value), "+1E0 + 3E-1");
assert_eq!(format!("{:.2E}", value), "1.00E0 + 3.00E-1");
assert_eq!(format!("{:.2E}", -value), "-1.00E0 - 3.00E-1");
assert_eq!(format!("{:+.2E}", value), "+1.00E0 + 3.00E-1");
}

#[test]
fn default_test() {
let value: TwoFloat = Default::default();
Expand Down
3 changes: 1 addition & 2 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::convert::{From, TryFrom};

use crate::base::no_overlap;
use crate::{TwoFloat, TwoFloatError};
use crate::{base::no_overlap, TwoFloat, TwoFloatError};

macro_rules! from_conversion {
(|$source_i:ident : TwoFloat| -> $dest:tt $code:block) => {
Expand Down
3 changes: 1 addition & 2 deletions src/functions/explog.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use hexf::hexf64;

use crate::consts::LN_2;
use crate::TwoFloat;
use crate::{consts::LN_2, TwoFloat};

// 1/ln(2)
const FRAC_1_LN_2: TwoFloat = TwoFloat {
Expand Down
3 changes: 1 addition & 2 deletions src/functions/fraction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::arithmetic::fast_two_sum;
use crate::TwoFloat;
use crate::{arithmetic::fast_two_sum, TwoFloat};

impl TwoFloat {
/// Returns the fractional part of the number.
Expand Down
7 changes: 4 additions & 3 deletions src/functions/trigonometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,10 @@ impl TwoFloat {
#[cfg(test)]
mod tests {
use super::quadrant;

use crate::consts::{FRAC_PI_2, FRAC_PI_4, PI};
use crate::TwoFloat;
use crate::{
consts::{FRAC_PI_2, FRAC_PI_4, PI},
TwoFloat,
};

const THRESHOLD: f64 = 1e-10;

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ pub mod consts;
mod convert;
mod functions;
mod num_integration;
#[cfg(feature = "string_convert")]
mod string;

pub use base::no_overlap;

Expand Down
13 changes: 1 addition & 12 deletions src/num_integration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::convert::TryFrom;
use core::num::FpCategory;
use core::{convert::TryFrom, num::FpCategory};

use hexf::hexf64;
use num_traits::{Inv, Pow};
Expand All @@ -9,16 +8,6 @@ use crate::{consts, TwoFloat, TwoFloatError};
impl num_traits::Num for TwoFloat {
type FromStrRadixErr = TwoFloatError;

#[cfg(feature = "string_convert")]
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
if radix == 10 {
str.parse()
} else {
Err(TwoFloatError::ParseError)
}
}

#[cfg(not(feature = "string_convert"))]
fn from_str_radix(_str: &str, _radix: u32) -> Result<Self, Self::FromStrRadixErr> {
Err(TwoFloatError::ParseError)
}
Expand Down
Loading

0 comments on commit 0c94681

Please sign in to comment.