Node.js Project to Build Fraction Calculator in Browser Using fraction-calculator Library & Javascript

 

 

fraction-calculator

This is a library to calculate fraction. This library :

  1. can calculate floating number precisely
  2. can calculate(plus, minus, times, div, mod…) a fraction
  3. can power or sqrt a fraction.
  4. can convert a decimal to a fraction and convert it back
  5. can convert a recurring decimal to a fraction and convert it back
  6. supports IE
  7. has 100% UT coverage
  8. has 0 dependencies

Usage

Use it in ES6 or Node.js

Install:

Then:

Use it in modern browsers

This library has used some ES6 features, if you only target modern browsers, you can use ./dist/fraction-calculator.min.js, this file is only 7kb. This script will set a varable fc in the window, you can call it directly.

Use it in old browsers

If you also target some old browsers, like IE, you can use ./dist/fraction-calculator.polyfill.min.js, it is built with babel-polyfill

Features

Calculating floating number precisely

Calculating fraction

Converting a decimal to a fraction and convert it back

Converting a recurring decimal to a fraction and convert it back

API

All API

When you use this library, you shouldn’t use new, just fc(). Let’s take a look at all API:

constractor

Fraction calculator constractor supports many kinds of parameter, but if you pass an invalid parameter, you will get an error:

String

If you pass 0 or NaN as denominator, you will get an error:

Sting with whole part

Number

If you pass a NaN or Infinity, you will get an error:

Two numbers

Recurring decimal

You can use ''or()to mark recurring part. Please note if you input a long decimals(over 15 numbers), you may only get a closed fraction, not precise one.

Another instance

Calculation API

All calculation api are chainable, all return value is this. It means you can call it like this:

plus, minus, times, div,mod

You can know what they are doing from their names. They can support one parameter in all kinds that constractor supports. mod will get the same result as JS %.

pow(n)

pow can only support a number parameter, it is Math.pow(numerator, n)/Math.pow(denominator, n), if denominator reached Infinity, fraction will be reset to 0. If numerator reached Infinity, it will throw an error:

sqrt()

It is pow('1/2').

If fraction is negative, you call sqrt, you will get a error:

abs()

Get the absolute value.

neg()

Get the negation.

inverse()

Get the inverse.

ceil()

Based on Math.ceil().

floor()

Based on Math.floor().

round()

Based on Math.round().

Compare API

There are 3 compare API,equals,greaterThan,lessThan. They can support one parameter in all kinds that constractor supports. Their return valus are boolean.

Display API

toFraction(withWholePart = false)

This API will show fraction in string, but if it is an integer, it will only show integer in string.This api has an optional parameterwithWholePart, default is false. You can set it to true to show whole part.

toFixed(n)

Based on js toFixed:

toNumber()

This will show all decimals JS can handle:

toRecurringDecimal()

This API will show fraction in recurring decimal, recurring part will marked with (). Please note recurring part can be very long, the search can be very slow if it is. So this library will only search first 3000 decimals, If still not find a pattern, it will output the whole 3000 numbers, like “0.1234567890…….”.

Static API

fc.gcd(a, b)

This API will return GCD(greatest common divisor) of a and b.

But if a or b is NaN or Infinity, it will throw an error:

fc.lcm(a,b)

This API will return LCM(Lowest Common Multiple) of a and b.

But if a or b is NaN or Infinity, it will throw an error:

Other API

clone()

This API will return a copy of current instance:

Config

This library can work well without any config. But there is still a global config, you can disable reducing.

If you disable reducing, then call pow or sqrt, you may get decimals in numerator and denominator:

 

Leave a Reply