Giter VIP home page Giter VIP logo

fraction's Introduction

Fraction

Lossless fractions and decimals; drop-in float replacement

CircleCI Documentation Current Version on crates.io MIT / Apache2 License

Features

  • Drop in replacement for floats with the exception for NaN == NaN so that it's hashable
  • It's hashable, so may be used as values in Sets and keys in dictionaries and hash maps
  • Fraction type, representing floats as fractions
  • Decimal type, based on Fraction type, represents floats as decimals
  • DynaInt implements dynamically growing integer type that performs checked math and avoids stack overflows
  • PostgreSQL integration for Numeric/Decimal type (with no extra memory allocations)
  • Juniper integration for both fractions and decimals
  • Generic integer conversions, such as i8 -> u8, usize -> u8 and so on
  • Lossless division with no allocations and infinite precision

Documentation

Here: Documentation

Examples

Formatting

use fraction::Fraction;
fn main() {
  let fraction = Fraction::new(7u, 4u);

  assert_eq!("7/4", format!("{}", fraction));
  assert_eq!("1.75", format!("{:.2}", fraction));
  assert_eq!("1.750", format!("{:#.3}", fraction));
}

Simple arithmetic

use fraction::Fraction;
use fraction::ToPrimitive;
use fraction::Zero;

fn main() {
  let mut fr = Fraction::zero();

  fr = fr + Fraction::from(2);   // 0 + 2   = 2
  fr = fr / Fraction::from(0.5); // 2 / 0.5 = 4

  assert_eq!(fr, Fraction::from(4));
  assert_eq!(4.0f64, fr.to_f64().unwrap());
}

Decimal

fn main() {
  type D = fraction::Decimal;

  let result = D::from(0.5) / D::from(0.3);
  assert_eq!(format!("{:.4}", result), "1.6666");
}

Using as keys for a HashMap

use std::collections::HashMap;
use fraction::Fraction;

fn main () {
  let f = Fraction::from(0.75);

  let mut map: HashMap<Fraction, ()> = HashMap::new();

  map.insert(f, ());

  assert!(map.contains_key(&Fraction::new(3u64, 4u64)));   // 0.75 == 3/4
  assert!(map.contains_key(&Fraction::new(6u64, 8u64)));   // 0.75 == 6/8
  assert!(map.contains_key(&Fraction::new(12u64, 16u64))); // 0.75 == 12/16
  assert!(! map.contains_key(&Fraction::from(0.5))); // 0.75 != 1/2
}

Generic integer conversion

use fraction::{GenericFraction, Sign};

fn main() {
  type F = GenericFraction<u32>;

  let fra = F::new_generic(Sign::Plus, 1i8, 42usize).unwrap();
  assert_eq!(fra, F::new(1u32, 42u32));
}

Comparison

use fraction::Fraction;

fn main () {
  let f14 = Fraction::new(1u64, 4u64); // 1/4 == 0.25
  let f12 = Fraction::new(1u64, 2u64); // 1/2 == 0.50
  let f24 = Fraction::new(2u64, 4u64); // 2/4 == 0.50
  let f34 = Fraction::new(3u64, 4u64); // 3/4 == 0.75

  assert_eq!(f12, f24);                   // 1/2 == 2/4
  assert_eq!(f34, f12 + f14);             // 3/4 == 1/2 + 1/4
  assert_eq!(f14, Fraction::from(0.25)); // 1/4 == 0.25
}

Change Log

Look into the CHANGELOG.md file for details

fraction's People

Contributors

dnsl48 avatar hbina avatar pthariensflame avatar christopherrabotin avatar eutampieri avatar jgarvin avatar tan-wei avatar

Watchers

James Cloos avatar  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.