Skip to content

Commit 1b76f99

Browse files
charlesHetterichshawntabrizibkchr
authored
Added checked_sqrt to the FixedPointNumber trait (paritytech#8238)
Adds `checked_sqrt` function to `FixedPointNumber` and renames `const fn try_sqrt` in the `implement_fixed` macro to `const fn checked_sqrt`. CLOSES: paritytech#8214 ## Integration Any projects that use the **const** function `try_sqrt` from `FixedI64`, `FixedU64`, `FixedI128`, `FixedU128` should now use `checked_sqrt`. ## Review Notes - renamed the const `try_sqrt` with `checked_sqrt` - updated tests for new name - added `checked_sqrt` to impl of `FixedPointNumber` We keep both the const version along with adding the non-const version of `checked_sqrt` so that we can now access `checked_sqrt` from associated types that implement `FixedPointNumber` without breaking all of the uses of `sqrt` that rely on it being const. --------- Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: Bastian Köcher <git@kchr.de>
1 parent f0aed8e commit 1b76f99

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

prdoc/pr_8238.prdoc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
title: Add `checked_sqrt` to the `FixedPointNumber` trait
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |-
6+
Adds the function `checked_sqrt` to the `FixedPointNumber` trait, and deprecates `const fn try_sqrt` inside
7+
of the `implement_fixed` macro in favor of a `const fn checked_sqrt` which does the same thing. This affects
8+
[`FixedI64`, `FixedU64`, `FixedI128`, `FixedU128`].
9+
10+
crates:
11+
- name: sp-arithmetic
12+
bump: major

substrate/primitives/arithmetic/src/fixed_point.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ pub trait FixedPointNumber:
140140
/// Consumes `self` and returns the inner raw value.
141141
fn into_inner(self) -> Self::Inner;
142142

143+
/// Compute the square root. If it overflows or is negative, then `None` is returned.
144+
fn checked_sqrt(self) -> Option<Self>;
145+
143146
/// Creates self from an integer number `int`.
144147
///
145148
/// Returns `Self::max` or `Self::min` if `int` exceeds accuracy.
@@ -443,6 +446,10 @@ macro_rules! implement_fixed {
443446
fn into_inner(self) -> Self::Inner {
444447
self.0
445448
}
449+
450+
fn checked_sqrt(self) -> Option<Self> {
451+
self.checked_sqrt()
452+
}
446453
}
447454

448455
impl $name {
@@ -551,15 +558,21 @@ macro_rules! implement_fixed {
551558
/// WARNING: This is a `const` function designed for convenient use at build time and
552559
/// will panic on overflow. Ensure that any inputs are sensible.
553560
pub const fn sqrt(self) -> Self {
554-
match self.try_sqrt() {
561+
match self.checked_sqrt() {
555562
Some(v) => v,
556563
None => panic!("sqrt overflow or negative input"),
557564
}
558565
}
559566

560-
/// Compute the square root, rounding as desired. If it overflows or is negative, then
561-
/// `None` is returned.
567+
#[deprecated(
568+
note = "`try_sqrt` will be removed after October 2025. Use `checked_sqrt` instead."
569+
)]
562570
pub const fn try_sqrt(self) -> Option<Self> {
571+
self.checked_sqrt()
572+
}
573+
574+
/// Compute the square root. If it overflows or is negative, then `None` is returned.
575+
pub const fn checked_sqrt(self) -> Option<Self> {
563576
if self.0 == 0 {
564577
return Some(Self(0))
565578
}
@@ -1211,9 +1224,9 @@ macro_rules! implement_fixed {
12111224
fn op_sqrt_works() {
12121225
for i in 1..1_000i64 {
12131226
let x = $name::saturating_from_rational(i, 1_000i64);
1214-
assert_eq!((x * x).try_sqrt(), Some(x));
1227+
assert_eq!((x * x).checked_sqrt(), Some(x));
12151228
let x = $name::saturating_from_rational(i, 1i64);
1216-
assert_eq!((x * x).try_sqrt(), Some(x));
1229+
assert_eq!((x * x).checked_sqrt(), Some(x));
12171230
}
12181231
}
12191232

0 commit comments

Comments
 (0)