Skip to content

Commit 0302b02

Browse files
split out remove_zero_from_interval
1 parent 5af2177 commit 0302b02

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/algebraic_numbers.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -623,35 +623,43 @@ impl RealAlgebraicNumber {
623623
pub fn trunc(&self) -> Self {
624624
self.to_integer_trunc().into()
625625
}
626-
pub fn checked_recip(&self) -> Option<Self> {
627-
if let Some(value) = self.to_rational() {
628-
if value.is_zero() {
629-
return None;
630-
}
631-
return Some(value.recip().into());
632-
}
626+
/// shrinks the interval till it doesn't contain zero
627+
#[must_use]
628+
fn remove_zero_from_interval(&mut self) -> Option<(Sign, IntervalShrinker)> {
633629
let sign = match self.cmp_with_zero() {
634-
Ordering::Equal => unreachable!("already checked for zero"),
630+
Ordering::Equal => return None,
635631
Ordering::Less => Sign::Negative,
636632
Ordering::Greater => Sign::Positive,
637633
};
638-
let mut value = self.clone();
639634
match sign {
640635
Sign::Negative => {
641-
if value.interval().upper_bound_numer().is_positive() {
642-
value.data.interval.set_upper_bound_to_zero();
636+
if self.interval().upper_bound_numer().is_positive() {
637+
self.data.interval.set_upper_bound_to_zero();
643638
}
644639
}
645640
Sign::Positive => {
646-
if value.interval().lower_bound_numer().is_negative() {
647-
value.data.interval.set_lower_bound_to_zero();
641+
if self.interval().lower_bound_numer().is_negative() {
642+
self.data.interval.set_lower_bound_to_zero();
648643
}
649644
}
650645
}
651-
let mut interval_shrinker = value.interval_shrinker();
646+
let mut interval_shrinker = self.interval_shrinker();
652647
while interval_shrinker.interval.contains_zero() {
653648
interval_shrinker.shrink();
654649
}
650+
Some((sign, interval_shrinker))
651+
}
652+
pub fn checked_recip(&self) -> Option<Self> {
653+
if let Some(value) = self.to_rational() {
654+
if value.is_zero() {
655+
return None;
656+
}
657+
return Some(value.recip().into());
658+
}
659+
let mut value = self.clone();
660+
value
661+
.remove_zero_from_interval()
662+
.expect("known to be non-zero");
655663
let RealAlgebraicNumberData {
656664
minimal_polynomial,
657665
interval,

0 commit comments

Comments
 (0)