Skip to content

Commit

Permalink
Merge pull request #2332 from DoTheBestToGetTheBest/better-trunc_div-…
Browse files Browse the repository at this point in the history
…docs

docs for trunc_div functio
  • Loading branch information
brentstone authored Dec 28, 2023
2 parents 66de1db + 13a6880 commit b4de06a
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion core/src/types/dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,36 @@ pub type Result<T> = std::result::Result<T, Error>;
pub struct Dec(pub I256);

impl Dec {
/// Division with truncation (TODO: better description)
/// Performs division with truncation.
///
/// This method divides `self` by `rhs` (right-hand side) and truncates the
/// result to [`POS_DECIMAL_PRECISION`] decimal places. Truncation here
/// means that any fractional part of the result that exceeds
/// [`POS_DECIMAL_PRECISION`] is discarded.
///
/// The division is performed in the following way:
/// 1. The absolute values of the numerator and denominator are used for the
/// division. 2. The result is calculated to a fixed precision defined
/// by [`POS_DECIMAL_PRECISION`]. 3. If either the numerator or
/// denominator (but not both) is negative, the result is negated. 4. If
/// the division is impossible (e.g., division by zero or overflow), `None`
/// is returned.
///
/// Example:
/// ```
///
/// let x = Dec::new(3, 1).unwrap(); // Represents 0.3
/// let y = Dec::new(2, 1).unwrap(); // Represents 0.2
/// let result = x.trunc_div(&y).unwrap();
/// assert_eq!(result, Dec::new(15, 1).unwrap()); // Result is 1.5 truncated to 1 decimal place
/// ```
///
/// # Arguments
/// * `rhs`: The right-hand side `Dec` value for the division.
///
/// # Returns
/// An `Option<Dec>` which is `Some` with the result if the division is
/// successful, or `None` if the division cannot be performed.
pub fn trunc_div(&self, rhs: &Self) -> Option<Self> {
let is_neg = self.0.is_negative() ^ rhs.0.is_negative();
let inner_uint = self.0.abs();
Expand Down

0 comments on commit b4de06a

Please sign in to comment.