Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 766e21c

Browse files
committed
Remove lossy casting in logspace
Currently `logspace` does a lossy cast from `F::Int` to `usize`. This could be problematic in the rare cases that this is called with a step count exceeding what is representable in `usize`. Resolve this by instead adding bounds so the float's integer type itself can be iterated.
1 parent 186f864 commit 766e21c

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

crates/libm-test/src/gen/domain_logspace.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! A generator that produces logarithmically spaced values within domain bounds.
22
3+
use std::ops::RangeInclusive;
4+
35
use libm::support::{IntTy, MinInt};
46

57
use crate::domain::HasDomain;
@@ -34,6 +36,7 @@ pub fn get_test_cases<Op>(_ctx: &CheckCtx) -> impl Iterator<Item = (Op::FTy,)>
3436
where
3537
Op: MathOp + HasDomain<Op::FTy>,
3638
IntTy<Op::FTy>: TryFrom<usize>,
39+
RangeInclusive<IntTy<Op::FTy>>: Iterator,
3740
{
3841
let domain = Op::DOMAIN;
3942
let start = domain.range_start();

crates/libm-test/src/num.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Helpful numeric operations.
22
33
use std::cmp::min;
4+
use std::ops::RangeInclusive;
45

5-
use libm::support::{CastInto, Float};
6+
use libm::support::Float;
67

78
use crate::{Int, MinInt};
89

@@ -214,7 +215,10 @@ fn as_ulp_steps<F: Float>(x: F) -> Option<F::SignedInt> {
214215
/// to logarithmic spacing of their values.
215216
///
216217
/// Note that this tends to skip negative zero, so that needs to be checked explicitly.
217-
pub fn logspace<F: FloatExt>(start: F, end: F, steps: F::Int) -> impl Iterator<Item = F> {
218+
pub fn logspace<F: FloatExt>(start: F, end: F, steps: F::Int) -> impl Iterator<Item = F>
219+
where
220+
RangeInclusive<F::Int>: Iterator,
221+
{
218222
assert!(!start.is_nan());
219223
assert!(!end.is_nan());
220224
assert!(end >= start);
@@ -225,7 +229,7 @@ pub fn logspace<F: FloatExt>(start: F, end: F, steps: F::Int) -> impl Iterator<I
225229
steps = steps.min(between); // At maximum, one step per ULP
226230

227231
let mut x = start;
228-
(0..=steps.cast()).map(move |_| {
232+
(F::Int::ZERO..=steps).map(move |_| {
229233
let ret = x;
230234
x = x.n_up(spacing);
231235
ret

0 commit comments

Comments
 (0)