Git Product home page Git Product logo

bigfloat-deno's Introduction

BigFloat

A library for arbitrary precision decimal floating point arithmetic that can exactly represent all decimal fractions, unlike JavaScript's number data type which is 64-bit binary floating point.

Based on the original work by Douglas Crockford. This implementation is built upon ES native bigints.

This library provides three ways to make bigfloat operations:

  • A set of functions for a functional style approach
  • A BigFloat class with an API similar to that of Decimal.js
  • An evaluate() function that parses and resolves an expression

Basic usage

Functional style

import { make, string, sqrt } from "https://deno.land/x/bigfloat/mod.ts";

string(sqrt(make("2")));               // "1.4142"

Class based

import { BigFloat } from "https://deno.land/x/bigfloat/mod.ts";

new BigFloat("2").sqrt().toString();   // "1.4142"

The evaluate() function

evaluate(expression: string, precision?: number): string | boolean

The first argument can be any valid arithmetic or relational expression, including scientific e-notation. Precision should be a negative integer. Default is -4.

import { evaluate } from "https://deno.land/x/bigfloat/mod.ts";

0.1 + 0.2 === 0.3;                     // false
evaluate("0.1 + 0.2 == 0.3");          // true

0.1 + 0.2;                             // 0.30000000000000004
evaluate("0.1 + 0.2");                 // "0.3"

1 + Number.EPSILON / 2;                // 1
evaluate(`1 + ${Number.EPSILON / 2}`); // "1.00000000000000011102230246251565"

evaluate("1 + 2.220446049250313e-16"); // "1.0000000000000002220446049250313"

evaluate(`4 >= ${Math.PI}`);           // true

Valid tokens:

  • Parenthesis: (,)
  • Number: Decimal, integer or scientific e-notation
  • Operator: Arithmetic +,-,/,*,** Relational ===,==,!==,!=,<,>,<=,>=

Change precision

import { BigFloat, set_precision } from "https://deno.land/x/bigfloat/mod.ts";

new BigFloat(2).sqrt().toString();     // "1.4142"
set_precision(-10);
new BigFloat(2).sqrt().toString();     // "1.4142135623"

The bigfloat object

interface IBigFloat {
  coefficient: bigint;
  exponent: number;
}

Valid bigfloat made from primitives:

const bigfloat: IBigFloat {
  coefficient: 522299n,
  exponent: -4
};

bigfloat-deno's People

Contributors

davidmartinez10 avatar

Stargazers

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