Skip to content

Commit 0c94681

Browse files
committed
Temporarily remove incomplete features to support releasing a version with num_traits support.
1 parent 1c25545 commit 0c94681

File tree

16 files changed

+150
-323
lines changed

16 files changed

+150
-323
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Breaking change: use `serde` as the feature flag name.
77
* Breaking change: use helper struct for Serde serialization.
88
* Integrate with `num_traits` crate.
9-
* Add `FromStr` implementation.
9+
* Internal: use hexf to specify constants.
1010

1111
## Version 0.4.1
1212

Cargo.toml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,15 @@ license = "BSD-3-Clause"
1313
description = "Double-double arithmetic functionality."
1414
repository = "https://github.com/ajtribick/twofloat"
1515

16-
[lib]
17-
name = "twofloat"
18-
test = true
19-
2016
[features]
21-
default = ["math_funcs", "string_convert"]
17+
default = ["math_funcs"]
2218
math_funcs = ["num-traits/std"]
23-
string_convert = ["lazy_static", "num-bigint", "num-rational"]
2419

2520
[dependencies]
2621
hexf = "0.2"
27-
lazy_static = { version = "1.4", optional = true }
28-
num-bigint = { version = "0.3", optional = true }
29-
num-rational = { version = "0.3", optional = true }
3022
num-traits = { version = "0.2.14", default-features = false }
3123
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
3224

3325
[dev-dependencies]
3426
rand = "0.8"
3527
serde_test = "1.0"
36-
37-
[workspace]
38-
members = ["twofloat_macro"]
39-
default-members = [".", "twofloat_macro"]

src/base.rs

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use core::cmp::Ordering;
2-
use core::num::FpCategory;
1+
use core::{cmp::Ordering, fmt, num::FpCategory};
32

43
use hexf::hexf64;
54

@@ -265,6 +264,109 @@ impl TwoFloat {
265264
}
266265
}
267266

267+
impl fmt::Display for TwoFloat {
268+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
269+
let sign_char = if self.lo().is_sign_positive() {
270+
'+'
271+
} else {
272+
'-'
273+
};
274+
if f.sign_plus() {
275+
match f.precision() {
276+
Some(p) => write!(
277+
f,
278+
"{:+.*} {} {:.*}",
279+
p,
280+
self.hi,
281+
sign_char,
282+
p,
283+
self.lo.abs()
284+
),
285+
None => write!(f, "{:+} {} {}", self.hi, sign_char, self.lo.abs()),
286+
}
287+
} else {
288+
match f.precision() {
289+
Some(p) => write!(f, "{:.*} {} {:.*}", p, self.hi, sign_char, p, self.lo.abs()),
290+
None => write!(f, "{} {} {}", self.hi, sign_char, self.lo.abs()),
291+
}
292+
}
293+
}
294+
}
295+
296+
impl fmt::LowerExp for TwoFloat {
297+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
298+
let sign_char = if self.lo().is_sign_positive() {
299+
'+'
300+
} else {
301+
'-'
302+
};
303+
if f.sign_plus() {
304+
match f.precision() {
305+
Some(p) => write!(
306+
f,
307+
"{:+.*e} {} {:.*e}",
308+
p,
309+
self.hi,
310+
sign_char,
311+
p,
312+
self.lo.abs()
313+
),
314+
None => write!(f, "{:+e} {} {:e}", self.hi, sign_char, self.lo.abs()),
315+
}
316+
} else {
317+
match f.precision() {
318+
Some(p) => write!(
319+
f,
320+
"{:.*e} {} {:.*e}",
321+
p,
322+
self.hi,
323+
sign_char,
324+
p,
325+
self.lo.abs()
326+
),
327+
None => write!(f, "{:e} {} {:e}", self.hi, sign_char, self.lo.abs()),
328+
}
329+
}
330+
}
331+
}
332+
333+
impl fmt::UpperExp for TwoFloat {
334+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
335+
let sign_char = if self.lo().is_sign_positive() {
336+
'+'
337+
} else {
338+
'-'
339+
};
340+
if f.sign_plus() {
341+
match f.precision() {
342+
Some(p) => write!(
343+
f,
344+
"{:+.*E} {} {:.*E}",
345+
p,
346+
self.hi,
347+
sign_char,
348+
p,
349+
self.lo.abs()
350+
),
351+
None => write!(f, "{:+E} {} {:E}", self.hi, sign_char, self.lo.abs()),
352+
}
353+
} else {
354+
match f.precision() {
355+
Some(p) => write!(
356+
f,
357+
"{:.*E} {} {:.*E}",
358+
p,
359+
self.hi,
360+
sign_char,
361+
p,
362+
self.lo.abs()
363+
),
364+
None => write!(f, "{:E} {} {:E}", self.hi, sign_char, self.lo.abs()),
365+
}
366+
}
367+
}
368+
}
369+
268370
impl PartialEq<f64> for TwoFloat {
269371
fn eq(&self, other: &f64) -> bool {
270372
self.hi.eq(other) && self.lo == 0.0
@@ -370,6 +472,39 @@ mod tests {
370472
assert!(no_overlap(0.0, 0.0));
371473
}
372474

475+
#[test]
476+
fn display_test() {
477+
let value = TwoFloat { hi: 1.0, lo: 0.3 };
478+
assert_eq!(format!("{}", value), "1 + 0.3");
479+
assert_eq!(format!("{}", -value), "-1 - 0.3");
480+
assert_eq!(format!("{:+}", value), "+1 + 0.3");
481+
assert_eq!(format!("{:.2}", value), "1.00 + 0.30");
482+
assert_eq!(format!("{:.2}", -value), "-1.00 - 0.30");
483+
assert_eq!(format!("{:+.2}", value), "+1.00 + 0.30");
484+
}
485+
486+
#[test]
487+
fn lowerexp_test() {
488+
let value = TwoFloat { hi: 1.0, lo: -0.3 };
489+
assert_eq!(format!("{:e}", value), "1e0 - 3e-1");
490+
assert_eq!(format!("{:e}", -value), "-1e0 + 3e-1");
491+
assert_eq!(format!("{:+e}", value), "+1e0 - 3e-1");
492+
assert_eq!(format!("{:.2e}", value), "1.00e0 - 3.00e-1");
493+
assert_eq!(format!("{:.2e}", -value), "-1.00e0 + 3.00e-1");
494+
assert_eq!(format!("{:+.2e}", value), "+1.00e0 - 3.00e-1");
495+
}
496+
497+
#[test]
498+
fn upperexp_test() {
499+
let value = TwoFloat { hi: 1.0, lo: 0.3 };
500+
assert_eq!(format!("{:E}", value), "1E0 + 3E-1");
501+
assert_eq!(format!("{:E}", -value), "-1E0 - 3E-1");
502+
assert_eq!(format!("{:+E}", value), "+1E0 + 3E-1");
503+
assert_eq!(format!("{:.2E}", value), "1.00E0 + 3.00E-1");
504+
assert_eq!(format!("{:.2E}", -value), "-1.00E0 - 3.00E-1");
505+
assert_eq!(format!("{:+.2E}", value), "+1.00E0 + 3.00E-1");
506+
}
507+
373508
#[test]
374509
fn default_test() {
375510
let value: TwoFloat = Default::default();

src/convert.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::convert::{From, TryFrom};
22

3-
use crate::base::no_overlap;
4-
use crate::{TwoFloat, TwoFloatError};
3+
use crate::{base::no_overlap, TwoFloat, TwoFloatError};
54

65
macro_rules! from_conversion {
76
(|$source_i:ident : TwoFloat| -> $dest:tt $code:block) => {

src/functions/explog.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use hexf::hexf64;
22

3-
use crate::consts::LN_2;
4-
use crate::TwoFloat;
3+
use crate::{consts::LN_2, TwoFloat};
54

65
// 1/ln(2)
76
const FRAC_1_LN_2: TwoFloat = TwoFloat {

src/functions/fraction.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::arithmetic::fast_two_sum;
2-
use crate::TwoFloat;
1+
use crate::{arithmetic::fast_two_sum, TwoFloat};
32

43
impl TwoFloat {
54
/// Returns the fractional part of the number.

src/functions/trigonometry.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,10 @@ impl TwoFloat {
527527
#[cfg(test)]
528528
mod tests {
529529
use super::quadrant;
530-
531-
use crate::consts::{FRAC_PI_2, FRAC_PI_4, PI};
532-
use crate::TwoFloat;
530+
use crate::{
531+
consts::{FRAC_PI_2, FRAC_PI_4, PI},
532+
TwoFloat,
533+
};
533534

534535
const THRESHOLD: f64 = 1e-10;
535536

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ pub mod consts;
8080
mod convert;
8181
mod functions;
8282
mod num_integration;
83-
#[cfg(feature = "string_convert")]
84-
mod string;
8583

8684
pub use base::no_overlap;
8785

src/num_integration.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use core::convert::TryFrom;
2-
use core::num::FpCategory;
1+
use core::{convert::TryFrom, num::FpCategory};
32

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

12-
#[cfg(feature = "string_convert")]
13-
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
14-
if radix == 10 {
15-
str.parse()
16-
} else {
17-
Err(TwoFloatError::ParseError)
18-
}
19-
}
20-
21-
#[cfg(not(feature = "string_convert"))]
2211
fn from_str_radix(_str: &str, _radix: u32) -> Result<Self, Self::FromStrRadixErr> {
2312
Err(TwoFloatError::ParseError)
2413
}

0 commit comments

Comments
 (0)