HTML pre code to Markdown code black

This commit is contained in:
Brandon Rozek 2025-02-16 13:03:06 -05:00
parent 3b1974b3fd
commit e06d45e053
No known key found for this signature in database
GPG key ID: DFB0E78F805F4567
6 changed files with 127 additions and 191 deletions

View file

@ -15,19 +15,19 @@ mf2_cite:
- 'a:1:{s:6:"author";a:0:{}}'
tags: ["JS"]
---
Last week I published my first library over on Github called [Fractions.js](https://github.com/brandonrozek/Fractions.js). Fractions.js is a library to help avoid the [mathmatetical errors](http://floating-point-gui.de/) in floating point arithmetic. What do you mean by floating point artihmetic errors? Here is an example: <code class="language-javascript">.1 * .2</code> outputs <code class="language-javascript">0.020000000000000004</code> even though the correct answer is <code class="language-javascript">.02</code>
Last week I published my first library over on Github called [Fractions.js](https://github.com/brandonrozek/Fractions.js). Fractions.js is a library to help avoid the [mathmatetical errors](http://floating-point-gui.de/) in floating point arithmetic. What do you mean by floating point artihmetic errors? Here is an example: `.1 * .2` outputs `0.020000000000000004` even though the correct answer is `.02`.
<!--more-->
## <a href="#purpose" name="purpose"></a>Purpose {#purpose}
## Purpose
My team and I are currently working on a new game for Math I/O. One of the levels is testing whether the player can add, subtract, multiply, and divide fractions correctly. I didnt want to implement a solution where we check if the input entered is within a range of the answer. So I asked the team, how do you add, subtract, multiply, and divide fractions in your head? As we were working through it together, I realized that this is a common problem over at Math I/O. So I decided to make a library dedicated to this.
## <a href="#how-it-works" name="how-it-works"></a>How it works {#how-it-works}
## How it works
I broke up each fraction into two things, a numerator and a denominator. With these two numbers, I can figure out all of the most common operations.
### <a href="#addition" name="addition"></a>Addition {#addition}
### Addition
For addition, if two fractions have the same denominator, then you just need to add the numerators.
@ -39,66 +39,66 @@ If not, then you need to change it to have a common denominator. We can do this
1/2 + 1/3 = (1 * 3)/ (2 * 3) + (1 * 2)/ (2 * 3) = 3/6 + 2/6 = 5/6
### <a href="#subtraction" name="subtraction"></a>Subtraction {#subtraction}
### Subtraction
Works the same as addition, except the second fraction is subtracted (taken away) from the first.
### <a href="#multiplication" name="multiplication"></a>Multiplication {#multiplication}
### Multiplication
To multiply two fractions, just multiply the numerators by each other, and the denominators by each other.
2/3 * 1/2 = 2/6
### <a href="#division" name="division"></a>Division {#division}
### Division
Treated similar to multiplication since dividing a number is the same thing as multiplying by its [reciprocal](https://www.mathsisfun.com/reciprocal.html).
1 / (1 / 2) = 1 * 2 = 2
### <a href="#simplification" name="simplification"></a>Simplification {#simplification}
### Simplification
Sometimes with the operations above, itll produce fractions in an unsimplified form. To avoid any confusion, I created a simplify function. It was challanging trying to figure out a way to code this. While I was browsing around for an implementation, I knocked into Eucliders algorithm for finding the greatest common factor. Straight from the [Wikipedia article](https://en.wikipedia.org/wiki/Euclidean_algorithm) (where a is greater than b):
function gcd(a, b)
while b ≠ 0
t := b;
b := a mod b;
a := t;
return a;
```
function gcd(a, b)
while b ≠ 0
t := b;
b := a mod b;
a := t;
return a;
```
I can then simplify the fraction by dividing the numerator and denominator by the greatest common factor.
## <a href="#the-api" name="the-api"></a>The API {#the-api}
## The API
I decided to provide as much flexibility as I can in the API. You have several ways to create a new Fraction.
<pre><code class="language-javascript">
```javascript
var oneHalf = new Fraction(1,2);
var oneHalf = new Fraction(.5);
var oneHalf = new Fraction("1/2");
var oneHalf = new Fraction("1", "2")
</code></pre>
var oneHalf = new Fraction("1", "2");
```
All of these results will return a Fraction with a numerator of <code class="language-javascript">1</code> and a denominator of <code class="language-javascript">2</code>. You also have two different ways to do the most common operations.
All of these results will return a Fraction with a numerator of `1` and a denominator of `2`. You also have two different ways to do the most common operations.
<pre><code class="language-javascript">
```javascript
var fiveThirds = Fraction.add("1/3", "4/3");
var fiveThirds = new Fraction("1/3").add("4/3");
</code></pre>
```
The second style came from how jQuery implements its library. That way you can chain operations.
<pre><code class="language-javascript">
```javascript
new Fraction("1/2").add("2/3").divide("5/6").subtract("7/8").multiply("6/5").toString()
</code></pre>
```
Outputs <code class="language-javascript">'63/100'</code> This is accomplished in the code through [prototypes](http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/).
Outputs `'63/100'` This is accomplished in the code through [prototypes](http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/).
<pre><code class="language-javascript">
```javascript
Fraction.add = function(frac1, frac2) {
Fraction.correctArgumentLength(2, arguments.length);
frac1 = Fraction.toFraction(frac1)
@ -113,10 +113,10 @@ Fraction.prototype.add = function(frac) {
Fraction.correctArgumentLength(1, arguments.length);
return Fraction.change(this, Fraction.add(this, frac));
}
</code></pre>
```
In the code, the add prototype calls the Fraction.add function within it to avoid code duplication.
## <a href="#conclusion" name="conclusion"></a>Conclusion {#conclusion}
## Conclusion
After I coded this up, I looked online for different implementations and found [fraction.js](https://github.com/ekg/fraction.js) by [Erik Garrison](http://hypervolu.me/~erik/). Its important to look at different implementations and see which matches your needs better. This post isnt meant to go fully into detail of the library. To know what else the library can do, visit the [readme page](https://github.com/brandonrozek/Fractions.js/blob/master/README.md). If youre curious in how its implemented, check out the [code](https://github.com/brandonrozek/Fractions.js/blob/master/Fraction.js). [Email me](mailto:brozek@brandonrozek.com) if you have any questions/criticisms 🙂