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

mod1, fld1, rem1 broken #5570

Closed
mschauer opened this issue Jan 27, 2014 · 9 comments
Closed

mod1, fld1, rem1 broken #5570

mschauer opened this issue Jan 27, 2014 · 9 comments
Labels
kind:bug Indicates an unexpected problem or unintended behavior

Comments

@mschauer
Copy link
Contributor

julia> map(x -> int(mod1(uint(x),uint(5))), 0:15)'
1x16 Array{Int64,2}:
 5  1  2  3  4  5  5  1  2  3  4  5  1  2  3  4

It is like this since 0.1.2.

@GunnarFarneback
Copy link
Contributor

This is indeed broken. mod1(x,y) doesn't work at all for unsigned x,y when x>y. Moreover fld1(x,y) gives rather less useful results for signed x<=0 and incorrect results for unsigned x=0.

julia> [rem1(k,5) for k=-10:10]'
1x21 Array{Int64,2}:
 0  1  -3  -2  -1  0  1  -3  -2  -1  0  1  2  3  4  5  1  2  3  4  5

julia> rem1(uint(0),uint(11))
0x0000000000000005

@ivarne
Copy link
Sponsor Member

ivarne commented Jan 27, 2014

The definition for mod1 is in base/operators.jl:115

mod1{T<:Real}(x::T, y::T) = y-mod(y-x,y)

For moderately large x where x > y, y - x underflows and the result will only be accurate for y = 2^n

@GunnarFarneback
Copy link
Contributor

When I wrote fld1 above I actually meant rem1, as in the examples, but it's valid for fld1 too. Those are really only designed to be used for integer x > 0 and y > 0, in the context of 1-based indexing. The simplest solution for rem1 may be to document that it's only intended for x > 0 and otherwise refer to mod1. Or unexport it; the difference between rem and mod is how they behave for negative arguments and it's not clear that this distinction is interesting or even relevant for rem1/mod1.

As for the mod1 problem at the core of this issue there seems to be a very simple solution. It was broken by this commit with the comment

Also, in this commit: mod1 is now just as fast as mod.
That's a 213x improvement.

When I test it now I see no measurable speed difference, so just revert that change.

@ivarne
Copy link
Sponsor Member

ivarne commented Jan 28, 2014

We should at least add the old method definition for T <: Unsigned

@mschauer
Copy link
Contributor Author

Btw, @StefanKarpinski did you see this?

@mschauer
Copy link
Contributor Author

mschauer commented Jan 6, 2015

This should perhaps get a bug tag.

@mschauer mschauer changed the title Hiccup in mod1(Uint, Uint) Bug in mod1(Uint, Uint) Jan 6, 2015
@jiahao jiahao added the kind:bug Indicates an unexpected problem or unintended behavior label Jan 6, 2015
jiahao added a commit that referenced this issue Jan 6, 2015
Restore old implementation of mod1 that was overwritten in
3842422

Adds test of mod1 behavior from #5570

Note: #5570 is not fixed for fld1, rem1. The documentation bug of #8108
is not fixed.
@jiahao jiahao changed the title Bug in mod1(Uint, Uint) mod1, fld1, rem1 broken Jan 6, 2015
@jiahao jiahao mentioned this issue Jan 6, 2015
@jiahao
Copy link
Member

jiahao commented Jan 6, 2015

Note: mod1, fld1, rem1 have no tests.

jiahao added a commit that referenced this issue Jan 6, 2015
Restore old implementation of mod1 that was overwritten in
3842422

Adds test of mod1 behavior from #5570

Note: #5570 is not fixed for fld1, rem1. The documentation bug of #8108
is not fixed.
jiahao added a commit that referenced this issue Jan 13, 2015
Restore old implementation of mod1 that was overwritten in
3842422

Adds test of mod1 behavior from #5570

Note: #5570 is not fixed for fld1, rem1. The documentation bug of #8108
is not fixed.
jiahao added a commit that referenced this issue Jan 17, 2015
Restore old implementation of mod1 that was overwritten in
3842422

Adds test of mod1 behavior from #5570

Note: #5570 is not fixed for fld1, rem1. The documentation bug of #8108
is not fixed.
@eschnett
Copy link
Contributor

eschnett commented Dec 3, 2015

This should be fixed by #14140: mod1 was corrected by #10585, fld1 now has a consistent behaviour for negative values, and rem1 is deprecated.

@mschauer
Copy link
Contributor Author

mschauer commented Dec 3, 2015

Great

@mschauer mschauer closed this as completed Dec 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants