Add "integer square root" method to integer primitive types by Federic...
source link: https://github.com/rust-lang/rust/pull/116176
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Conversation
Contributor
For every suffix N
among 8
, 16
, 32
, 64
, 128
and size
, this PR adds the methods
const fn uN::isqrt() -> uN;
const fn iN::isqrt() -> iN;
const fn iN::checked_isqrt() -> Option<iN>;
to compute the integer square root, addressing issue #89273.
The implementation is based on the base 2 digit-by-digit algorithm on Wikipedia, which after some benchmarking has proved to be faster than both binary search and Heron's/Newton's method. I haven't had the time to understand and port this code based on lookup tables instead, but I'm not sure whether it's worth complicating such a function this much for relatively little benefit.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK