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

better operator definitions for largest unsigned integer #180

Closed
JeffBezanson opened this issue Aug 17, 2011 · 10 comments
Closed

better operator definitions for largest unsigned integer #180

JeffBezanson opened this issue Aug 17, 2011 · 10 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@JeffBezanson
Copy link
Member

Currently we get:

julia> div(typemax(Uint64),1)
-1
julia> div(typemax(Uint64),2)
0
julia> typemax(Uint64)-1
-2

For these cases it would probably be better to give the right answer if representable and an error otherwise.
I believe the only problematic cases involve combining Uint64 with signed integers.

@StefanKarpinski
Copy link
Member

Maybe we should more generally treat unsigned integers as slightly less arithmetic types than signed integers. They really have limited applicability, so it seems acceptable to limit their functionality somewhat.

@JeffBezanson
Copy link
Member Author

LLVM has +, -, and * that can detect overflow efficiently.

@StefanKarpinski
Copy link
Member

Oh, that's pretty awesome. Should we throw an error on overflow then?

@JeffBezanson
Copy link
Member Author

We need to decide whether the promotion type should switch to Uint64 or stay as Int64, then add logic to give an error if the result would not be representable. Unfortunately I don't think this is the same as detecting overflow. If n = uint64(typemax(Int64))+uint64(2) then n-1 is not representable as Int64 but n-2 is, even though n-2 causes overflow and n-1 does not. At least some kind of additional logic is needed.

Comparisons also need a new approach. >(x::Uint64,y::Int) should first return true if y is negative, etc.

@StefanKarpinski
Copy link
Member

I feel like we should punt on this for v1.0, although of course this isn't marked as a v1.0 issue.

@JeffBezanson
Copy link
Member Author

Yeah, posting "wrong answers" like the ones I wrote always seems scary, but the fact is we always get the right answer as long as the Uint64s involved are <= typemax(Int64), which is certainly the common case by far.

@JeffBezanson
Copy link
Member Author

Commit 2175a8b made these operations promote to Uint64, which fixes the examples listed here. Commit 6b7ede3 fixed comparisons with Uint64. So I think we're OK now.

@timholy
Copy link
Member

timholy commented Mar 17, 2012

Here's something a little strange:
julia> x = convert(Uint8,2)
0x02

julia> -x
0xfffffffe

julia> x = zeros(Uint8,5)
[0x00, 0x00, 0x00, 0x00, 0x00]

julia> for i = 1:5; x[i] = i-1; end

julia> -x
[0x00, 0xff, 0xfe, 0xfd, 0xfc]

Note that a scalar gets promoted to Uint64, while the vector of Uint8 does not.

@StefanKarpinski
Copy link
Member

Actually the promotion happens both times, but it gets converted to Uint8 when you assign into the Uint8 array. This behavior was the end-result of many experiments and discussions. The basic idea is that going through "large" types like Int and Uint and then converting at the end to whatever storage type you want almost always gives you the right answer in the end. This works really nicely for signed arithmetic but can be a little weirder for unsigned.

@timholy
Copy link
Member

timholy commented Mar 18, 2012

OK, thanks. Just thought I'd bring it to your attention in case it was a
surprise.

--Tim

StefanKarpinski pushed a commit that referenced this issue Feb 8, 2018
Keno pushed a commit that referenced this issue Oct 9, 2023
IanButterworth pushed a commit that referenced this issue Jul 24, 2024
Stdlib: Tar
URL: https://github.com/JuliaIO/Tar.jl.git
Stdlib branch: master
Julia branch: master
Old commit: 81888a3
New commit: 1114260
Julia version: 1.12.0-DEV
Tar version: 1.10.0(Does not match)
Bump invoked by: @StefanKarpinski
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaIO/Tar.jl@81888a3...1114260

```
$ git log --oneline 81888a3..1114260
1114260 Accept other string types for all string arguments (fix #179) (#180)
a2e39d6 Bump julia-actions/cache from 1 to 2 (#178)
152d12e Bump julia-actions/setup-julia from 1 to 2 (#177)
5012536 Fix Codecov (#176)
9b5460b Add `public` declarations using `eval` (#175)
4e9d73a Add docstring for Tar module (#173)
38a4bf4 Bump codecov/codecov-action from 3 to 4 (#172)
166deb3 [CI] Switch to `julia-actions/cache` (#171)
d0085d8 Hardcode doc edit backlink (#164)
7e83ed7 [NFC] fix some wonky formatting (#168)
6269b5b Bump actions/checkout from 3 to 4 (#163)
```

Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
lazarusA pushed a commit to lazarusA/julia that referenced this issue Jul 24, 2024
Stdlib: Tar
URL: https://github.com/JuliaIO/Tar.jl.git
Stdlib branch: master
Julia branch: master
Old commit: 81888a3
New commit: 1114260
Julia version: 1.12.0-DEV
Tar version: 1.10.0(Does not match)
Bump invoked by: @StefanKarpinski
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaIO/Tar.jl@81888a3...1114260

```
$ git log --oneline 81888a3..1114260
1114260 Accept other string types for all string arguments (fix JuliaLang#179) (JuliaLang#180)
a2e39d6 Bump julia-actions/cache from 1 to 2 (JuliaLang#178)
152d12e Bump julia-actions/setup-julia from 1 to 2 (JuliaLang#177)
5012536 Fix Codecov (JuliaLang#176)
9b5460b Add `public` declarations using `eval` (JuliaLang#175)
4e9d73a Add docstring for Tar module (JuliaLang#173)
38a4bf4 Bump codecov/codecov-action from 3 to 4 (JuliaLang#172)
166deb3 [CI] Switch to `julia-actions/cache` (JuliaLang#171)
d0085d8 Hardcode doc edit backlink (JuliaLang#164)
7e83ed7 [NFC] fix some wonky formatting (JuliaLang#168)
6269b5b Bump actions/checkout from 3 to 4 (JuliaLang#163)
```

Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants