Skip to content

Commit

Permalink
Asserting that denominator is non-zero
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Jul 25, 2024
1 parent 8fed631 commit 4a058fa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- `Fraction::new` now contains a `debug_assert!` ensuring the denominator is
non-zero.

## v0.4.0 (2024-07-22)

### Breaking Changes
Expand Down
4 changes: 2 additions & 2 deletions src/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ macro_rules! fraction {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct Fraction {
#[cfg_attr(feature = "Serde", serde(rename = "n"))]
numerator: i16,
#[cfg_attr(feature = "Serde", serde(rename = "d"))]
denominator: i16,
}

Expand Down Expand Up @@ -244,6 +242,8 @@ impl Fraction {
/// `denominator` will be limited to the absolute value of `i16::MIN`.
#[must_use]
pub fn new(numerator: i16, denominator: i16) -> Self {
debug_assert!(denominator != 0);

Self::new_maybe_reduced(numerator.max(MIN_VALUE), denominator).reduce()
}

Expand Down

0 comments on commit 4a058fa

Please sign in to comment.