Skip to content

Files

Latest commit

 

History

History
 
 

bigfraction

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

cljsjs/bigfraction

[cljsjs/bigfraction "4.1.1-0"] ;; latest release

This jar comes with deps.cljs as used by the [Foreign Libs][flibs] feature of the ClojureScript compiler. After adding the above dependency to your project you can require the packaged library like so:

(ns application.core
  (:require cljsjs.bigfraction))

This package also supports :global-exports:

(ns application.core
  (:require ["fraction.js/bigfraction.js" :as BigFraction]))

This works too:

(ns application.core
  (:require [cljsjs.bigfraction :as BigFraction]))

Fraction is a rational number library written in JavaScript. BigFraction is an API-identical version of Fraction, bundled inside the fraction.js npm dependency. From the site:

Tired of inprecise numbers represented by doubles, which have to store rational and irrational numbers like PI or sqrt(2) the same way? Obviously the following problem is preventable:

1 / 98 * 98 // = 0.9999999999999999

If you need more precision or just want a fraction as a result, have a look at Fraction.js:

var Fraction = require('fraction.js/bigfraction.js');

Fraction(1).div(98).mul(98) // = 1

Internally, numbers are represented as numerator / denominator, which adds just a little overhead. However, the library is written with performance in mind and outperforms any other implementation, as you can see here. This basic data-type makes it the perfect basis for Polynomial.js and Math.js.

Example usage from Clojurescript:

(ns example.core
  (:require ["fraction.js/bigfraction.js" :as BigFraction]))

(extend-type BigFraction
  IEquiv
  (-equiv [this other]
    (.equals this other)))

(defn fraction
  [num denom]
  (BigFraction. num denom))

(= (fraction 50 100)
   (fraction 1 2))
;; => true