Functional Weave
Code in TypeScript

math.rational@2.0.0

impl/rust/add_rational.rs

587 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_from_value, rational_from_wide, rational_parts, rational_to_value};  ← rational, another function of this group · built into the same file, even by a slim install

/// a + b, reduced.
///
/// # Panics
/// Panics on a zero denominator or a result outside ±(2^53 - 1).
pub fn add_rational(a: &Rational, b: &Rational) -> Rational {
    let (an, ad) = rational_parts(a);
    let (bn, bd) = rational_parts(b);
    rational_from_wide(an * bd + bn * ad, ad * bd)
}

pub fn fune_vector(args: &[Value]) -> Value {
    rational_to_value(&add_rational(&rational_from_value(&args[0]), &rational_from_value(&args[1])))
}