Commit Graph

334 Commits

Author SHA1 Message Date
Dan Barella 3534a89858 Don't use assert_ne!
`num` is tested against `rust 1.8.0`, which doesn't include
`assert_ne!` -- so we use a plain ol' `assert` instead.
2018-03-10 15:14:23 -08:00
Dan Barella f99aa0e181 Check overflow when casting floats to integers.
This change adds some new macro rules used when converting from floats
to integers. There are two macro rule variants, one for signed ints, one
for unsigned ints.

Among other things, this change specifically addresses the overflow case
documented in https://github.com/rust-num/num-traits/issues/12
2018-03-10 15:14:23 -08:00
Josh Stone 04a3f2a591 Comment the Rust version for NAN.is_sign_* behavior 2018-02-28 11:43:55 -08:00
Josh Stone 080f6f259e Comment the i32::MIN case for FloatCore::powi 2018-02-28 11:33:34 -08:00
Josh Stone aa9ceba628 Add doctests to FloatCore 2018-02-27 22:12:37 -08:00
Josh Stone ec3cd50f3d Weaken the std f32::to_degrees/to_radians tests 2018-02-27 22:10:46 -08:00
Josh Stone ac6eca4b66 Use more FloatCore in src/sign.rs 2018-02-27 21:50:44 -08:00
Josh Stone 36c7e324db Fix FloatCore::powi with i32::MIN exponent 2018-02-27 21:34:01 -08:00
Josh Stone 964a7e52a8 Reinstate NAN-sign fixes in FloatCore
Formerly changed on the next branch, part of rust-num/num#319.
2018-02-27 21:03:30 -08:00
Josh Stone 8d16921579 allow unused macros 2018-02-27 17:09:43 -08:00
Josh Stone 6fa29be7c0 Use macros for more float constants 2018-02-27 17:09:43 -08:00
Josh Stone 83d498d0be Add integer_decode to FloatCore 2018-02-27 16:33:04 -08:00
Josh Stone f365a4205f Add rounding methods to FloatCore 2018-02-27 16:33:04 -08:00
Josh Stone 99c6cc11ba Add more constants to FloatCore 2018-02-27 16:33:04 -08:00
Josh Stone 7d6575da0f Add NumCast to FloatCore, matching Float 2018-02-27 16:33:04 -08:00
Josh Stone ac503261ca Forward FloatCore to inherent methods when possible 2018-02-27 16:33:04 -08:00
Josh Stone c848562fcf Use forwarding macros to implement Float and Real 2018-02-27 16:33:04 -08:00
Clar Charr 79b557f040 Ensure infalliability of conversions, avoid closures. 2018-02-27 14:06:46 -05:00
Clar Charr aca8dc8149 Remove Pow<u64> (accidentally added). 2018-02-27 13:25:53 -05:00
Clar Charr 61a6acc9c2 Add more Pow implementations. 2018-02-27 13:25:53 -05:00
Clar Charr 5d6933f34a Fix doc tests. 2018-02-27 13:25:53 -05:00
Clar Charr ce3badca57 Move Pow to pow module. 2018-02-27 13:25:53 -05:00
Clar Charr c1f4118b4e Fix Inv trait, add Pow trait. 2018-02-27 13:25:53 -05:00
Clar Charr 5bdff3f0ff Add Inv trait. 2018-02-27 13:25:53 -05:00
Clar Charr 51dad501aa Add #[inline] to is_one. 2018-02-23 17:44:07 -05:00
Clar Charr 45856ee846 Make doc comment less scary. 2018-02-23 17:21:47 -05:00
Clar Charr 9461cd84f2 Add is_one. 2018-02-19 15:05:11 -05:00
bors[bot] bfd62d4638 Merge #32
32: Implement CoreFloat trait r=cuviper a=vks

This is a subset of the `Float` trait, but works with `no_std`.
Some code was simplified by using `CoreFloat`.
2018-02-07 22:26:47 +00:00
Vinzent Steinberg d115dadeb1 Don't re-export FloatCore
This avoids breaking `use num_traits::*`.
2018-02-07 12:42:30 +01:00
Vinzent Steinberg 1db660ed56 Inline only actual trait implementations 2018-02-07 12:38:01 +01:00
Vinzent Steinberg efad5329b4 Rename CoreFloat to FloatCore 2018-02-07 12:34:14 +01:00
Vinzent Steinberg 8a7f383eb1 Implement CoreFloat trait
This is a subset of the `Float` trait, but works with `no_std`.
Some code was simplified by using `CoreFloat`.
2018-02-02 19:48:25 +01:00
Josh Stone 47515a10e1 Add a min-rustc badge and document compatibility 2018-02-02 10:24:14 -08:00
Josh Stone 67f03391a1 Bump to 0.2 for the breaking feature change 2018-01-31 16:19:00 -08:00
Josh Stone 4fbc583eb9 Don't use wildcards for pub use 2018-01-31 16:05:43 -08:00
Josh Stone e6bb97b3ac Make `Float` and `Real` depend on the `std` feature
We don't have implementations for many of the methods in `no_std`.  It's
hostile to external implementors if some trait methods are conditional
on a feature, as that feature could be added by anyone in a dependency
tree.  Instead, let's just live without these traits for now.
2018-01-31 15:56:06 -08:00
Vinzent Steinberg a843027b56 Re-introduce the std feature
This is a port of @vks's rust-num/num#296, but without the feature-
toggled changes to `Float`.
2018-01-31 15:42:55 -08:00
bors[bot] 93be5dbff2 Merge #23
23: Add RealNum trait for real data types (Float, but without floating-point specific features) r=cuviper a=yoanlcq

This is supposed to fix [#19](https://github.com/rust-num/num-traits/issues/19); I assumed going ahead would be better than bumping the thread.  

In any case, I understand that it is a quite significant addition and won't mind too much if it doesn't make it.

This adds a new `RealNum` trait, along with a universal impl `impl<T: Float> RealNum for T { ... }`.  
Therefore, this shouldn't be a breaking change, except in places where both traits are imported (which obviously only happened in a few places in this crate).

The intent is that generic code may prefer to use `RealNum` instead of `Float` when floating-point isn't a requirement. In the future (next major version ?), I guess `Float` could be made to only provide floating-point-specific features on top of `RealNum`.

Most of the code+doc was copy-pasted from `Float`, but the doc comments should be up-to-date with the situation; `Float` only makes an appearance when talking about NaN and infinity.

Issues I've seen : 
- `RealNum` might not be the name we want;
- I've mentioned that `sqrt()` is allowed to panic if the input is negative and has no meaningful NaN representation;
- Should we do that too for e.g `log()` ? Like `sqrt()`, it's supposed to return Nan when `x < 0`.

Thanks for your time. :)
2018-01-19 01:39:13 +00:00
Yoan Lecoq 1e892e2238 Remove legacy default implementations 2018-01-18 21:32:46 +01:00
Yoan Lecoq a2337f392b Document panic cases where T doesn't support NaN 2018-01-18 08:44:05 +01:00
bors[bot] aa7c15e0e9 Merge #17 #21
17: Add AsPrimitive trait for generic casting with `as` r=cuviper a=Enet4

This is my personal attempt at #7. It is fairly similar to what can be found in `asprim`, although implemented from scratch. Please let me know of what you think. Could it use more tests? Should I also leave a safety notice that some conversions with `as` are currently UB (rust-lang/rust#10184)? 


21: Add checked shifts r=cuviper a=fabianschuiki

Add traits `CheckedShl` and `CheckedShr` that correspond to the standard
library's `checked_shl` and `checked_shr` functions. Implement the trait
on all primitive integer types by default, akin to what the standard
library does.

The stdlib is somewhat inconsistent when it comes to the type of the
shift amount. The `checked_*` functions have a `u32` shift amount, but
the `std::ops::{Shl,Shr}` traits are generic over the shift amount. Also
the stdlib implements these traits for all primitive integer types as
right-hand sides. Our implementation mimics this behaviour.
2018-01-18 05:37:47 +00:00
Eduardo Pinho 31218add95 Include note for implementers of AsPrimitive 2018-01-14 21:23:19 +00:00
Fabian Schuiki 809ccff63f Fix checked shift RHS to u32, drop from PrimInt
Make the checked left and right shifts take a `u32` as right-hand side,
which is more consistent with the other checked operations. Also drop
`CheckedShl` and `CheckedShr` from the `PrimInt` trait, to not break
existing code. Add doctests for the two traits.
2018-01-13 15:02:38 +01:00
Yoan Lecoq ddd664ae2b RealNum -> Real 2018-01-06 15:51:10 +01:00
Yoan Lecoq 52c87dd410 Remove 2018-01-05 10:01:29 +01:00
Yoan Lecoq 5a6997c1c8 Add RealNum trait 2018-01-05 09:06:23 +01:00
svartalf 53ab360d94 std::fmt::Display implemented for ParseFloatError 2018-01-04 16:46:02 +03:00
Fabian Schuiki 21dfae004c Add checked shifts
Add traits `CheckedShl` and `CheckedShr` that correspond to the standard
library's `checked_shl` and `checked_shr` functions. Implement the trait
on all primitive integer types by default, akin to what the standard
library does.

The stdlib is somewhat inconsistent when it comes to the type of the
shift amount. The `checked_*` functions have a `u32` shift amount, but
the `std::ops::{Shl,Shr}` traits are generic over the shift amount. Also
the stdlib implements these traits for all primitive integer types as
right-hand sides. Our implementation mimics this behaviour.
2018-01-03 16:25:13 +01:00
Eduardo Pinho af693fef48 Add safety notice in AsPrimitive docs 2017-12-21 21:03:25 +00:00
Eduardo Pinho 773a222237 Add conversions from `bool` in `AsPrimitive` 2017-12-20 21:41:45 +00:00
Eduardo Pinho 741e1f4e09 Add AsPrimitive trait, impls and tests 2017-12-20 00:05:36 +00:00
Josh Stone 42a610d323 Move num-traits to its own repo
All the prior `num` history is kept, so old `num-traits` tags are still
valid, but future development here will be just for `num-traits`.
2017-12-18 17:35:41 -08:00
Jacob Kiesel f8dcec366b Made requested changes. 2017-02-07 13:02:32 -07:00
Jacob Kiesel 182e08a091 Add inline attribute 2017-02-07 10:49:28 -07:00
Jacob Kiesel b346f9c2df Add a new line for improved formatting. 2017-02-07 10:18:23 -07:00
Jacob Kiesel a5445b7619 Adding documentation comments 2017-02-07 10:18:23 -07:00
Jacob Kiesel 07ff5b62b9 Changing to >= and <= as it's a slight optimization 2017-02-07 09:52:23 -07:00
Jacob Kiesel 28633b7e6d Add clamp function 2017-02-07 09:48:34 -07:00
bluss ff2a350e98 Use the integer32 playground 2016-11-02 19:51:10 +01:00
bluss e4a6956e5a Add doc(html_root_url) and other doc attrs to each crate
Also update to use https instead of http. This avois mixed content
degradation on docs.rs.

The doc root URLs are correct as they are, the URL does not include the
crate name itself.
2016-11-01 02:14:23 +01:00
Vinzent Steinberg 3e4595eac6 Move functions remaining in num to num-traits
Fixes #102.
2016-05-13 10:38:14 +02:00
Josh Stone 865c491ce2 num: remove `pub` from sub-crates
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.
2016-04-29 15:36:17 -07:00
Łukasz Jan Niemier 58b5fe5883 Serializers dependencies 2016-04-11 20:43:07 +02:00
Josh Stone c9d82acf00 test_features.sh: re-enable as a simple build 2016-03-25 18:30:27 -07:00
Josh Stone 9d439e7860 num: don't need std::hash even for testing 2016-03-25 17:50:51 -07:00
Josh Stone 529d7634dd num: use original num_foo crate names for imports
Rust 1.0.0 can't seem to find `foo::bar` imports from renamed crates like
`pub use num_foo as foo`.  Use `num_foo::bar` instead.
2016-03-25 17:50:38 -07:00
Łukasz Jan Niemier b962e2b708 Remove leftovers 2016-03-25 12:40:34 +01:00
Łukasz Jan Niemier 96e9166b0a Extract iter 2016-03-11 01:06:37 +01:00
Łukasz Jan Niemier ed076070e6 Extract complex 2016-03-11 01:06:37 +01:00
Łukasz Jan Niemier 2a67a5b86e Extract num-rational 2016-03-11 01:06:37 +01:00
Łukasz Jan Niemier 54685c46a1 Extract rational 2016-03-11 01:06:37 +01:00
Łukasz Jan Niemier 2176b7048c Extract bigint 2016-03-11 01:06:05 +01:00
Łukasz Jan Niemier f1a80857ee Extract integer module 2016-03-11 01:06:05 +01:00
Łukasz Jan Niemier c124be549f Move traits to separate crate 2016-03-11 01:06:05 +01:00
Murarth df34563eda Expose `BigUint::bits` through `BigInt` 2016-03-05 13:46:22 -07:00
Erick Tryzelaar 112923eec5 Add support for Serde 0.7.
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)`.
2016-02-27 00:07:32 -08:00
Josh Stone 4e66bbe6a7 Avoid overflows in Ratio's Ord::cmp
Fixes #7
2016-02-22 18:10:29 -08:00
Josh Stone 8be7e7bab5 Simplify some Ratio methods 2016-02-22 18:09:21 -08:00
Josh Stone 5b2cb8df97 Simplify Integer dependant traits
- 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.
2016-02-22 15:34:10 -08:00
Mike MacDonald (crazymykl) 9def836603 Allow unary `+` when parsing BigInt from strings 2016-02-21 18:09:56 -05:00
Felix S. Klock II d298262573 Reorder ops in LCM to avoid overflow. Fix #166 2016-02-18 22:49:34 +01:00
Josh Stone ebed6756de impl Debug for ParseFloatError
Fixes #165.
2016-02-16 15:37:06 -08:00
Josh Stone 9b2116d78f Fix spelling of "occurred". 2016-02-05 16:04:34 -08:00
est31 3c5ecdc636 Add checked_pow function 2016-02-03 06:43:40 +01:00
Josh Stone afee4e4662 Change PrimInt shift names to signed/unsigned 2016-01-28 22:37:18 -08:00
Josh Stone d798bd4fb8 Add arithmetic/logical shifts to PrimInt 2016-01-27 23:30:33 -08:00
Josh Stone b454a14bc4 bigint: avoid new allocations for small shifts
Before:
    test shl               ... bench:       7,312 ns/iter (+/- 218)
    test shr               ... bench:       5,282 ns/iter (+/- 243)

After:
    test shl               ... bench:       4,946 ns/iter (+/- 88)
    test shr               ... bench:       4,121 ns/iter (+/- 52)
2016-01-27 23:08:47 -08:00
Oliver Middleton 95d8ccdb24 bigint: Make fmt traits take into account formatting parameters 2016-01-22 00:35:35 +00:00
Colin Davidson 3c7441ab0a Add Binary, Octal to BigUint. Add UpperHex, LowerHex, Binary, Octal to BigInt 2016-01-18 21:19:21 -08:00
Colin Davidson a89a2e82e6 Fix test names
Upper and Lower test names were swapped
2016-01-17 12:17:52 -08:00
Colin Davidson d5d7c855b0 Add tests for UpperHex and LowerHex 2016-01-17 12:13:45 -08:00
Colin Davidson 04000436b1 Switch to ascii uppercase function 2016-01-16 22:27:56 -08:00
Colin Davidson 80c4d01b7f Add LowerHex and UpperHex formatting 2016-01-16 21:52:47 -08:00
Alex Burka 5962dd29c9 remove stale #[deriving(Zero)] docs
Fixes #156.
2016-01-15 13:29:34 -05:00
Homu e65fd4e5b2 Auto merge of #150 - ollie27:bigint_to_str, r=cuviper
bigint: small to_str_radix optimization

Before:
```
test fac_to_string     ... bench:       1,630 ns/iter (+/- 34)
test fib_to_string     ... bench:         359 ns/iter (+/- 11)
test to_str_radix_02   ... bench:       3,097 ns/iter (+/- 19)
test to_str_radix_08   ... bench:       1,146 ns/iter (+/- 38)
test to_str_radix_10   ... bench:       4,248 ns/iter (+/- 36)
test to_str_radix_16   ... bench:         881 ns/iter (+/- 44)
test to_str_radix_36   ... bench:       8,073 ns/iter (+/- 75)
```
After:
```
test fac_to_string     ... bench:       1,492 ns/iter (+/- 20)
test fib_to_string     ... bench:         368 ns/iter (+/- 7)
test to_str_radix_02   ... bench:       2,038 ns/iter (+/- 47)
test to_str_radix_08   ... bench:         812 ns/iter (+/- 9)
test to_str_radix_10   ... bench:       3,919 ns/iter (+/- 40)
test to_str_radix_16   ... bench:         703 ns/iter (+/- 58)
test to_str_radix_36   ... bench:       7,852 ns/iter (+/- 81)
```
2016-01-13 08:59:01 +09:00
Brian Smith 9e3e8552ac Make `rand` and `rustc-serialize` dependencies optional.
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.
2016-01-07 16:30:50 -10:00
Josh Stone 22722ac55d pow: shift exp powers of 2 before accumulating
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)
2016-01-06 18:32:01 -08:00
Oliver Middleton d2b23d0f29 bigint: small to_str_radix optimization
Before:

test fac_to_string     ... bench:       1,630 ns/iter (+/- 34)
test fib_to_string     ... bench:         359 ns/iter (+/- 11)
test to_str_radix_02   ... bench:       3,097 ns/iter (+/- 19)
test to_str_radix_08   ... bench:       1,146 ns/iter (+/- 38)
test to_str_radix_10   ... bench:       4,248 ns/iter (+/- 36)
test to_str_radix_16   ... bench:         881 ns/iter (+/- 44)
test to_str_radix_36   ... bench:       8,073 ns/iter (+/- 75)

After:

test fac_to_string     ... bench:       1,492 ns/iter (+/- 20)
test fib_to_string     ... bench:         368 ns/iter (+/- 7)
test to_str_radix_02   ... bench:       2,038 ns/iter (+/- 47)
test to_str_radix_08   ... bench:         812 ns/iter (+/- 9)
test to_str_radix_10   ... bench:       3,919 ns/iter (+/- 40)
test to_str_radix_16   ... bench:         703 ns/iter (+/- 58)
test to_str_radix_36   ... bench:       7,852 ns/iter (+/- 81)
2016-01-05 18:01:12 +00:00
Łukasz Jan Niemier 8e267ee4cb Fix docs and function position 2015-12-30 10:57:54 +01:00
Łukasz Jan Niemier 458e9594d4 Add complex `i` constant function 2015-12-29 16:36:08 +01:00
Oliver Middleton 2c235d2dde bigint: cleanup float conversions 2015-12-24 18:15:56 +00:00
Oliver Middleton a480c7ca6c bigint: fix float conversions
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.
2015-12-23 07:30:40 +00:00
Josh Stone 2e3e575117 Fix the feature masks in the approx_sqrt doc example
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.
2015-12-21 12:21:47 -08:00
Homu 9b45e6dd9b Auto merge of #139 - ollie27:bigint_hash, r=cuviper
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.
2015-12-17 15:25:49 +09:00
Josh Stone 7593c17fa7 More bigint str_radix performance tweaks 2015-12-16 21:49:18 -08:00
Oliver Middleton e152bbcb46 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.
2015-12-16 19:08:24 +00:00
Josh Stone c9e15aef2d bigint: use full cmp results for checked_sub 2015-12-15 22:31:46 -08:00
Josh Stone 4a8752274c bigint: slightly improve multiply/divide performance
Using `vec![0; len]` initialization is a little faster.

Before:
     test multiply_0        ... bench:         354 ns/iter (+/- 6)
     test multiply_1        ... bench:      33,966 ns/iter (+/- 1,508)
     test multiply_2        ... bench:   3,663,686 ns/iter (+/- 60,880)
     test divide_0          ... bench:         891 ns/iter (+/- 51)
     test divide_1          ... bench:      17,316 ns/iter (+/- 387)
     test divide_2          ... bench:   1,290,378 ns/iter (+/- 73,016)

After:
     test multiply_0        ... bench:         351 ns/iter (+/- 39)
     test multiply_1        ... bench:      30,827 ns/iter (+/- 680)
     test multiply_2        ... bench:   3,692,968 ns/iter (+/- 91,146)
     test divide_0          ... bench:         902 ns/iter (+/- 14)
     test divide_1          ... bench:      16,981 ns/iter (+/- 102)
     test divide_2          ... bench:   1,146,367 ns/iter (+/- 60,152)
2015-12-15 22:07:34 -08:00
Josh Stone b7724a6650 Simplify BigInt Mul forwarding 2015-12-15 22:06:59 -08:00
Josh Stone 22ff3f918d bigint: improve from_str_radix performance
Before:
    test from_str_radix_02 ... bench:       8,432 ns/iter (+/- 280)
    test from_str_radix_08 ... bench:       7,397 ns/iter (+/- 95)
    test from_str_radix_10 ... bench:       7,344 ns/iter (+/- 142)
    test from_str_radix_16 ... bench:       6,753 ns/iter (+/- 157)
    test from_str_radix_36 ... bench:       7,093 ns/iter (+/- 60)

After:
    test from_str_radix_02 ... bench:       3,295 ns/iter (+/- 81)
    test from_str_radix_08 ... bench:       1,377 ns/iter (+/- 56)
    test from_str_radix_10 ... bench:       1,583 ns/iter (+/- 16)
    test from_str_radix_16 ... bench:       1,483 ns/iter (+/- 53)
    test from_str_radix_36 ... bench:       1,628 ns/iter (+/- 27)
2015-12-15 21:59:51 -08:00
Josh Stone 49529895a2 bigint: greatly improve to_str_radix performance
Before:
     test fac_to_string     ... bench:      18,183 ns/iter (+/- 310)
     test fib_to_string     ... bench:         819 ns/iter (+/- 8)
     test to_str_radix_02   ... bench:     204,479 ns/iter (+/- 2,826)
     test to_str_radix_08   ... bench:      68,275 ns/iter (+/- 769)
     test to_str_radix_10   ... bench:      61,809 ns/iter (+/- 907)
     test to_str_radix_16   ... bench:      51,438 ns/iter (+/- 539)
     test to_str_radix_36   ... bench:      39,939 ns/iter (+/- 976)

After:
     test fac_to_string     ... bench:       1,204 ns/iter (+/- 16)
     test fib_to_string     ... bench:         269 ns/iter (+/- 3)
     test to_str_radix_02   ... bench:       2,428 ns/iter (+/- 80)
     test to_str_radix_08   ... bench:         820 ns/iter (+/- 38)
     test to_str_radix_10   ... bench:       2,984 ns/iter (+/- 303)
     test to_str_radix_16   ... bench:         689 ns/iter (+/- 25)
     test to_str_radix_36   ... bench:       7,995 ns/iter (+/- 100)
2015-12-15 21:57:27 -08:00
Oliver Middleton 9c7c995631 bigint: add tests for From implementations 2015-12-12 23:17:37 +00:00
Oliver Middleton 8e1c6a3c11 bigint: add From implementations
From all primative unsigned ints to BigUint
From all primative ints to BigInt
From BigUint to BigInt

Closes: #117
2015-12-12 23:16:03 +00:00
Kent Overstreet fa372e230b Multiply/divide torture test 2015-12-10 15:28:01 -09:00
Kent Overstreet 79928b3185 Improve division performance
Before:
test divide_0      ... bench:       4,058 ns/iter (+/- 255)
test divide_1      ... bench:     304,507 ns/iter (+/- 28,063)
test divide_2      ... bench: 668,293,969 ns/iter (+/- 25,383,239)

After:
test divide_0      ... bench:         874 ns/iter (+/- 71)
test divide_1      ... bench:      16,641 ns/iter (+/- 1,205)
test divide_2      ... bench:   1,336,888 ns/iter (+/- 77,450)
2015-12-10 15:25:56 -09:00
Kent Overstreet 08b0022aab Improve multiply performance
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)
2015-12-10 15:25:42 -09:00
Kent Overstreet 496ae0337c Add add/subtract functions that work in place, on slices
This is needed for the multiply optimizations in the next patch.
2015-12-10 12:33:28 -09:00
Josh Stone 7fa80f7786 Update intros to better reflect the breadth of num
Closes #132.
2015-11-21 16:39:00 -08:00
Josh Stone e41016975f bigint: expand op tests to cover all val/ref combinations 2015-11-16 11:06:39 -08:00
Josh Stone 978d14aa56 bigint: refactor BigInt ops
Try to let the underlying BigUint op implement val/ref forwarding as
much as possible, to reduce cloning.
2015-11-16 11:04:41 -08:00
Josh Stone ce3d375b21 bigint: refactor BigUint ops
As much as possible, ops are forwarded to val-ref so the LHS memory can
be reused for the result.  This reduces the number of clones required.
2015-11-16 10:56:30 -08:00
Josh Stone 7781256041 bigint: use a simple let-Some-pop to truncate zeros 2015-11-16 10:20:42 -08:00
Josh Stone c310e5da8e complex: forward ops to val-val
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.
2015-11-16 10:10:59 -08:00
Josh Stone b4026b9fec Update repo and doc links 2015-11-12 18:40:13 -08:00
Homu 93db3998ef Auto merge of #131 - murarth:str-radix, r=cuviper
Improve `to_str_radix` implementation
2015-11-12 03:33:55 +09:00
Homu 879f2c868e Auto merge of #125 - IvanUkhov:real-arithmetic, r=cuviper
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
2015-11-12 03:03:06 +09:00
Murarth 786541bd24 Improve `to_str_radix` implementation
Uses in-place division with `u32` divisor to provide a significant speed
improvement.
2015-11-09 13:50:25 -07:00
Abhishek Chanda 7acad5ac33 Implement some more functions for the complex type 2015-11-05 14:20:37 -08:00
Ivan Ukhov b57e4a4db4 Implement arithmetic with real for complex 2015-10-31 06:38:38 +01:00
Abhishek Chanda e5099930e7 Implement is_nan for the complex type
Also run the associated test
2015-10-30 15:35:04 -07:00
Josh Stone 49237f6f45 integer: expand tests for gcd min_value()
- 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
2015-10-30 00:40:21 -07:00
Josh Stone 642787fbd2 Merge pull request #121 from wrieger93/issue_120
Add a function that returns the sign of a BigInt.
2015-10-27 12:13:17 -07:00
Josh Stone 9e5a209dcd Merge pull request #123 from murarth/ratio-parse-fix
Check for zero denominator when parsing Ratio
2015-10-23 10:23:18 -07:00
David Ripton e86913924e Remove extra parentheses to fix a warning
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
2015-10-19 09:43:47 -04:00
Murarth b2767517f4 Check for zero denominator when parsing Ratio
Closes #122
2015-10-16 13:10:16 -07:00
Łukasz Niemier 2e4afbc9ba Merge pull request #112 from Emerentius/master
Implement Stein's algorithm for gcd
2015-10-15 13:25:33 +02:00
William Rieger 1eb3deaca8 Add a function that returns the sign of a BigInt. 2015-10-06 00:32:32 -07:00
William Rieger c715405b30 Add functions and tests. 2015-09-13 18:19:00 -04:00
Emerentius 499def9207 Added tests for gcd
compare gcd with euclidean gcd
test panics on gcd(min_val, min_val) and gcd(min_val, 0) for signed integers
2015-09-11 20:33:39 +02:00
Emerentius 59089d9d5c Made it panic on gcd = min_val in debug mode
The additional call to .abs() causes a panic on the min value. There are
no other negative values it can encounter.
Does nothing in release mode
2015-09-11 20:27:06 +02:00
Emerentius 82a6ab2f30 comment corrected
referenced a,b, which aren't defined
changed to n,m
2015-09-07 16:50:58 +02:00
Emerentius e892054813 implement Stein's algorithm for gcd 2015-09-07 03:15:16 +02:00
William Rieger 50d89519a2 Merge branch 'master' into develop
Conflicts:
	src/complex.rs
2015-09-04 18:56:40 -04:00
William Rieger ff6a40cc88 Remove reduntant Num trait from an impl. 2015-09-04 17:48:31 -04:00
William Rieger 98b9df622f Add mathematical functions for complex numbers.
For Complex<T: Clone + Float>, the following functions along with
corresponding tests were added:
- exp
- sin, cos, tan
- sinh, cosh, tanh
2015-09-04 02:08:31 -04:00
Gustorn 3ccb4bd6c5 Added Sized trait bound for traits that requite it
See rust-lang/rfcs#1214
2015-08-19 19:11:55 +02:00
Alex Crichton b57accf314 Update to rust nightly 2015-08-10 12:11:56 -07:00
root 4c123a9e71 Make rustc-serialize, bignum, rational, complex into opt-out features
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.
2015-06-02 23:48:06 +02:00
Alex Crichton 0a7847cd65 Merge pull request #99 from b52/issue-93-float-min-positive
Provide generic around f32/f64::MIN_POSITIVE
2015-05-26 07:23:41 -07:00
Oliver Mader 785e07d83c Re-export `cast` for ergonomic reasons 2015-05-26 15:03:34 +02:00