-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
document x % T #20759
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
document x % T #20759
Conversation
|
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`. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 x ≡ y in the native modular arithmetic of the type T? For example, x % Int8 returns y :: Int8 such that x ≡ y (mod 256) – i.e. one of the representatives from -128 through 127.
There was a problem hiding this comment.
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 [...]"?
There was a problem hiding this comment.
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)]`. |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
|
It occurred to me that There's an exception: |
|
| %(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)]`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in -> by?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
|
@Sacha0 Yes! |
|
We should consider whether the |
Perhaps the |
| end | ||
|
|
||
| """ | ||
| rem(x::Integer, T<:Integer) |
There was a problem hiding this comment.
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.
|
I feel like as long as |
| %(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)]`. |
There was a problem hiding this comment.
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...)
There was a problem hiding this comment.
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.
|
@ararslan's comment was not addressed |
|
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: |
|
Thought that was a temporary experiment for Docathon. Scroll up.
|
Cf. #20707
Add docstring
Could be briefer: strictly speaking, "and
zis restricted to ..." is already implied byz::T?(Sorry for cruft in commit history. Still trying to figure this out. I guess it will be eliminated by squashing?)