Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unitless quantities should convert to primitives #64

Closed
nickbabcock opened this issue Apr 4, 2018 · 7 comments
Closed

Unitless quantities should convert to primitives #64

nickbabcock opened this issue Apr 4, 2018 · 7 comments

Comments

@nickbabcock
Copy link

I expected something along the lines of the following to work:

let start = Time::new::<second>(1.0);
let end = Time::new::<second>(10.0);
let interval = Time::new::<second>(3.0);

let buckets: f32 = ((end - start) / interval).into()

I'm curious if I'm missing something, as I'm still exploring the library. Currently the workaround seems to be

(end - start).get(second) / interval.get(second)

Which isn't as pretty 😄

@iliekturtles
Copy link
Owner

The way to do this right now is to directly access the value member of the Quantity struct. However I would like to make value private in the future so a better way is needed.

I haven't put much thought into dimensionless quantities at this point as I wanted to complete #3. This issue touches on dimensionless quantities because similar to the difference between ThermodynamicTemperature and TemperatureInterval there are multiple distinct dimensionless types: Ratio, as seen here, but also types like Angle (m/m) and PowerEfficiency (W/W).

I'll leave the issue open with the intention of implementing impl<U, V> From<V> for Quantity<DimensionOne, U, V>.

    let start = Time::new::<second>(1.0);
    let end = Time::new::<second>(10.0);
    let interval = Time::new::<second>(3.0);

    let buckets: f32 = ((end - start) / interval).value;

    println!("{}", buckets);

@nickbabcock
Copy link
Author

The temporary workaround worked great. uom helped me solve a couple head scratching bugs.

I'm excited for when dimensionless quantities become first class citizens 😄

@iliekturtles
Copy link
Owner

Awesome! Glad to hear it helped solve some problems for you!

@shradej1
Copy link

Is there a dimensionless unit that can be used to get at the value in a unit-safe way? Like,

let buckets: f32 = ((end - start) / interval).get(unit);

@iliekturtles
Copy link
Owner

Not yet, but there will be "soon." I have most of the changes complete in the dev-ratio branch to create a new Ratio quantity. You'll be able to do the following once it is merged:

let buckets: f32 = ((end - start) / interval).get(ratio);

@shradej1
Copy link

That's great! An Into conversion to primitives might be nice too, but I think the ratio approach is a more consistent treatment.

@iliekturtles
Copy link
Owner

From/Into will also be available for Ratios once changes are merged into master!

impl<U, V> ::lib::convert::From<V> for Ratio<U, V>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants