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

Implementing epsilon function to retrieve EPSILON constant #231

Merged
merged 2 commits into from
Sep 22, 2016

Conversation

AtheMathmo
Copy link
Contributor

Hey!

This PR exposes a new epsilon function in the Float trait so that users can access the EPSILON constant on the float types. I figured as this was such a minimal change it was easier to get a PR in offering the change then write up an issue.

For me this is a valuable addition. When writing linear algebra or other optimization routines with generic floats we often want to check if some stopping criteria is reached, often something like: (a - b).abs() < eps. Having access to a standard small value would make this a little easier.

Copy link
Member

@hauleth hauleth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @cuviper

If you think it is ok then r=me

@cuviper
Copy link
Member

cuviper commented Sep 19, 2016 via email

@AtheMathmo
Copy link
Contributor Author

Thank you for your comments! I didn't realize this was a breaking change - sorry about that!

I'm assuming that to avoid breakage you are implying we could provide a default implementation? I'm not too sure how this would work (without bringing in FromPrimitive or something - which is also a breaking change?). If you have any suggestions I'm happy to try and implement them or you can feel free to push directly to this branch.

@cuviper
Copy link
Member

cuviper commented Sep 19, 2016

Right, it avoids the break if you have a default impl. Float doesn't have FromPrimitive, but it does have NumCast which can probably help. (I always thought that was a weirdly defined trait.)

@AtheMathmo
Copy link
Contributor Author

So perhaps we can provide a default implementation like this:

fn epsilon() -> T {
    T::from(f32::EPSILON)
}

And we leave the overrides for f32 and f64 in the macro? This way we have the correct implementations for f32 and f64 with a sensible (enough?) default for users implementing Float. If that is acceptable I'm happy to make this change.

@cuviper
Copy link
Member

cuviper commented Sep 22, 2016

Yeah, that's probably Good Enough -- it's hard to be confident that any other heuristic would truly be better. It's Self instead of T, and you get an Option so you should expect("some panic message").

@AtheMathmo
Copy link
Contributor Author

I've implemented the default as above. If you have a better panic message let me know!

(I also added a panic header in the docs - I can remove it if you prefer)

@cuviper
Copy link
Member

cuviper commented Sep 22, 2016

Looks good, thanks! When we do make breaking changes, we might drop this default impl to make it mandatory for implementors.

@homu r=hauleth

@homu
Copy link
Contributor

homu commented Sep 22, 2016

📌 Commit 381942e has been approved by hauleth

@homu homu merged commit 381942e into rust-num:master Sep 22, 2016
homu added a commit that referenced this pull request Sep 22, 2016
Implementing epsilon function to retrieve EPSILON constant

Hey!

This PR exposes a new `epsilon` function in the `Float` trait so that users can access the `EPSILON` constant on the float types. I figured as this was such a minimal change it was easier to get a PR in offering the change then write up an issue.

For me this is a valuable addition. When writing linear algebra or other optimization routines with generic floats we often want to check if some stopping criteria is reached, often something like: `(a - b).abs() < eps`. Having access to a standard _small value_ would make this a little easier.
@homu
Copy link
Contributor

homu commented Sep 22, 2016

⚡ Test exempted - status

@AtheMathmo
Copy link
Contributor Author

Looks good, thanks! When we do make breaking changes, we might drop this default impl to make it mandatory for implementors.

Great, thank you!

Would you like me to open an issue to track the removal of the default impl with the next minor version bump? (I agree that it is probably best to remove it)

@cuviper
Copy link
Member

cuviper commented Sep 22, 2016

Sure, a tracking issue would be fine.

@eddyb
Copy link

eddyb commented Oct 3, 2016

FWIW this broke glm and simple_gaussian (click Logs to see the output) due to ambiguity.

@cuviper
Copy link
Member

cuviper commented Oct 3, 2016

That's unfortunate, but allowable as a minor change under RFC#1105, thanks to the default impl. It sure would be nice to have Crater access ourselves though. I'll file heads-up bugs on those packages.

@eddyb
Copy link

eddyb commented Oct 3, 2016

cc @brson for the crater thing.

remexre pushed a commit to remexre/num that referenced this pull request Jun 1, 2017
…n-to_value

Serialize Map with integer keys
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

Successfully merging this pull request may close these issues.

5 participants