Functional Weave
Code in TypeScript

math.rational@2.0.0

impl/rust/compare_rational.rs

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

/// -1, 0 or 1. Exact: cross-multiplied in i128, never through a float.
///
/// # Panics
/// Panics on a zero denominator or a part outside ±(2^53 - 1).
pub fn compare_rational(a: &Rational, b: &Rational) -> i64 {
    let (an, ad) = rational_parts(a);
    let (bn, bd) = rational_parts(b);
    match (an * bd).cmp(&(bn * ad)) {
        std::cmp::Ordering::Less => -1,
        std::cmp::Ordering::Equal => 0,
        std::cmp::Ordering::Greater => 1,
    }
}

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