Functional Weave
Code in Rust

math.rational@2.0.0

impl/rust/rational_to_integer.rs

702 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::math_rational_rational::{rational, rational_from_value};  ← rational, another function of this group · built into the same file, even by a slim install
use super::math_round_div::round_div;  ← from math.round-div ^1.0.0 · built alongside by fune

/// The integer a fraction rounds to, under an explicit rounding mode: the
/// point where an exact rate becomes minor units, so the policy is the
/// caller's to state.
///
/// # Panics
/// Panics on a zero denominator, a part outside ±(2^53 - 1), or an unknown mode.
pub fn rational_to_integer(r: &Rational, mode: &str) -> i64 {
    let n = rational(r.numerator, r.denominator);
    round_div(n.numerator, n.denominator, mode)
}

pub fn fune_vector(args: &[Value]) -> Value {
    Value::Int(rational_to_integer(&rational_from_value(&args[0]), args[1].as_str()))
}