10000 [MRG] MNT: Use `fmax` when finding the maximum by jakirkham · Pull Request #12005 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG] MNT: Use fmax when finding the maximum #12005

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

Merged
merged 1 commit into from
Sep 5, 2018

Conversation

jakirkham
Copy link
Contributor

Instead of adding an if to check for values that become the new max, simply use fmax to get the maximum and update the value. This improves readability. It may improve performance as fmax can be a single assembly instruction. Though most compilers can probably figure this out anyways.

Instead of adding an `if` to check for values that become the new max,
simply use `fmax` to get the maximum and update the value. This improves
readability. It may improve performance as `fmax` can be a single
assembly instruction. Though most compilers can probably figure this out
anyways.
@jakirkham jakirkham changed the title Use fmax when finding the maximum MNT: Use fmax when finding the maximum Sep 4, 2018
Copy link
Member
@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

I would be surprised if cython did not translate this usage of python max into fmax, fwiw

@jakirkham
Copy link
Contributor Author

Was wondering that too, but found Cython translate it into a comparison like the one written. For example, Cython takes this function...

cpdef float my_max(float a, float b) nogil:
    return max(a, b)

...and turns the max into this.

  __pyx_t_1 = __pyx_v_b;
  __pyx_t_2 = __pyx_v_a;
  if (((__pyx_t_1 > __pyx_t_2) != 0)) {
    __pyx_t_3 = __pyx_t_1;
  } else {
    __pyx_t_3 = __pyx_t_2;
  }
  __pyx_r = __pyx_t_3;

(There's of course more C code that Cython generates that has been snipped for clarity.)

@jakirkham jakirkham changed the title MNT: Use fmax when finding the maximum [MRG] MNT: Use fmax when finding the maximum Sep 4, 2018
@jnothman
Copy link
Member
jnothman commented Sep 5, 2018 via email

@qinhanmin2014 qinhanmin2014 merged commit dff84c8 into scikit-learn:master Sep 5, 2018
@jakirkham jakirkham deleted the use_fmax_enet_cd branch September 5, 2018 15:56
@jakirkham
Copy link
Contributor Author

Nope. I can understand why they would want max to behave that way actually. That code always works. fmax should really only be used with floating point values. Whereas that code works well for any type where comparisons might be defined. This is especially important when using specialized C++ objects that overload the comparison operator.

We could ask them about special casing max for floating point values. WDYT?

@jnothman
Copy link
Member
jnothman commented Sep 6, 2018 via email

jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Sep 6, 2018
Instead of adding an `if` to check for values that become the new max,
simply use `fmax` to get the maximum and update the value. This improves
readability. It may improve performance as `fmax` can be a single
assembly instruction. Though most compilers can probably figure this out
anyways.
jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Sep 17, 2018
Instead of adding an `if` to check for values that become the new max,
simply use `fmax` to get the maximum and update the value. This improves
readability. It may improve performance as `fmax` can be a single
assembly instruction. Though most compilers can probably figure this out
anyways.
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.

3 participants
0