Claude Code for fractions: Exact Rational Arithmetic in Python — Claude Skills 360 Blog
Blog / AI / Claude Code for fractions: Exact Rational Arithmetic in Python
AI

Claude Code for fractions: Exact Rational Arithmetic in Python

Published: August 13, 2028
Read time: 5 min read
By: Claude Skills 360

Python’s fractions module provides exact rational arithmetic via Fraction. from fractions import Fraction. Construction: Fraction(3, 4) → 3/4; Fraction("3/4") → 3/4; Fraction(0.75) → 3/4 (exact); Fraction("0.75") → 3/4; Fraction(int) → exact integer as Fraction. numerator/denominator: Fraction(2, 6).numerator = 1, .denominator = 3 (always auto-reduces). limit_denominator: Fraction(math.pi).limit_denominator(100) → 22/7; .limit_denominator(10000) → 355/113 — approximates irrational floats as simple fractions. Arithmetic: +, -, *, /, ** all return exact Fractions (no rounding). Comparison: Fraction(1, 3) > Fraction(1, 4) — exact comparison. Mixed: Fraction(1, 3) + 1 → 4/3; Fraction(1, 3) + 0.1 → float (mixed with float loses exactness). from_float: Fraction.from_float(0.1) — exact binary representation (3602879701896397 / 36028797018963968). from_decimal: Fraction.from_decimal(Decimal("0.1")) → 1/10 (exact). Conversion: float(Fraction(1, 3)), int(Fraction(7, 3)) = 2 (truncates). math.floor(Fraction(7, 3)) = 2. Claude Code generates music interval calculators, aspect ratio normalizers, probability fraction reducers, and unit conversion chains.

CLAUDE.md for fractions

## fractions Stack
- Stdlib: from fractions import Fraction
- Create:  Fraction(3, 4)  Fraction("3/4")  Fraction("0.75")
- Reduce:  Fraction(6, 8) → 3/4 (always auto-reduces)
- Approx:  Fraction(math.pi).limit_denominator(100) → 22/7
- Exact:   Fraction(1,3) + Fraction(1,6) → Fraction(1,2)  (no float error)
- Mixed:   avoid mixing Fraction + float → loses exactness

fractions Rational Arithmetic Pipeline

# app/fracutil.py — fractions, ratios, probability, music, aspect, unit
from __future__ import annotations

import math
from fractions import Fraction
from dataclasses import dataclass
from decimal import Decimal
from typing import NamedTuple


# ─────────────────────────────────────────────────────────────────────────────
# 1. Fraction construction helpers
# ─────────────────────────────────────────────────────────────────────────────

def frac(numerator: int, denominator: int) -> Fraction:
    """
    Create a Fraction and return its reduced form.

    Example:
        frac(6, 8)   # Fraction(3, 4)
        frac(10, 3)  # Fraction(10, 3)
    """
    return Fraction(numerator, denominator)


def from_float(x: float) -> Fraction:
    """
    Exact Fraction representation of a float.
    Note: Fraction.from_float(0.1) ≠ 1/10 due to binary representation.

    Example:
        from_float(0.5)   # Fraction(1, 2)
        from_float(0.1)   # Fraction(3602879701896397, 36028797018963968)
    """
    return Fraction.from_float(x)


def from_decimal(d: Decimal) -> Fraction:
    """
    Exact Fraction from a Decimal string (e.g., from user input).

    Example:
        from_decimal(Decimal("0.1"))  # Fraction(1, 10)
        from_decimal(Decimal("1.25")) # Fraction(5, 4)
    """
    return Fraction.from_decimal(d)


def best_rational(x: float, max_denominator: int = 1000) -> Fraction:
    """
    Find the simplest rational approximation to x with denominator ≤ max_denominator.

    Example:
        best_rational(math.pi, 100)   # Fraction(22, 7)
        best_rational(math.pi, 10000) # Fraction(355, 113)
        best_rational(0.333)          # Fraction(1, 3)
    """
    return Fraction(x).limit_denominator(max_denominator)


def parse_fraction(s: str) -> Fraction:
    """
    Parse a fraction from '3/4', '0.75', '75%', or integer strings.

    Example:
        parse_fraction("3/4")  # Fraction(3, 4)
        parse_fraction("75%")  # Fraction(3, 4)
        parse_fraction("0.75") # Fraction(3, 4)
    """
    s = s.strip()
    if s.endswith("%"):
        return Fraction(s[:-1]) / 100
    return Fraction(s)


# ─────────────────────────────────────────────────────────────────────────────
# 2. Fraction arithmetic and display
# ─────────────────────────────────────────────────────────────────────────────

def fraction_add(*fracs: Fraction | int) -> Fraction:
    """
    Sum multiple fractions exactly.

    Example:
        fraction_add(Fraction(1, 3), Fraction(1, 6), Fraction(1, 2))  # Fraction(1, 1)
    """
    result = Fraction(0)
    for f in fracs:
        result += Fraction(f)
    return result


def fraction_mul(*fracs: Fraction | int) -> Fraction:
    """
    Multiply multiple fractions exactly.

    Example:
        fraction_mul(Fraction(2, 3), Fraction(3, 4), 2)  # Fraction(1, 1)
    """
    result = Fraction(1)
    for f in fracs:
        result *= Fraction(f)
    return result


def to_mixed_number(f: Fraction) -> tuple[int, Fraction]:
    """
    Return (whole, fractional) representation of f.
    E.g., 7/3 → (2, 1/3); -7/3 → (-2, -1/3).

    Example:
        whole, frac_part = to_mixed_number(Fraction(7, 3))
        print(f"{whole} and {frac_part}")  # "2 and 1/3"
    """
    whole = int(f)
    frac_part = f - whole
    return whole, frac_part


def format_fraction(f: Fraction, mixed: bool = False) -> str:
    """
    Format a Fraction as '3/4' or as a mixed number '2 1/3'.

    Example:
        format_fraction(Fraction(3, 4))           # "3/4"
        format_fraction(Fraction(7, 3), mixed=True) # "2 1/3"
        format_fraction(Fraction(4, 1))           # "4"
    """
    if f.denominator == 1:
        return str(f.numerator)
    if not mixed:
        return f"{f.numerator}/{f.denominator}"
    whole, frac_part = to_mixed_number(f)
    if whole == 0:
        return f"{frac_part.numerator}/{frac_part.denominator}"
    if frac_part == 0:
        return str(whole)
    return f"{whole} {abs(frac_part.numerator)}/{frac_part.denominator}"


def as_percentage(f: Fraction, decimals: int = 2) -> str:
    """
    Return a Fraction as a percentage string.

    Example:
        as_percentage(Fraction(1, 3))   # "33.33%"
        as_percentage(Fraction(1, 4))   # "25.00%"
    """
    return f"{float(f) * 100:.{decimals}f}%"


# ─────────────────────────────────────────────────────────────────────────────
# 3. Ratio and proportion
# ─────────────────────────────────────────────────────────────────────────────

def simplify_ratio(a: int, b: int) -> tuple[int, int]:
    """
    Simplify an integer ratio a:b to its lowest terms.

    Example:
        simplify_ratio(1920, 1080)  # (16, 9)
        simplify_ratio(6, 9)        # (2, 3)
    """
    g = math.gcd(a, b)
    return a // g, b // g


def aspect_ratio(width: int, height: int) -> str:
    """
    Return the simplest aspect ratio string e.g. '16:9'.

    Example:
        aspect_ratio(1920, 1080)  # "16:9"
        aspect_ratio(800, 600)    # "4:3"
    """
    w, h = simplify_ratio(width, height)
    return f"{w}:{h}"


def distribute_proportionally(total: int, weights: list[int]) -> list[int]:
    """
    Distribute total integer units according to weights, using exact fractions.
    Guarantees the parts sum exactly to total.

    Example:
        distribute_proportionally(100, [1, 2, 3])  # [17, 33, 50]
    """
    if not weights or total == 0:
        return [0] * len(weights)
    weight_sum = sum(weights)
    fracs = [Fraction(w, weight_sum) * total for w in weights]
    # Use floor and distribute remainder by largest fractional parts
    floors = [int(math.floor(f)) for f in fracs]
    remainders = sorted(
        range(len(fracs)),
        key=lambda i: -(fracs[i] - floors[i]),
    )
    deficit = total - sum(floors)
    for i in remainders[:deficit]:
        floors[i] += 1
    return floors


# ─────────────────────────────────────────────────────────────────────────────
# 4. Probability fractions
# ─────────────────────────────────────────────────────────────────────────────

def reduce_probability(favorable: int, total: int) -> Fraction:
    """
    Represent a probability as an exact reduced fraction.

    Example:
        reduce_probability(3, 6)       # Fraction(1, 2)
        reduce_probability(13, 52)     # Fraction(1, 4)
    """
    return Fraction(favorable, total)


def combine_independent(*probs: Fraction) -> Fraction:
    """
    P(A ∩ B ∩ ...) for independent events.

    Example:
        combine_independent(Fraction(1,2), Fraction(1,6))  # Fraction(1, 12)
    """
    return fraction_mul(*probs)


def at_least_one(*probs: Fraction) -> Fraction:
    """
    P(at least one occurs) = 1 - P(none occur), for independent events.

    Example:
        at_least_one(Fraction(1,6), Fraction(1,6))  # Fraction(11, 36)
    """
    none = fraction_mul(*(1 - p for p in probs))
    return Fraction(1) - none


# ─────────────────────────────────────────────────────────────────────────────
# 5. Music interval ratios
# ─────────────────────────────────────────────────────────────────────────────

# Just intonation intervals (pure ratios, no equal temperament)
INTERVALS: dict[str, Fraction] = {
    "unison":         Fraction(1, 1),
    "minor_second":   Fraction(16, 15),
    "major_second":   Fraction(9, 8),
    "minor_third":    Fraction(6, 5),
    "major_third":    Fraction(5, 4),
    "perfect_fourth": Fraction(4, 3),
    "tritone":        Fraction(45, 32),
    "perfect_fifth":  Fraction(3, 2),
    "minor_sixth":    Fraction(8, 5),
    "major_sixth":    Fraction(5, 3),
    "minor_seventh":  Fraction(16, 9),
    "major_seventh":  Fraction(15, 8),
    "octave":         Fraction(2, 1),
}


def interval_ratio(interval: str) -> Fraction:
    """
    Return the just-intonation frequency ratio for a named interval.

    Example:
        r = interval_ratio("perfect_fifth")  # Fraction(3, 2)
        print(f"Fifth = {float(r):.4f}")      # 1.5000
    """
    key = interval.lower().replace(" ", "_")
    if key not in INTERVALS:
        raise KeyError(f"Unknown interval {interval!r}. Options: {list(INTERVALS)}")
    return INTERVALS[key]


def compound_interval(a: str, b: str) -> Fraction:
    """
    Compound two named intervals (multiply their ratios).

    Example:
        compound_interval("perfect_fifth", "major_second")  # Fraction(27, 16) ≈ major sixth
    """
    return interval_ratio(a) * interval_ratio(b)


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    print("=== fractions demo ===")

    print("\n--- construction ---")
    print(f"  Fraction(6, 8)    = {Fraction(6, 8)}")
    print(f"  from_float(0.75)  = {from_float(0.75)}")
    print(f"  from_float(0.1)   = {from_float(0.1)}")   # binary rep
    print(f"  from_decimal('0.1') = {from_decimal(Decimal('0.1'))}")  # exact
    print(f"  parse_fraction('75%') = {parse_fraction('75%')}")
    print(f"  best_rational(π, 100) = {best_rational(math.pi, 100)}")
    print(f"  best_rational(π, 10000) = {best_rational(math.pi, 10000)}")

    print("\n--- exact arithmetic ---")
    a, b, c = Fraction(1, 3), Fraction(1, 6), Fraction(1, 2)
    print(f"  1/3 + 1/6 + 1/2 = {fraction_add(a, b, c)}")   # exactly 1
    print(f"  float approximation: {1/3 + 1/6 + 1/2}")       # may not be 1.0
    print(f"  2/3 × 3/4 × 2 = {fraction_mul(Fraction(2,3), Fraction(3,4), 2)}")

    print("\n--- mixed number / display ---")
    f = Fraction(7, 3)
    print(f"  format {f}{format_fraction(f)}")
    print(f"  as mixed: {format_fraction(f, mixed=True)}")
    print(f"  as pct: {as_percentage(Fraction(1, 3))}")

    print("\n--- simplify_ratio / aspect_ratio ---")
    print(f"  1920×1080 → {aspect_ratio(1920, 1080)}")
    print(f"  2560×1600 → {aspect_ratio(2560, 1600)}")
    print(f"  simplify(6, 9) → {simplify_ratio(6, 9)}")

    print("\n--- distribute_proportionally ---")
    parts = distribute_proportionally(100, [1, 2, 3])
    print(f"  [1,2,3] of 100 → {parts}  sum={sum(parts)}")
    parts2 = distribute_proportionally(7, [1, 1, 1])
    print(f"  [1,1,1] of 7   → {parts2}  sum={sum(parts2)}")

    print("\n--- probability fractions ---")
    p_head     = reduce_probability(1, 2)
    p_six      = reduce_probability(1, 6)
    p_both     = combine_independent(p_head, p_six)
    p_either   = at_least_one(p_head, p_six)
    print(f"  P(head) = {p_head}")
    print(f"  P(six)  = {p_six}")
    print(f"  P(head AND six) = {p_both} = {as_percentage(p_both)}")
    print(f"  P(head OR six)  = {p_either} = {as_percentage(p_either)}")

    print("\n--- music intervals ---")
    for name in ["perfect_fifth", "major_third", "octave"]:
        r = interval_ratio(name)
        print(f"  {name:20s} = {r}  ({float(r):.5f}×)")
    fifth_second = compound_interval("perfect_fifth", "major_second")
    print(f"  fifth + second = {fifth_second}{float(fifth_second):.4f}")

    print("\n=== done ===")

For the decimal.Decimal alternative — Decimal provides arbitrary-precision fixed-point decimal arithmetic with configurable rounding modes; it is ideal for monetary amounts where you need a fixed number of decimal places and IEEE 754 decimal semantics; Fraction provides exact rational arithmetic regardless of how many decimal places the value requires — use Decimal for money and financial calculations where the number of decimal places matters and you want control over rounding policy; use Fraction for exact rational mathematics, music theory, probability, and any domain where you need to represent exact ratios that may not have finite decimal expansions. For the sympy.Rational alternative — sympy.Rational in the SymPy library (PyPI) is part of a full computer algebra system that can simplify expressions, solve equations, and differentiate symbolically; it is far more powerful but requires SymPy as a dependency; fractions.Fraction is stdlib with zero dependencies and covers all purely operational rational arithmetic needs — use sympy for symbolic computation, equation solving, and mathematical proofs; use fractions.Fraction for runtime calculations that just need exact rational numbers in application code. The Claude Skills 360 bundle includes fractions skill sets covering frac()/from_float()/from_decimal()/best_rational()/parse_fraction() construction helpers, fraction_add()/fraction_mul()/to_mixed_number()/format_fraction()/as_percentage() arithmetic and display, simplify_ratio()/aspect_ratio()/distribute_proportionally() ratio and proportion, reduce_probability()/combine_independent()/at_least_one() probability fractions, and INTERVALS/interval_ratio()/compound_interval() just-intonation music ratios. Start with the free tier to try exact rational arithmetic patterns and fractions pipeline code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free