Rust 1.4 through 1.7 will issue warnings that `pub extern crate` does
not work as expected, although it appears to be fine. But Rust 1.8 and
later issue `#[warn(private_in_public)]` for `pub use num_foo as foo` if
that crate isn't public, and that's headed toward a hard error.
@bluss suggested instead `pub mod foo { pub use num_foo::*; }`, which
I thought I had tried before, but it appears to work fine on all
versions of Rust. Let's do it!
A small downside is that docs for `num::foo` now just show the wildcard
reexport, instead of direct documentation, but at least there's a link
to follow to the sub-crate.
It's a breaking change that `num::num_foo` paths are no longer public,
but we didn't really want those exposed in the first place. I consider
this minor -- people should either use the `num::foo` module as before
the split-up, or use the `num_foo` crate directly.
Fixes#189.
Serde 0.7 dropped it's dependency on num, so this patch moves
the implementations here. For the sake of a better implementation,
this just serializes BigUint as a `Vec<u32>`, `BigInt` as a
`(u8, Vec<u32>)`, `Complex<T>` as a `(T, T)`, and `Ratio<T>`
as a `(T, T)`.
- Integer only needs to require Ord explicitly, and then PartialOrd, Eq,
and PartialEq come transitively.
- Generics on Integer can implicitly use all of those comparison traits.
This should not be a breaking change, as it doesn't actually change any
effective trait requirements -- only what's explicit for simplicity.
Previously, the `rand` and `rustc-serialize` dependencies were optional
except they were required for the `bigint` feature.
Make the dependency on the `rand` crate optional in all cases.
including when the `bigint` feature is selected. Some of the tests for
the bigint feature are randomized so, while `rand` is now an optional
dependency, it is a non-optional dev-dependency.
Similarly, make the dependency on the `rustc-serialize` crate optional
in all cases, including when the `bigint` feature is selected.
We can save a multiplication if we start the accumulation basically at
the first set bit of the exponent, rather than starting at one and
waiting to multiply. We only need one itself if the exponent is zero,
which is easy to pre-check.
Before:
test pow_bench ... bench: 8,267,370 ns/iter (+/- 93,319)
After:
test pow_bench ... bench: 7,506,463 ns/iter (+/- 116,311)
The default implementations of to_f32, to_f64, from_32 and from_f64 are
limited to numbers fitting in i64 but BigInt can of course be much bigger.
The default to_f32 also has double rounding.
from_f32 and from_f64 have undefined behaviour if the float is too big to
fit in an i64.
This fixes these issues and keeps the rounding consistant with other float
to int conversions.
Also add ToBigUint and ToBigInt implementations for f32 and f64.
The hidden "mod test" layout of the first example has been broken for a
while, but it wasn't noticed because rustdoc wasn't passing any features
at all. That was fixed in rust-lang/rust#30372, and now we need to get
our ducks in a row too.
bigint: simplify Hash
There cannot be any leading zeros in a BigUint so just derive Hash which will just hash the Vec directly.
Add Hash to Sign so we can derive it for BigInt as well.
There cannot be any leading zeros in a BigUint so just derive Hash which will just hash the Vec directly.
Add Hash to Sign so we can derive it for BigInt as well.
The main idea here is to do as much as possible with slices, instead of
allocating new BigUints (= heap allocations).
Current performance:
multiply_0: 10,507 ns/iter (+/- 987)
multiply_1: 2,788,734 ns/iter (+/- 100,079)
multiply_2: 69,923,515 ns/iter (+/- 4,550,902)
After this patch, we get:
multiply_0: 364 ns/iter (+/- 62)
multiply_1: 34,085 ns/iter (+/- 1,179)
multiply_2: 3,753,883 ns/iter (+/- 46,876)
Since the Num constraint only requires val-val ops, we ended up cloning
everything in Complex ops when they were forwarded to ref-ref. By using
val-val, we can reduce how often clones are needed.
Implement arithmetic with real for complex
Hello,
It might be handy to be able to perform basic arithmetic operations in expressions mixing complex numbers and numeric primitives; see #116. I would be grateful for any feedback, especially regarding the subsets of primitives for which certain operations are implemented.
Regards,
Ivan
- Use explicit asserts for should-panic tests, since overflow is not
checked in release mode.
- Add positive tests where gcd min_value() can be represented.
- In all cases, test min_value() in both positions.
Closes#127
Removing extra parentheses in a for loop fixes this warning:
src/bigint.rs:1636:18: 1636:31 warning: unnecessary parentheses around `for` head expression, #[warn(unused_parens)] on by default
Making bignum optional allows skipping the rustc-serialize and rand
dependencies too, and it makes a big difference in num's build time.
With default (all) features, clean build time including dependencies: 27
seconds.
With no default features, clean build time including dependencies (none):
5 seconds.
For example, the following will print (4294967295, 65535):
extern crate num;
use num::traits::Bounded;
fn main() {
let t : (u32, u16) = Bounded::max_value();
println!("{:?}", t);
}
If T is an unsigned integer type, these methods are guaranteed to
overflow unless the result is actually real, so we should disallow
them for the same reason that Neg was removed from these types.
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Along the way, this commit also rationalizes the traits a bit more.
Moving to stable required vendoring a couple of minor things (notably,
radix formatting).
This commit brings in a load of unstable and/or deprecated traits from
the `std::num` module. These traits provide for some degree of generic
programming over numeric types. They are not stable in `std` mostly
because we want more time to iterate on their design. Moving them to the
`num` crate allows existing code to keep using this functionality as we
do so.
Closes#74
The code was performing an extra squaring of the base, which might
trigger an arithmetic overflow that doesn't matter to the result. Now
this squaring is only attempted when enough exp remains to need it.
A new doctest tries pow(6u8, 3), where an extra square would exceed 256.
`str::from_utf8` changed its return type to `Result`, but
`num::FromStrRadix::from_str_radix` still returns an `Option`. In this case
discarding the `Err` variant with `ok()` seems fine to me.
Now, arithmetic binary operator traits take operands by value,
but non-copyable types such as `BigUint` should not always moved out when
applying operators.
This commit implements these operators not only for bare structs also
these references.
By-value implementations are forwarded to by-reference implementations
for now. In the future, by-value implementations may be replaced with
more efficient implementations (for example, the implementation that
reuses moved buffers.)