Skip to content

Simplify, convert, and do math with fractions in Zig

License

Notifications You must be signed in to change notification settings

Chriscbr/zig-fractions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zig-fractions

A Zig library for performing math with fractions, where each fraction is represented by a pair of integers.

Compatible with Zig 0.13 stable.

Supported APIs:

  • convertion from floats, or numerator/denominator pairs (fromFloat(), init())
  • convertion to floats and integers (to(T))
  • formatting (toString(), toStringAlloc(), format())
  • negation, absolute value (negate(), abs())
  • addition, subtraction, multiplication, division (add(), sub(), mul(), div())
  • comparison (eql(), eqlAbs(), eqlZero(), order(), orderAbs())
  • simplifying (simplify())

Installation

First, run the following:

zig fetch --save git+https://github.com/Chriscbr/zig-fractions

Then add the following to build.zig:

const zig_fractions = b.dependency("zig-fractions", .{});
exe.root_module.addImport("zig-fractions", zig_fractions.module("zig-fractions"));

Then you can use the library in your Zig project:

const Fraction = @import("zig-fractions").Fraction;

var f1 = try Fraction.fromFloat(@as(f32, 2.5));
const f2 = try Fraction.init(1, 5, false);
try f1.mul(&f2); // 2.5 * 1/5 = 1/2
std.debug.print("{}\n", .{f1}); // "1/2"

Contributing

Pull requests are welcome.

About

Simplify, convert, and do math with fractions in Zig

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages