Skip to content

Conversation

@felixrehren
Copy link
Contributor

@felixrehren felixrehren commented Feb 23, 2017

Cf. #20707

Add docstring

rem(x, T)
mod(x, T)
%(x, T)

For an integer type T, finds z::T such that xz (mod n), where n is the
cardinality of T and z is restricted to the interval of values representable by T.

julia> 129 % Int8
-127

Could be briefer: strictly speaking, "and z is restricted to ..." is already implied by z::T ?

(Sorry for cruft in commit history. Still trying to figure this out. I guess it will be eliminated by squashing?)

@kshyatt kshyatt added the docs This change adds or pertains to documentation label Feb 23, 2017
@StefanKarpinski
Copy link
Member

Pkg test error is unrelated.

base/int.jl Outdated
%(x, T)
For an integer type `T`, finds `z::T` such that `x` ≡ `z` (mod n), where n is the
cardinality of `T` and `z` is restricted to the interval of values representable by `T`.
Copy link
Contributor

Choose a reason for hiding this comment

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

is cardinality of a type a term we expect people to understand without any further elaboration?

Copy link
Member

Choose a reason for hiding this comment

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

Would it be clearer to explain this by saying that x % T returns y :: T such that xy in the native modular arithmetic of the type T? For example, x % Int8 returns y :: Int8 such that xy (mod 256) – i.e. one of the representatives from -128 through 127.

Copy link
Member

@Sacha0 Sacha0 Feb 24, 2017

Choose a reason for hiding this comment

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

Given that both terminologies ("cardinality of a type", "a type's native modular arithmetic") are potentially challenging, perhaps describing this behavior in both ways (to help readers tripped up by one form or the other) and including an example (e.g. that above) would be worthwhile? E.g., "For an integer of type T, finds [... present version in the PR ...]. In other words, finds z::T such that x ≡ z in the native modular arithmetic of type T. For example, x & Int8 returns [...]"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair points. I'm going to try to simplify it --

base/int.jl Outdated
For an integer type `T`, finds `z::T` such that `x` ≡ `z` (mod n), where n is the
cardinality of `T` and `z` is restricted to the interval of values representable by `T`.
For an integer type `T`, finds `y::T` such that `x` ≡ `y` (mod n), where n is the
number of elements representable by `T`, and `y` is in `[typemin(T),typemax(T)]`.
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps "number of values representable by T,"? ("elements" seems the right choice when referring to a set, e.g. "number of elements in the set of values of T"; values seems more appropriate coupled to "representable by T".)

Perhaps ", and y is restricted to the values representable by T"? (y not merely being restricted to the interval [typemin(T), typemax(T)].)

Perhaps collapsing those phrases is possible: "where y is restricted to the values representable by T, and n is the number of such values" or so? Best!

Copy link
Contributor Author

@felixrehren felixrehren Feb 25, 2017

Choose a reason for hiding this comment

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

Instead of "elements" or "values", I suppose we could just say "integers"?

base/int.jl Outdated
mod(x, T)
%(x, T)
For an integer type `T`, finds `y::T` such that `x` ≡ `y` (mod n), where n is the
Copy link
Member

Choose a reason for hiding this comment

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

I think it should be "find" rather than "finds," since we typically try to stick to the imperative when describing functionality in docstrings.

@felixrehren
Copy link
Contributor Author

It occurred to me that x % T is only defined for x::Integer, in contrast to x % y, and thought this should be part of the documentation.

There's an exception: x % T is also defined for x::Char, but is the same as T(x) in this case. I thought this was not worth documenting.

@Sacha0
Copy link
Member

Sacha0 commented Feb 25, 2017

It occurred to me that x % T is only defined for x::Integer, in contrast to x % y, and thought this should be part of the documentation.

There's an exception: x % T is also defined for x::Char, but is the same as T(x) in this case. I thought this was not worth documenting.

Bool-T where T is an integer type and Integer-Type{Bool} methods also exist per methods(rem)?

%(x::Integer, T<:Integer)
Find `y::T` such that `x` ≡ `y` (mod n), where n is the number of integers representable
in `T`, and `y` is an integer in `[typemin(T),typemax(T)]`.
Copy link
Member

Choose a reason for hiding this comment

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

in -> by?

Copy link
Contributor Author

@felixrehren felixrehren Feb 26, 2017

Choose a reason for hiding this comment

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

Changed by -> in on purpose. Felt like it sounded more natural. "Represented in" suggests a collection. "Represented by" suggests a mapping to me (like lie algebra element -> matrix operator). I thought Int8 etc. is more like a collection than like a mapping. Is there a convention on this in the docs? Happy to change back

Copy link
Member

Choose a reason for hiding this comment

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

Cheers either way; I lack strong feelings. (by sounds more natural to me, hence the suggestion.) Best!

@felixrehren
Copy link
Contributor Author

@Sacha0 Yes! b % IntX will always map b::Bool to 0 or 1, same as convert. On the other hand, x % Bool maps x to whether x is odd. I can imagine they would go away if Bool stops being a number, for example. Should these be additionally documented?

@StefanKarpinski
Copy link
Member

We should consider whether the x % Char methods should be deprecated. I think we should probably just have conversion to Char instead.

@Sacha0
Copy link
Member

Sacha0 commented Feb 26, 2017

b % IntX will always map b::Bool to 0 or 1, same as convert. On the other hand, x % Bool maps x to whether x is odd.

We should consider whether the x % Char methods should be deprecated. I think we should probably just have conversion to Char instead.

Perhaps the Bool methods should go away as well? isodd(x) seems much clearer than x % Bool. Best!

end

"""
rem(x::Integer, T<:Integer)
Copy link
Member

Choose a reason for hiding this comment

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

I think this notation is nonstandard with regards to the rest of the Base docstrings. Also note that <:Integer isn't entirely accurate, since you can't do something like 1 % BigInt.

@StefanKarpinski
Copy link
Member

I feel like as long as Bool is an integer type having x % Bool makes sense, but if we make it a non-integer type then we should probably remove those methods.

%(x::Integer, T<:Integer)
Find `y::T` such that `x` ≡ `y` (mod n), where n is the number of integers representable
in `T`, and `y` is an integer in `[typemin(T),typemax(T)]`.
Copy link
Member

Choose a reason for hiding this comment

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

With this definition would it make sense to define (extend) this operation for T == BigInt ? (but somehow, till now I understood x % T as a cheap bit-level cast operation, which would not work for BigInt...)

Copy link
Member

Choose a reason for hiding this comment

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

Sure, it would make sense semantically. Since nothing is a cheap bit-level operation on BigInts, there's no harm. Since the range of BigInts is unbounded, n % BigInt is equivalent to a convert. In the other direction, b % Int can be done by doing a single word load.

@StefanKarpinski StefanKarpinski merged commit 407995a into JuliaLang:master Mar 28, 2017
@tkelman
Copy link
Contributor

tkelman commented Mar 28, 2017

@ararslan's comment was not addressed

@StefanKarpinski
Copy link
Member

We have a new policy of merging documentation PRs quickly and then iterating on them. I don't even see what comment you are talking about, so if it still needs fixing, if someone who sees the problem could take care of it, that would be lovely. Meanwhile, this produces this warning during a build:

WARNING: replacing docs for 'Base.rem :: Union{}' in module 'Base'.

@tkelman
Copy link
Contributor

tkelman commented Mar 28, 2017

Thought that was a temporary experiment for Docathon.

Scroll up.

I think this notation is nonstandard with regards to the rest of the Base docstrings.

@iamnapo iamnapo mentioned this pull request Apr 5, 2017
@felixrehren felixrehren deleted the fr/docrem branch May 15, 2017 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs This change adds or pertains to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants