<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Fraction.js - ℚ in JavaScript]]></title><description><![CDATA[<p dir="auto">&lt;h2&gt;Fraction.js - ℚ in JavaScript&lt;/h2&gt;</p>
<p dir="auto">&lt;p&gt;&lt;a href="upload://nVGfF0lVnvmkQJgPYtAwmBO1Lcp.svg"&gt;&lt;img src="<a href="https://npmjs.org/package/fraction.js" rel="nofollow ugc">https://npmjs.org/package/fraction.js</a> "View this project on npm"" alt="NPM Package&lt;/a&gt;"&gt;&lt;/p&gt;<br />
&lt;p&gt;&lt;a href="upload://htIhYxu9Zv4NLwcFjeH6LBexRWC.svg"&gt;&lt;img src="<a href="http://opensource.org/licenses/MIT" rel="nofollow ugc">http://opensource.org/licenses/MIT</a>" alt="MIT license&lt;/a&gt;"&gt;&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;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:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
1 / 98 * 98 // = 0.9999999999999999<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;If you need more precision or just want a fraction as a result, just include &lt;em&gt;Fraction.js&lt;/em&gt;:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
var Fraction = require('fraction.js');<br />
// or<br />
import Fraction from 'fraction.js';<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;and give it a trial:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
Fraction(1).div(98).mul(98) // = 1<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;Internally, numbers are represented as &lt;em&gt;numerator / denominator&lt;/em&gt;, which adds just a little overhead. However, the library is written with performance and accuracy in mind, which makes it the perfect basis for &lt;a href="<a href="https://github.com/infusion/Polynomial.js" rel="nofollow ugc">https://github.com/infusion/Polynomial.js</a>"&gt;Polynomial.js&lt;/a&gt; and &lt;a href="<a href="https://github.com/josdejong/mathjs" rel="nofollow ugc">https://github.com/josdejong/mathjs</a>"&gt;Math.js&lt;/a&gt;.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;Convert decimal to fraction&lt;/p&gt;<br />
&lt;p&gt;===&lt;/p&gt;<br />
&lt;p&gt;The simplest job for fraction.js is to get a fraction out of a decimal:&lt;/p&gt;<br />
&lt;pre&gt;&lt;code&gt;<br />
var x = new Fraction(1.88);<br />
var res = x.toFraction(true); // String "1 22/25"<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;Examples / Motivation&lt;/p&gt;<br />
&lt;p&gt;===&lt;/p&gt;<br />
&lt;p&gt;A simple example might be&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
var f = new Fraction("9.4'31'"); // 9.4313131313131...<br />
f.mul([-4, 3]).mod("4.'8'"); // 4.88888888888888...<br />
&lt;/code&gt;&lt;/pre&gt;<br />
&lt;p&gt;The result is&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
console.log(f.toFraction()); // -4154 / 1485<br />
&lt;/code&gt;&lt;/pre&gt;<br />
&lt;p&gt;You could of course also access the sign (s), numerator (n) and denominator (d) on your own:&lt;/p&gt;<br />
&lt;pre&gt;&lt;code&gt;<br />
f.s * f.n / f.d = -1 * 4154 / 1485 = -2.797306...<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;If you would try to calculate it yourself, you would come up with something like:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
(9.4313131 * (-4 / 3)) % 4.888888 = -2.797308133...<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;Quite okay, but yea - not as accurate as it could be.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;Laplace Probability&lt;/p&gt;<br />
&lt;p&gt;===&lt;/p&gt;<br />
&lt;p&gt;Simple example. What's the probability of throwing a 3, and 1 or 4, and 2 or 4 or 6 with a fair dice?&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;P({3}):&lt;/p&gt;<br />
&lt;pre&gt;&lt;code&gt;<br />
var p = new Fraction([3].length, 6).toString(); // 0.1(6)<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;P({1, 4}):&lt;/p&gt;<br />
&lt;pre&gt;&lt;code&gt;<br />
var p = new Fraction([1, 4].length, 6).toString(); // 0.(3)<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;P({2, 4, 6}):&lt;/p&gt;<br />
&lt;pre&gt;&lt;code&gt;<br />
var p = new Fraction([2, 4, 6].length, 6).toString(); // 0.5<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;Convert degrees/minutes/seconds to precise rational representation:&lt;/p&gt;<br />
&lt;p&gt;===&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;57+45/60+17/3600&lt;/p&gt;<br />
&lt;pre&gt;&lt;code&gt;<br />
var deg = 57; // 57°<br />
var min = 45; // 45 Minutes<br />
var sec = 17; // 17 Seconds</p>
<p dir="auto">new Fraction(deg).add(min, 60).add(sec, 3600).toString() // -&gt; 57.7547(2)<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;Rational approximation of irrational numbers&lt;/p&gt;<br />
&lt;p&gt;===&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;Now it's getting messy ;d To approximate a number like &lt;em&gt;sqrt(5) - 2&lt;/em&gt; with a numerator and denominator, you can reformat the equation as follows: &lt;em&gt;pow(n / d + 2, 2) = 5&lt;/em&gt;.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;Then the following algorithm will generate the rational number besides the binary representation.&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
var x = "/", s = "";</p>
<p dir="auto">var a = new Fraction(0),<br />
b = new Fraction(1);<br />
for (var n = 0; n &lt;= 10; n++) {</p>
<p dir="auto">var c = a.add(b).div(2);</p>
<p dir="auto">console.log(n + "\t" + a + "\t" + b + "\t" + c + "\t" + x);</p>
<p dir="auto">if (c.add(2).pow(2) &lt; 5) {<br />
a = c;<br />
x = "1";<br />
} else {<br />
b = c;<br />
x = "0";<br />
}<br />
s+= x;<br />
}<br />
console.log(s)<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;The result is&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
n   a[n]        b[n]        c[n]            x[n]<br />
0   0/1         1/1         1/2             /<br />
1   0/1         1/2         1/4             0<br />
2   0/1         1/4         1/8             0<br />
3   1/8         1/4         3/16            1<br />
4   3/16        1/4</p>
<hr />
<p dir="auto">新手建议收藏，老手欢迎指点。<br />
&lt;/code&gt;&lt;/pre&gt;</p>
]]></description><link>https://forum.mj7.cn/topic/7205/fraction-js-ℚ-in-javascript</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 09:34:23 GMT</lastBuildDate><atom:link href="https://forum.mj7.cn/topic/7205.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Apr 2026 21:56:14 GMT</pubDate><ttl>60</ttl></channel></rss>