Code Monkey home page Code Monkey logo

big-int's Introduction

Big Int

Simple library for arbitrary-precision, arbitrary-base arithmetic, supporting arbitrarily large integers of any base from 2 to u64::MAX.

use big_int::prelude::*;

let mut a: Loose<10> = "9000000000000000000000000000000000000000".parse().unwrap();
a /= 13.into();
assert_eq!(a, "692307692307692307692307692307692307692".parse().unwrap());

let mut b: Loose<16> = a.convert();
assert_eq!(b, "208D59C8D8669EDC306F76344EC4EC4EC".parse().unwrap());
b >>= 16.into();

let c: Loose<2> = b.convert();
assert_eq!(c, "100000100011010101100111001000110110000110011010011110110111000011".parse().unwrap());

let mut d: Tight<256> = c.convert();
d += vec![15, 90, 0].into();
assert_eq!(d, vec![2, 8, 213, 156, 141, 134, 121, 71, 195].into());

let e: Tight<10> = d.convert();
assert_eq!(format!("{e}"), "37530075201422313411".to_string());

This crate contains five primary big int implementations:

  • LooseBytes<BASE> - A collection of loosely packed 8-bit byte values representing each digit. Slightly memory inefficient, but with minimal performance overhead. Capable of representing any base from 2-256.
  • LooseShorts<BASE> - A collection of loosely packed 16-bit short values representing each digit. Somewhat memory inefficient, but with minimal performance overhead. Capable of representing any base from 2-65536.
  • LooseWords<BASE> - A collection of loosely packed 32-bit word values representing each digit. Fairly memory inefficient, but with minimal performance overhead. Capable of representing any base from 2-2^32.
  • Loose<BASE> - A collection of loosely packed 64-bit ints representing each digit. Very memory inefficient, but with minimal performance overhead. Capable of representing any base from 2-2^64.
  • Tight<BASE> - A collection of tightly packed bits representing each digit. Maximally memory efficient, and capable of representing any base from 2-2^64. However, the additional indirection adds some performance overhead.

Ints support most basic arithmetic operations, including addition, subtraction, multiplication, division, exponentiation, logarithm, nth root, and left/right shifting. Notably, shifting acts on the BASE of the associated number, increasing or decreasing the magnitude by powers of BASE as opposed to powers of 2.

big-int's People

Contributors

maurdekye avatar

Watchers

 avatar  avatar

Forkers

stefnotch

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.