Giter Site home page Giter Site logo

rational's Introduction

A minimal library for representing rational numbers (ratios of integers).

Construction

// Rationals are automatically reduced when created:
let one_half = Rational::new(1, 2);
let two_quarters = Rational::new(2, 4);
assert_eq!(one_half, two_quarters);

// `From` is implemented for integers and integer tuples:
assert_eq!(Rational::from(1), Rational::new(1, 1));
assert_eq!(Rational::from((1, 2)), Rational::new(1, 2));

// The `new` method takes a numerator and denominator that implement `Into<Rational>`:
let one_half_over_one_quarter = Rational::new((1, 2), (1, 4));
assert_eq!(one_half_over_one_quarter, Rational::new(2, 1));

Mathematical operations

// Operations and comparisons are implemented for Rationals and integers:
let one_ninth = Rational::new(1, 9);
assert_eq!(one_ninth + Rational::new(5, 4), Rational::new(49, 36));
assert_eq!(one_ninth - 4, Rational::new(-35, 9));
assert_eq!(one_ninth / Rational::new(21, 6), Rational::new(2, 63));
assert!(one_ninth < Rational::new(1, 8));
assert!(one_ninth < 1);

Other properties

// Inverse:
let eight_thirds = Rational::new(8, 3);
let inverse = eight_thirds.inverse();
assert_eq!(inverse, Rational::new(3, 8));

// Mixed fractions:
let (whole, fractional) = eight_thirds.mixed_fraction();
assert_eq!(whole, 2);
assert_eq!(fractional, Rational::new(2, 3));

Features

  • Enable the num-traits feature to gain access to implementations of many of the traits defined in the num-traits crate for Rationals

rational's People

Contributors

ijagberg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.