Comments (10)
AsPrimitive
was added in #17, and released back in num-traits 0.1.42.
from num-traits.
From @Binero on March 29, 2016 21:27
It also appears impossible to cast from f32
or f64
to num::traits::Float
.
from num-traits.
From @bluss on March 29, 2016 21:33
Yes, Float
is not a "trait object compatible" trait (a.k.a. "object safe"), so it can not be used as a type like that. I don't think that's expected, not every trait is made to be object compatible.
from num-traits.
From @Binero on March 31, 2016 13:8
It does make code generic over Float very hard to write, as it's not possible to use constants. In my specific instance, I had to multiply the Float
input by Euler's number, which was impossible. I cannot make a Float
from an f64
.
from num-traits.
From @bluss on March 31, 2016 13:30
Euler's number is accessible from Float
though, F::exp(F::one()) where F: Float
. Not so nice maybe..
from num-traits.
From @bluss on March 31, 2016 13:32
I'm using asprim in the mean time, but it is much better for crate interoperation if this kind of feature is already included in the float trait.
from num-traits.
From @hauleth on July 15, 2016 9:55
You can always use FromPrimitive
or From
.
from num-traits.
From @Enet4 on November 2, 2017 11:12
Just bumping this issue, as I believe that this crate could benefit from an AsPrim
trait. I have a use case where an N-dimensional volume of primitive elements are linearly transformed and converted to a compile-time value type T
on reading. The other traits don't work because I would prefer precision loss over not obtaining a value at all. The asprim
crate is nice, but this being covered by num
is almost a normal expectation, and would more easily implement the trait for other numeric types such as Complex
.
from num-traits.
more easily implement the trait for other numeric types such as Complex.
It's an infallible conversion -- would you just silently drop the imaginary part? I think it's better to make that explicit, like value.re.as_()
.
from num-traits.
From @Enet4 on November 2, 2017 16:50
It's an infallible conversion -- would you just silently drop the imaginary part? I think it's better to make that explicit, like
value.re.as_()
.
That is true, this conversion might bring some confusion. The main problem is that I'm dealing with the boundaries between a data type only known at run time and a compile-time type decided by the API user. Perhaps I need to rethink my approach, but I couldn't make such a conversion explicit (at least not without specialization or something similar).
from num-traits.
Related Issues (20)
- Why not make zero() and one() as const functions? HOT 2
- Add trigonometry functions HOT 1
- constifying traits in an nighlty branch or feature gate it? (with #[const_trait]) HOT 2
- `cargo doc` fails if they run it with nightly Rust HOT 2
- New nightly version broke `cargo-doc`, unintended use of unstable feature HOT 3
- Is there any hope of seeing a minor release anytime soon? HOT 1
- add a quotien_and_rem_euclid method to Euclid trait HOT 1
- Document and/or allow truncation/rounding when casting from float to int? HOT 4
- Feature request: OverflowingShl HOT 2
- Feature request: can we have a trait for checked_add_signed uX implementations?
- Add `BitOrAssign` etc. to `PrimInt` HOT 4
- Introduce derive macros HOT 1
- Adding {ilog, ilog2, ilog10} to PrimInt HOT 1
- Add `trait UnsignedAbs` HOT 3
- feat: ToPrimitive split into ToFloatPrimitive and ToIntegerPrimitive HOT 7
- Add `round_to_digit` method to `Float` trait. HOT 2
- `struct` built on top of `pub trait Num` can't compile if `pub trait Num` doesn't impl `Debug` HOT 4
- Add `Hash` as a trait bound for `PrimInt` HOT 2
- Add is_signed HOT 1
- [Build-script] unable to determine target details for custom target-spec HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from num-traits.