Git Product home page Git Product logo

arpfloat's Introduction

Arbitrary-Precision Floating-Point Library

ARPFloat is an implementation of arbitrary precision floating point data structures and utilities. The library can be used to emulate floating point operation, in software, or create new floating point data types.

Example

  // Create a new type: 15 bits exponent, 112 significand.
  type FP128 = Float<15, 112>;

  // Use Newton-Raphson to find the square root of 5.
  let n = FP128::from_u64(5);

  let two = FP128::from_u64(2);
  let mut x = n;

  for _ in 0..1000 {
      x = (x + (n / x))/two;
  }

  println!("fp128: {}", x);
  println!("fp64:  {}", x.as_f64());

The program above will print this output:

fp128: 2.2360679774997896964091736687312763
fp64:  2.23606797749979

The library also provides API that exposes rounding modes, and low-level operations.

    // Explicit control over rounding modes:
    let val = FP128::mul_with_rm(y, z, RoundingMode::NearestTiesToEven);

    // View the internals of the float:
    let fp = FP16::from_i64(15);
    let m = fp.get_mantissa();

    // Prints FP[+ E=+3 M=11110000000]
    fp.dump();

Control the rounding-mode for type conversion:

   use arpfloat::{FP16, FP32, RoundingMode};
   let x = FP32::from_u64(2649);

   // Convert from FP64 to FP16.
   let b : FP16 = x.cast_with_rm(RoundingMode::Zero);
   println!("{}", b); // Prints 2648!

The examples directory contains a program that computes many digits of pi in float-256.

Resources

There are excellent resources out there, some of which are referenced in the code:

  • Books:
    • Handbook of Floating-Point Arithmetic 2010th by Jean-Michel Muller et al.
    • Elementary Functions: Algorithms and Implementation by Jean-Michel Muller.
  • Papers:
    • How to print floating-point numbers accurately
  • Other excellent software implementations: APFloat by Neil Booth, RYU, newlib, musl, etc.

License

Licensed under Apache-2.0

arpfloat's People

Contributors

nadavrot avatar

Watchers

 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.