Skip to content

Commit 11ffd33

Browse files
benruijlclaude
andcommitted
Add set_precision to FloatLike
Iterative methods that correct their own roundoff, such as Newton's method, must not accumulate the precision loss that Float tracks on cancellation. set_precision restores the working precision of a value so that only its value carries over to the next iteration. Balls round their endpoints outward and stay certified; fixed-precision types ignore the call. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent f2a4de4 commit 11ffd33

10 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎src/domains/dual.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,13 @@ macro_rules! create_hyperdual_from_components {
920920
self.values[0].get_precision()
921921
}
922922

923+
#[inline(always)]
924+
fn set_precision(&mut self, precision: u32) {
925+
for value in &mut self.values {
926+
value.set_precision(precision);
927+
}
928+
}
929+
923930
#[inline(always)]
924931
fn get_epsilon(&self) -> f64 {
925932
self.values[0].get_epsilon()
@@ -2004,6 +2011,13 @@ impl<T: FloatLike> FloatLike for HyperDual<T> {
20042011
self.values[0].get_precision()
20052012
}
20062013

2014+
#[inline(always)]
2015+
fn set_precision(&mut self, precision: u32) {
2016+
for value in &mut self.values {
2017+
value.set_precision(precision);
2018+
}
2019+
}
2020+
20072021
#[inline(always)]
20082022
fn get_epsilon(&self) -> f64 {
20092023
self.values[0].get_epsilon()

‎src/domains/float.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ pub trait FloatLike:
110110

111111
/// Get the number of precise binary digits.
112112
fn get_precision(&self) -> u32;
113+
/// Set the working precision to `precision` binary digits. Types that loosely track
114+
/// their precision discard the tracked loss, whereas certified enclosures stay valid.
115+
/// The value is rounded when the precision is lowered. Types with a fixed precision
116+
/// ignore this call.
117+
fn set_precision(&mut self, precision: u32);
113118
fn get_epsilon(&self) -> f64;
114119
/// Return true iff the precision is fixed, or false
115120
/// if the precision is changed dynamically.

‎src/domains/float/complex.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,12 @@ impl<T: FloatLike> FloatLike for Complex<T> {
803803
self.im.set_from(&other.im);
804804
}
805805

806+
#[inline]
807+
fn set_precision(&mut self, precision: u32) {
808+
self.re.set_precision(precision);
809+
self.im.set_precision(precision);
810+
}
811+
806812
#[inline]
807813
fn mul_add(&self, a: &Self, b: &Self) -> Self {
808814
self.clone() * a + b

‎src/domains/float/double.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ impl FloatLike for DoubleFloat {
232232
106
233233
}
234234

235+
#[inline(always)]
236+
fn set_precision(&mut self, _precision: u32) {}
237+
235238
#[inline(always)]
236239
fn get_epsilon(&self) -> f64 {
237240
f64::EPSILON * f64::EPSILON / 2.0

‎src/domains/float/error.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,14 @@ impl<T: RealLike> FloatLike for ErrorPropagatingFloat<T> {
513513
self.value.get_precision()
514514
}
515515

516+
fn set_precision(&mut self, precision: u32) {
517+
self.value.set_precision(precision);
518+
// reset the tracked error to the roundoff of the value
519+
let scale = self.value.to_f64().abs();
520+
self.abs_err =
521+
if scale == 0. { 1. } else { scale } * 2f64.powf(-(self.value.get_precision() as f64));
522+
}
523+
516524
fn get_epsilon(&self) -> f64 {
517525
2.0f64.powi(-(self.value.get_precision() as i32))
518526
}

‎src/domains/float/interval.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,16 @@ impl FloatLike for RealBall {
534534
self.center.get_precision().min(self.radius.get_precision())
535535
}
536536

537+
fn set_precision(&mut self, precision: u32) {
538+
if self.center.is_finite() && self.radius.is_finite() {
539+
// Round the endpoints outward so that the enclosure stays certified.
540+
*self = Self::from_outward_bounds(self.lower_bound(), self.upper_bound(), precision);
541+
} else {
542+
self.center.set_precision(precision);
543+
self.radius.set_precision(precision);
544+
}
545+
}
546+
537547
fn get_epsilon(&self) -> f64 {
538548
2.0f64.powi(-(self.get_precision() as i32))
539549
}

‎src/domains/float/multiprecision.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,11 @@ impl FloatLike for Float {
956956
self.prec()
957957
}
958958

959+
#[inline(always)]
960+
fn set_precision(&mut self, precision: u32) {
961+
self.set_prec(precision);
962+
}
963+
959964
#[inline(always)]
960965
fn get_epsilon(&self) -> f64 {
961966
2.0f64.powi(-(self.prec() as i32))

‎src/domains/float/native.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ impl FloatLike for f64 {
8686
53
8787
}
8888

89+
#[inline(always)]
90+
fn set_precision(&mut self, _precision: u32) {}
91+
8992
#[inline(always)]
9093
fn get_epsilon(&self) -> f64 {
9194
f64::EPSILON / 2.
@@ -411,6 +414,11 @@ impl FloatLike for F64 {
411414
self.0.get_precision()
412415
}
413416

417+
#[inline(always)]
418+
fn set_precision(&mut self, precision: u32) {
419+
self.0.set_precision(precision);
420+
}
421+
414422
#[inline(always)]
415423
fn get_epsilon(&self) -> f64 {
416424
self.0.get_epsilon()

‎src/domains/float/rational.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ impl FloatLike for Rational {
6969
u32::MAX
7070
}
7171

72+
#[inline(always)]
73+
fn set_precision(&mut self, _precision: u32) {}
74+
7275
#[inline(always)]
7376
fn get_epsilon(&self) -> f64 {
7477
0.

‎src/domains/float/simd.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ macro_rules! simd_impl {
6868
53
6969
}
7070

71+
#[inline(always)]
72+
fn set_precision(&mut self, _precision: u32) {}
73+
7174
#[inline(always)]
7275
fn get_epsilon(&self) -> f64 {
7376
f64::EPSILON / 2.

0 commit comments

Comments
 (0)