Dynamic Fraction Library 1.0.0
Reference-counted arbitrary precision rational number library (MIT OR Unlicense)
Loading...
Searching...
No Matches

Version Language ![License](https://img.shields.io/badge/license-MIT%20OR%20Unlicense-green.svg) ![Platform](https://img.shields.io/badge/platform-Linux%20%7C%20Windows%20%7C%20macOS%20%7C%20MCU-lightgrey.svg) Documentation

A single-header C library implementing arbitrary precision rational numbers (fractions) with reference counting, built on top of dynamic_int.h for seamless arbitrary precision arithmetic.

Features

  • Single Header: Easy integration - just include dynamic_fraction.h
  • Arbitrary Precision: Handle fractions with arbitrarily large numerators and denominators
  • Reference Counting: Automatic memory management with copy-on-write semantics
  • Always Reduced: Fractions are automatically kept in lowest terms
  • Comprehensive API: Full set of arithmetic, comparison, and conversion functions
  • Built on dynamic_int.h: Leverages proven arbitrary precision integer library
  • Fail-Fast Error Handling: Assertions on invalid inputs and allocation failures for immediate bug detection
  • Mathematical Correctness: Proper sign normalization and fraction reduction

Quick Start

#define DF_IMPLEMENTATION
int main() {
// Create fractions
df_frac a = df_from_ints(3, 4); // 3/4
df_frac b = df_from_ints(1, 2); // 1/2
// Arithmetic operations
df_frac sum = df_add(a, b); // 5/4
df_frac product = df_mul(a, b); // 3/8
// Convert to string
char* str = df_to_string(sum);
printf("Result: %s\n", str); // "5/4"
free(str);
// Clean up (reference counting)
df_release(&sum);
df_release(&product);
return 0;
}
Reference-counted arbitrary precision rational number library.
df_frac df_add(df_frac a, df_frac b)
Add two fractions.
df_frac df_mul(df_frac a, df_frac b)
Multiply two fractions.
char * df_to_string(df_frac f)
Convert to string.
void df_release(df_frac *f)
Decrease reference count and free if zero.
df_frac df_from_ints(int64_t numerator, int64_t denominator)
Create a fraction from numerator and denominator.
Internal structure for a rational number.

Documentation

Full API documentation is available at: https://[your-repo].github.io/dynamic_fraction.h/

Configuration

Customize the library by defining these macros before including:

#define DF_MALLOC malloc // Custom allocator
#define DF_FREE free // Custom deallocator
#define DF_ASSERT assert // Custom assert macro
#define DF_IMPLEMENTATION

Building

With CMake

mkdir build && cd build
cmake ..
make
./tests # Run unit tests

Manual Compilation

gcc -std=c11 -Wall -Wextra main.c -o example

API Overview

Creation and Memory Management

Arithmetic Operations

Comparison Functions

Rounding and Truncation

Type Conversion and Testing

Advanced Functions

Memory Management

The library uses reference counting for automatic memory management:

df_frac a = df_from_ints(3, 4); // ref_count = 1
df_frac b = df_retain(a); // ref_count = 2
df_release(&a); // ref_count = 1, a = NULL
df_release(&b); // ref_count = 0, memory freed, b = NULL
df_frac df_retain(df_frac f)
Increase reference count.

Dependencies

  • dynamic_int.h: For arbitrary precision integer arithmetic (included in devDeps/)
  • C11 or later: Standard library functions

License

This project is dual-licensed under:

  • MIT License
  • The Unlicense

Choose whichever license works best for your project.

Platform Support

This library supports:

  • Linux: Full support with GCC and Clang
  • Windows: Compatible with MSVC and MinGW
  • macOS: Native Clang support
  • MCU: Designed for microcontroller environments

Version History

v1.0.0 (September 2025)

  • Complete rational arithmetic: Full arbitrary precision fraction operations
  • Comprehensive test suite: 26 unit tests covering all functions with 0 failures
  • Advanced mathematical functions: Floor, ceil, truncate, round, power operations
  • String conversion: Full parsing and generation of fraction strings
  • Reference counting: Robust automatic memory management
  • Documentation: Complete Doxygen API documentation with examples
  • Dual licensing: MIT OR Unlicense for maximum compatibility
  • GitHub Actions: Automated documentation generation and deployment

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.