Skip to content

Commit

Permalink
Merge #73 #74
Browse files Browse the repository at this point in the history
73: Add a libm feature for Float r=cuviper a=cuviper



74: char::is_whitespace is available even in core now r=cuviper a=cuviper



Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
bors[bot] and cuviper committed Jan 21, 2020
3 parents f443e17 + bfcd391 + b4b91d4 commit 741c4d9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -18,7 +18,7 @@ matrix:
before_script:
- rustup target add $TARGET
script:
- cargo build --verbose --target $TARGET --no-default-features --features rand,serde
- cargo build --verbose --target $TARGET --no-default-features --features libm,rand,serde
- name: "rustfmt"
rust: 1.31.0
before_script:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -37,3 +37,4 @@ default-features = false
[features]
default = ["std"]
std = ["num-traits/std"]
libm = ["num-traits/libm"]
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -27,9 +27,9 @@ version = "0.3"
default-features = false
```

Features based on `Float` types are only available when `std` is enabled. Where
possible, `FloatCore` is used instead. Formatting complex numbers only supports
format width when `std` is enabled.
Features based on `Float` types are only available when `std` or `libm` is
enabled. Where possible, `FloatCore` is used instead. Formatting complex
numbers only supports format width when `std` is enabled.

## Releases

Expand Down
4 changes: 2 additions & 2 deletions ci/test_full.sh
Expand Up @@ -5,8 +5,8 @@ set -ex
echo Testing num-complex on rustc ${TRAVIS_RUST_VERSION}

case "$TRAVIS_RUST_VERSION" in
1.31.*) FEATURES="serde" ;;
*) FEATURES="serde rand" ;;
1.31.*) FEATURES="libm serde" ;;
*) FEATURES="libm serde rand" ;;
esac

# num-complex should build and test everywhere.
Expand Down
31 changes: 6 additions & 25 deletions src/lib.rs
Expand Up @@ -32,7 +32,7 @@ use std::error::Error;

use num_traits::{Inv, MulAdd, Num, One, Pow, Signed, Zero};

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
use num_traits::float::Float;
use num_traits::float::FloatCore;

Expand Down Expand Up @@ -159,7 +159,7 @@ impl<T: Clone + Signed> Complex<T> {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<T: Clone + Float> Complex<T> {
/// Calculate |self|
#[inline]
Expand Down Expand Up @@ -1269,25 +1269,6 @@ where
F: Fn(&str) -> Result<T, E>,
T: Clone + Num,
{
#[cfg(not(feature = "std"))]
#[inline]
fn is_whitespace(c: char) -> bool {
match c {
' ' | '\x09'..='\x0d' => true,
_ if c > '\x7f' => match c {
'\u{0085}' | '\u{00a0}' | '\u{1680}' => true,
'\u{2000}'..='\u{200a}' => true,
'\u{2028}' | '\u{2029}' | '\u{202f}' | '\u{205f}' => true,
'\u{3000}' => true,
_ => false,
},
_ => false,
}
}

#[cfg(feature = "std")]
let is_whitespace = char::is_whitespace;

let imag = match s.rfind('j') {
None => 'i',
_ => 'j',
Expand All @@ -1304,8 +1285,8 @@ where
// ignore '+'/'-' if part of an exponent
if (c == b'+' || c == b'-') && !(p == b'e' || p == b'E') {
// trim whitespace around the separator
a = &s[..=i].trim_right_matches(is_whitespace);
b = &s[i + 2..].trim_left_matches(is_whitespace);
a = &s[..=i].trim_right_matches(char::is_whitespace);
b = &s[i + 2..].trim_left_matches(char::is_whitespace);
neg_b = c == b'-';

if b.is_empty() || (neg_b && b.starts_with('-')) {
Expand Down Expand Up @@ -1604,7 +1585,7 @@ mod test {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
mod float {
use super::*;
use num_traits::{Float, Pow};
Expand Down Expand Up @@ -2265,7 +2246,7 @@ mod test {
}

#[test]
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
fn test_mul_add_float() {
assert_eq!(_05_05i.mul_add(_05_05i, _0_0i), _05_05i * _05_05i + _0_0i);
assert_eq!(_05_05i * _05_05i + _0_0i, _05_05i.mul_add(_05_05i, _0_0i));
Expand Down
18 changes: 9 additions & 9 deletions src/pow.rs
@@ -1,7 +1,7 @@
use super::Complex;

use core::ops::Neg;
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
use num_traits::Float;
use num_traits::{Num, One, Pow};

Expand Down Expand Up @@ -85,7 +85,7 @@ pow_impl!(u128, i128);

macro_rules! powf_impl {
($F:ty) => {
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a, T: Float> Pow<$F> for &'a Complex<T>
where
$F: Into<T>,
Expand All @@ -98,7 +98,7 @@ macro_rules! powf_impl {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a, 'b, T: Float> Pow<&'b $F> for &'a Complex<T>
where
$F: Into<T>,
Expand All @@ -111,7 +111,7 @@ macro_rules! powf_impl {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<T: Float> Pow<$F> for Complex<T>
where
$F: Into<T>,
Expand All @@ -124,7 +124,7 @@ macro_rules! powf_impl {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'b, T: Float> Pow<&'b $F> for Complex<T>
where
$F: Into<T>,
Expand All @@ -145,7 +145,7 @@ powf_impl!(f64);
// These blanket impls are OK, because both the target type and the trait parameter would be
// foreign to anyone else trying to implement something that would overlap, raising E0117.

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a, T: Float> Pow<Complex<T>> for &'a Complex<T> {
type Output = Complex<T>;

Expand All @@ -155,7 +155,7 @@ impl<'a, T: Float> Pow<Complex<T>> for &'a Complex<T> {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a, 'b, T: Float> Pow<&'b Complex<T>> for &'a Complex<T> {
type Output = Complex<T>;

Expand All @@ -165,7 +165,7 @@ impl<'a, 'b, T: Float> Pow<&'b Complex<T>> for &'a Complex<T> {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<T: Float> Pow<Complex<T>> for Complex<T> {
type Output = Complex<T>;

Expand All @@ -175,7 +175,7 @@ impl<T: Float> Pow<Complex<T>> for Complex<T> {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'b, T: Float> Pow<&'b Complex<T>> for Complex<T> {
type Output = Complex<T>;

Expand Down

0 comments on commit 741c4d9

Please sign in to comment.