Skip to content

N.pow(M) returns zero, does not overflow for unsigned N and both N and M even (sometimes) #34913

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
cjsut opened this issue Jul 19, 2016 · 8 comments

Comments

@cjsut
Copy link
Contributor

cjsut commented Jul 19, 2016

Today I learned that eight to the power of eight is zero. Who'da thunk?

Incorrect:
2u8.pow(500) ➡️ 0
8u8.pow(8) ➡️ 0
4u32.pow(1000) ➡️ 0

Correct:
8u8.pow(7) ➡️ overflow ✅
8i8.pow(8) ➡️ overflow ✅
2u8.pow(9) ➡️ overflow ✅

Oddly enough, this one breaks the pattern:
2u8.pow(10) ➡️ overflow ✅

Haven't bothered with further cases; at this point it's probably best to just look at the implementation and see what's going on.

@eefriedman
Copy link
Contributor

#28248 was supposed to fix this; looks like it missed the unsigned version?

@cjsut
Copy link
Contributor Author

cjsut commented Jul 19, 2016

This playpen says that 8u8.pow(8) is 0 on nightly.

@eefriedman
Copy link
Contributor

I meant that #28248 fixed this for pow() on signed integers, but not on unsigned integers; it looks like the implementation is copy-pasted for some reason.

@porglezomp
Copy link
Contributor

Could I take a stab at this?

@eefriedman
Copy link
Contributor

Sure; feel free to ask if you have questions.

@porglezomp
Copy link
Contributor

Is overflow checking off in the libcore tests? It'd be nice to be able to write some standard #[should_panic] tests for overflow, but they seem to not panic even on cases that should right now.

@eefriedman
Copy link
Contributor

eefriedman commented Jul 20, 2016

Overflow checking in unit tests (#[test]) reflects whether the whole compiler was built in debug or release mode.

If you need to specify the overflow mode, you can use a separate test which explicitly specifies -C debug-assertions, like #28248.

porglezomp added a commit to porglezomp/rust that referenced this issue Aug 7, 2016
The pow() method for unsigned integers produced 0 instead of trapping
overflow for certain inputs. Calls such as 2u32.pow(1024) produced 0
when they should trap an overflow. This also adds tests for the
correctly handling overflow in unsigned pow().

For issue number rust-lang#34913
bors added a commit that referenced this issue Sep 20, 2016
Fix overflow checking in unsigned pow()

The pow() method for unsigned integers produced 0 instead of trapping overflow for certain inputs. Calls such as 2u32.pow(1024) produced 0 when they should trap an overflow. This also adds tests for the correctly handling overflow in unsigned pow().

This was previously fixed for signed integers in #28248, but it seems unsigned integers got missed that time.

For issue number #34913
@Mark-Simulacrum
Copy link
Member

This has since been fixed, in #34942, which it looks like also added a test, so closing.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants