Dynamic Fraction Library 1.0.0
Reference-counted arbitrary precision rational number library (MIT OR Unlicense)
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
dynamic_fraction.h File Reference

Reference-counted arbitrary precision rational number library. More...

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include "devDeps/dynamic_int.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

Go to the source code of this file.

Data Structures

struct  df_frac_internal
 Internal structure for a rational number. More...
 

Typedefs

typedef struct df_frac_internaldf_frac
 Opaque pointer to a rational number.
 

Functions

df_frac df_from_ints (int64_t numerator, int64_t denominator)
 Create a fraction from numerator and denominator.
 
df_frac df_from_di (di_int numerator, di_int denominator)
 Create a fraction from a di_int numerator and denominator.
 
df_frac df_from_int (int64_t value)
 Create a fraction from an integer.
 
df_frac df_from_double (double value, int64_t max_denominator)
 Create a fraction from a double.
 
df_frac df_copy (df_frac f)
 Create a copy of a fraction.
 
df_frac df_retain (df_frac f)
 Increase reference count.
 
void df_release (df_frac *f)
 Decrease reference count and free if zero.
 
df_frac df_add (df_frac a, df_frac b)
 Add two fractions.
 
df_frac df_sub (df_frac a, df_frac b)
 Subtract two fractions.
 
df_frac df_mul (df_frac a, df_frac b)
 Multiply two fractions.
 
df_frac df_div (df_frac a, df_frac b)
 Divide two fractions.
 
df_frac df_negate (df_frac f)
 Negate a fraction.
 
df_frac df_abs (df_frac f)
 Get absolute value.
 
df_frac df_reciprocal (df_frac f)
 Get reciprocal (1/f)
 
int df_cmp (df_frac a, df_frac b)
 Compare two fractions.
 
bool df_eq (df_frac a, df_frac b)
 Test equality.
 
bool df_ne (df_frac a, df_frac b)
 Test inequality.
 
bool df_lt (df_frac a, df_frac b)
 Test less than.
 
bool df_le (df_frac a, df_frac b)
 Test less than or equal.
 
bool df_gt (df_frac a, df_frac b)
 Test greater than.
 
bool df_ge (df_frac a, df_frac b)
 Test greater than or equal.
 
bool df_is_zero (df_frac f)
 Test if fraction is zero.
 
bool df_is_one (df_frac f)
 Test if fraction is one.
 
bool df_is_negative (df_frac f)
 Test if fraction is negative.
 
bool df_is_positive (df_frac f)
 Test if fraction is positive.
 
bool df_is_integer (df_frac f)
 Test if fraction is an integer.
 
double df_to_double (df_frac f)
 Convert to double.
 
bool df_to_int64 (df_frac f, int64_t *result)
 Convert to int64_t if possible.
 
char * df_to_string (df_frac f)
 Convert to string.
 
df_frac df_from_string (const char *str)
 Parse fraction from string.
 
di_int df_numerator (df_frac f)
 Get numerator as di_int.
 
di_int df_denominator (df_frac f)
 Get denominator as di_int.
 
df_frac df_zero (void)
 Create zero fraction (0/1)
 
df_frac df_one (void)
 Create one fraction (1/1)
 
df_frac df_neg_one (void)
 Create negative one fraction (-1/1)
 
df_frac df_pow (df_frac base, int64_t exponent)
 Raise fraction to integer power.
 
df_frac df_floor (df_frac f)
 Floor function - greatest integer ≤ f.
 
df_frac df_ceil (df_frac f)
 Ceiling function - smallest integer ≥ f.
 
df_frac df_trunc (df_frac f)
 Truncate towards zero.
 
df_frac df_round (df_frac f)
 Round to nearest integer.
 
int df_sign (df_frac f)
 Get sign of fraction.
 
df_frac df_min (df_frac a, df_frac b)
 Minimum of two fractions.
 
df_frac df_max (df_frac a, df_frac b)
 Maximum of two fractions.
 
uint64_t df_hash (df_frac f)
 Hash function for fractions.
 
bool df_fits_int32 (df_frac f)
 Check if fraction fits in int32_t.
 
bool df_fits_int64 (df_frac f)
 Check if fraction fits in int64_t.
 
bool df_fits_double (df_frac f)
 Check if fraction fits in double without precision loss.
 
di_int df_whole_part (df_frac f)
 Get integer (whole) part of fraction.
 
df_frac df_fractional_part (df_frac f)
 Get fractional part of fraction.
 

Detailed Description

Reference-counted arbitrary precision rational number library.

Version
1.0.0
Date
September 2025

Single header library for arbitrary precision rational numbers (fractions) with reference counting. Built on top of dynamic_int.h for arbitrary precision integer arithmetic.

License

Dual licensed under MIT OR Unlicense. See LICENSE file for details. Choose the license that best fits your project's needs.

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
Reference-counted arbitrary precision rational number library.

Basic Usage

df_frac a = df_from_ints(3, 4); // 3/4
df_frac b = df_from_ints(2, 3); // 2/3
df_frac sum = df_add(a, b); // 17/12
double result = df_to_double(sum);
printf("Sum: %f\n", result);
df_release(&sum);
df_frac df_add(df_frac a, df_frac b)
Add two fractions.
double df_to_double(df_frac f)
Convert to double.
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.

Definition in file dynamic_fraction.h.

Typedef Documentation

◆ df_frac

Opaque pointer to a rational number.

Definition at line 101 of file dynamic_fraction.h.