Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Signed zero discrepancy vs. Python on float np.remainder #6317

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

Closed
pitrou opened this issue Sep 15, 2015 · 9 comments
Closed

Signed zero discrepancy vs. Python on float np.remainder #6317

pitrou opened this issue Sep 15, 2015 · 9 comments

Comments

@pitrou
Copy link
Member

pitrou commented Sep 15, 2015

>>> np.remainder(-1.,1.)
-0.0
>>> (-1.) % 1.
0.0

It seems Python is right here, since the result is supposed to be of the same sign as the dividend, e.g.:

>>> np.remainder(-1.01,1.)
0.98999999999999999

And actually:

>>> (-1) % (-1.)
-0.0
>>> np.remainder(-1.,-1.)
-0.0
@ahaldane
Copy link
Member

Speaking of np.remainder, I also recently noticed that it treats integer zero strangely:

>>> np.reciprocal(np.array(0))
-9223372036854775808

Probably should return 0 with a warning, like np.int32(1)/np.int32(0). Thus for ints, reciprocal would return 1 if x==1, otherwise 0.

@ahaldane
Copy link
Member

Oh oops misread, you are talking about np.remainder

@argriffing
Copy link
Contributor

fwiw that's a float nan interpreted as an integer (sorry for continuing off-topic..)

>>> np.float64(np.nan).astype(int)
-9223372036854775808

@ahaldane
Copy link
Member

Yes.

Sorry for noise, please continue any side discussion at #6318.

@mhvk
Copy link
Contributor

mhvk commented Sep 15, 2015

The problem with how the remainder is calculated also affects divmod -- see #6127

@pitrou
Copy link
Member Author

pitrou commented Sep 15, 2015

The problem with how the remainder is calculated also affects divmod -- see #6127

I'm not sure this is the same issue. Here the result's value is correct, just the sign is incorrect (note that 0.0 == -0.0).

@mhvk
Copy link
Contributor

mhvk commented Sep 15, 2015

@pitrou - you may be right that the problems are separate. My link was triggered by the problem in divmod being in part because the sign of the remainder is checked and then the remainder is changed without changing the quotient. It does seem that part of the code needs some looking after...

@tlatorre-uchicago
Copy link
Contributor

I think the root of the problem is how the sign is calculated here:

floordiv = (a / b > 0) ? 0.0@c@ : -0.0@c@;
.

From the most recent commit to that file it looks like the author's intention was to copy the behavior of the python function float_divmod, however the sign check there looks like this:

/* div is zero - get the same sign as the true quotient */
floordiv = copysign(0.0, vx / wx); /* zero w/ sign of vx/wx */

and copysign is defined as:

double
copysign(double x, double y)
{
    /* use atan2 to distinguish -0. from 0. */
    if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
        return fabs(x);
    } else {
        return -fabs(x);
    }
}

however the numpy implementation does not use the atan trick.

@bsipocz
Copy link
Member

bsipocz commented Oct 15, 2019

Closing as I think this has been fixed, on current master:

In [75]: >>> np.remainder(-1.,1.)                                                                                       
Out[75]: 0.0

In [76]: >>> (-1.) % 1.                                                                                                 
Out[76]: 0.0

@bsipocz bsipocz closed this as completed Oct 15, 2019
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

6 participants