Logos.Ratio (Logos v0.2.0)

Copy Markdown

A Logos ratio literal: 1/3, -22/7.

num/den are always stored already reduced to lowest terms (via Integer.gcd/2) with the sign normalized onto num (den is always positive). new/2 is the only constructor and is intentionally not guaranteed to return a %Logos.Ratio{} -- when the reduced denominator is 1, it demotes to a plain Elixir integer() instead (2/1 is just 2, not %Logos.Ratio{num: 2, den: 1}), matching Clojure's own ratio auto-demotion and keeping Logos.Form.t()'s integer and ratio cases genuinely distinct (an integer never wears ratio clothing). This means callers must treat new/2's result as integer() | t(), never assume it's always a struct.

Summary

Functions

Builds a reduced ratio from num/den, demoting to a plain integer when the reduced denominator is 1. Raises ArithmeticError for a zero denominator, matching division-by-zero semantics elsewhere in Logos.

The full printed form of a ratio: num/den.

Types

t()

@type t() :: %Logos.Ratio{den: integer(), num: integer()}

Functions

new(num, den)

@spec new(integer(), integer()) :: integer() | t()

Builds a reduced ratio from num/den, demoting to a plain integer when the reduced denominator is 1. Raises ArithmeticError for a zero denominator, matching division-by-zero semantics elsewhere in Logos.

to_string(ratio)

@spec to_string(t()) :: String.t()

The full printed form of a ratio: num/den.