Commit Graph

30 Commits

Author SHA1 Message Date
Josh Stone d2bf4e04e4 Run cargo fmt 2018-07-12 17:09:22 -07:00
Josh Stone 21e3620999 doc: fix a typo, s/the/then/ 2018-06-20 13:39:08 -07:00
Josh Stone dd7900d62f Avoid closures in default `ToPrimitive` methods
In `to_f64()`, we also try `to_u64()` if `to_i64()` failed.
2018-06-20 13:10:41 -07:00
Josh Stone d968efbc76 Avoid `as` casts in default FromPrimitive methods
Particularly, the default `from_f64` used `n as i64`, which has
undefined behavior on overflow, kind of defeating the purpose here.
Now we use a checked `to_i64()` for this, and even try `to_u64()` as a
fallback for completeness.

(All of the primitive implementations already do better, at least.)
2018-06-20 13:05:03 -07:00
Josh Stone 51f6c57c4b Automatically detect support for i128/u128 2018-05-11 15:50:48 -07:00
Josh Stone bbbc2bd1d7 impl 128-bit numeric casts
This includes new conditional methods `ToPrimitive::{to_i128,to_u128}`
and `FromPrimitive::{from_i128,from_u128}`.  Since features can only be
additive, these methods must not cause a breaking change to anyone when
enabled -- thus they have a default implementation that converts through
64-bit values.  Types that can do better with a full 128-bit integer,
like bigint or floating-point, will probably want to override these.
2018-05-07 12:28:53 -07:00
Josh Stone a4d234c253 Further simplify float-to-int range checks
We don't actually need to compute the `trunc()` value, as long as we can
figure out the right values for the exclusive range `(MIN-1, MAX+1)` to
measure the same truncation effect.
2018-03-13 13:38:17 -07:00
Josh Stone f0ed42b3bc Test edge cases of ToPrimitive with ints 2018-03-11 01:37:27 -08:00
Josh Stone 50868c60d2 Refactor to_primitive_int/uint macros 2018-03-11 01:37:00 -08:00
Josh Stone 6d7bbb1b53 Mask debug prints no-std mode 2018-03-11 01:36:17 -08:00
Josh Stone d195eafbe2 Simplify the to_primitive_float macros 2018-03-10 23:33:47 -08:00
Josh Stone f6dc4d29a4 Add thorough tests of float to int edge cases 2018-03-10 23:05:02 -08:00
Josh Stone b025c273c7 Rewrite range checks in float ToPrimitive macros 2018-03-10 23:01:30 -08:00
Dan Barella 8e27c7327d Rename some tests. 2018-03-10 15:14:23 -08:00
Dan Barella aab7098acd Reformat macros. 2018-03-10 15:14:23 -08:00
Alexander Popiak c32cb5c65b Patch in apopiak@'s changes from github.com/rust-num/num/pull/135/. 2018-03-10 15:14:23 -08:00
Dan Barella ab8fda7654 Change assert form. 2018-03-10 15:14:23 -08:00
Dan Barella ecb0816c83 Remove an unneeded assert. 2018-03-10 15:14:23 -08:00
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 ac503261ca Forward FloatCore to inherent methods when possible 2018-02-27 16:33:04 -08: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 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
Eduardo Pinho 31218add95 Include note for implementers of AsPrimitive 2018-01-14 21:23:19 +00: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