Intial Commit

This commit is contained in:
valki
2020-10-17 18:42:50 +02:00
commit 664c6d8ca3
5892 changed files with 759183 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" default="true" />
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Fraction.js.iml" filepath="$PROJECT_DIR$/.idea/Fraction.js.iml" />
</modules>
</component>
</project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,373 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="46d9ca99-180b-4535-b1d7-fbc8fc53e9de" name="Default" comment="">
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/bower.json" afterPath="$PROJECT_DIR$/bower.json" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/fraction.js" afterPath="$PROJECT_DIR$/fraction.js" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/fraction.min.js" afterPath="$PROJECT_DIR$/fraction.min.js" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/package.json" afterPath="$PROJECT_DIR$/package.json" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="CreatePatchCommitExecutor">
<option name="PATCH_PATH" value="" />
</component>
<component name="ExecutionTargetManager" SELECTED_TARGET="default_target" />
<component name="FileEditorManager">
<leaf>
<file leaf-file-name="bower.json" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/bower.json">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="51">
<caret line="3" column="21" lean-forward="false" selection-start-line="3" selection-start-column="21" selection-end-line="3" selection-end-column="21" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="fraction.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/fraction.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17">
<caret line="1" column="32" lean-forward="false" selection-start-line="1" selection-start-column="32" selection-end-line="1" selection-end-column="32" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="fraction.min.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/fraction.min.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="170">
<caret line="10" column="6" lean-forward="true" selection-start-line="10" selection-start-column="6" selection-end-line="10" selection-end-column="6" />
<folding>
<element signature="n#!!doc" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="package.json" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/package.json">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="510">
<caret line="30" column="0" lean-forward="true" selection-start-line="30" selection-start-column="0" selection-end-line="30" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>title</find>
</findStrings>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="GradleLocalSettings">
<option name="externalProjectsViewState">
<projects_view />
</option>
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/tests/fraction.test.js" />
<option value="$PROJECT_DIR$/package.json" />
<option value="$PROJECT_DIR$/fraction.min.js" />
<option value="$PROJECT_DIR$/fraction.js" />
<option value="$PROJECT_DIR$/bower.json" />
</list>
</option>
</component>
<component name="JsBuildToolGruntFileManager" detection-done="true" sorting="DEFINITION_ORDER" />
<component name="JsBuildToolPackageJson" detection-done="true" sorting="DEFINITION_ORDER" />
<component name="JsGulpfileManager">
<detection-done>true</detection-done>
<sorting>DEFINITION_ORDER</sorting>
</component>
<component name="NodeModulesDirectoryManager">
<handled-path value="$PROJECT_DIR$/node_modules" />
</component>
<component name="ProjectFrameBounds">
<option name="x" value="79" />
<option name="y" value="23" />
<option name="width" value="1334" />
<option name="height" value="837" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1">
<flattenPackages />
<showMembers />
<showModules />
<showLibraryContents />
<hideEmptyPackages />
<abbreviatePackageNames />
<autoscrollToSource />
<autoscrollFromSource />
<sortByType />
<manualOrder />
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="Fraction.js" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="Fraction.js" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="Fraction.js" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="Fraction.js" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="fraction.js" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.NestingTreeNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="Fraction.js" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="Fraction.js" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="tests" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
</subPane>
</pane>
<pane id="PackagesPane" />
<pane id="Scratches" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path" value="/usr/local/bin/node" />
</component>
<component name="RunManager">
<configuration default="true" type="#org.jetbrains.idea.devkit.run.PluginConfigurationType" factoryName="Plugin">
<module name="" />
<option name="VM_PARAMETERS" value="-Xmx512m -Xms256m -XX:MaxPermSize=250m -ea" />
<option name="PROGRAM_PARAMETERS" />
<predefined_log_file id="idea.log" enabled="true" />
<method />
</configuration>
<configuration default="true" type="Applet" factoryName="Applet">
<option name="HTML_USED" value="false" />
<option name="WIDTH" value="400" />
<option name="HEIGHT" value="300" />
<option name="POLICY_FILE" value="$APPLICATION_HOME_DIR$/bin/appletviewer.policy" />
<module />
<method />
</configuration>
<configuration default="true" type="Application" factoryName="Application">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" />
<option name="PROGRAM_PARAMETERS" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="ENABLE_SWING_INSPECTOR" value="false" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<module name="" />
<envs />
<method />
</configuration>
<configuration default="true" type="JUnit" factoryName="JUnit">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PACKAGE_NAME" />
<option name="MAIN_CLASS_NAME" />
<option name="METHOD_NAME" />
<option name="TEST_OBJECT" value="class" />
<option name="VM_PARAMETERS" value="-ea" />
<option name="PARAMETERS" />
<option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<envs />
<patterns />
<method />
</configuration>
<configuration default="true" type="NodeJSConfigurationType" factoryName="Node.js" path-to-node="project" working-dir="">
<method />
</configuration>
<configuration default="true" type="Remote" factoryName="Remote">
<option name="USE_SOCKET_TRANSPORT" value="true" />
<option name="SERVER_MODE" value="false" />
<option name="SHMEM_ADDRESS" value="javadebug" />
<option name="HOST" value="localhost" />
<option name="PORT" value="5005" />
<method />
</configuration>
<configuration default="true" type="TestNG" factoryName="TestNG">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="SUITE_NAME" />
<option name="PACKAGE_NAME" />
<option name="MAIN_CLASS_NAME" />
<option name="METHOD_NAME" />
<option name="GROUP_NAME" />
<option name="TEST_OBJECT" value="CLASS" />
<option name="VM_PARAMETERS" value="-ea" />
<option name="PARAMETERS" />
<option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
<option name="OUTPUT_DIRECTORY" />
<option name="ANNOTATION_TYPE" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<option name="USE_DEFAULT_REPORTERS" value="false" />
<option name="PROPERTIES_FILE" />
<envs />
<properties />
<listeners />
<method />
</configuration>
</component>
<component name="SbtLocalSettings">
<option name="modificationStamps">
<map>
<entry key="$PROJECT_DIR$/../pinyal/Core" value="1483405772000" />
</map>
</option>
<option name="externalProjectsViewState">
<projects_view />
</option>
</component>
<component name="ShelveChangesManager" show_recycled="false">
<option name="remove_strategy" value="false" />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="46d9ca99-180b-4535-b1d7-fbc8fc53e9de" name="Default" comment="" />
<created>1486592714442</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1486592714442</updated>
<workItem from="1486592716384" duration="2073000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="2073000" />
</component>
<component name="ToolWindowManager">
<frame x="79" y="23" width="1334" height="837" extended-state="0" />
<editor active="true" />
<layout>
<window_info id="Designer" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Palette&#9;" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="processedProjectFiles" value="true" />
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/tests/fraction.test.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-23576">
<caret line="25" column="6" lean-forward="true" selection-start-line="25" selection-start-column="6" selection-end-line="25" selection-end-column="6" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/bower.json">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="51">
<caret line="3" column="21" lean-forward="false" selection-start-line="3" selection-start-column="21" selection-end-line="3" selection-end-column="21" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/fraction.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17">
<caret line="1" column="32" lean-forward="false" selection-start-line="1" selection-start-column="32" selection-end-line="1" selection-end-column="32" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/fraction.min.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="170">
<caret line="10" column="6" lean-forward="true" selection-start-line="10" selection-start-column="6" selection-end-line="10" selection-end-column="6" />
<folding>
<element signature="n#!!doc" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/package.json">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="510">
<caret line="30" column="0" lean-forward="true" selection-start-line="30" selection-start-column="0" selection-end-line="30" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</component>
</project>

View File

@@ -0,0 +1,4 @@
language: node_js
node_js:
- "stable"
script: npm test

489
nodered/rootfs/data/node_modules/fraction.js/README.md generated vendored Normal file
View File

@@ -0,0 +1,489 @@
# Fraction.js - in JavaSript
[![NPM Package](https://img.shields.io/npm/v/fraction.js.svg?style=flat)](https://npmjs.org/package/fraction.js "View this project on npm")
[![Build Status](https://travis-ci.org/infusion/Fraction.js.svg?branch=master)](https://travis-ci.org/infusion/Fraction.js)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
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:
```javascript
1 / 98 * 98 // = 0.9999999999999999
```
If you need more precision or just want a fraction as a result, have a look at *Fraction.js*:
```javascript
var Fraction = require('fraction.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](http://jsperf.com/convert-a-rational-number-to-a-babylonian-fractions/28). This basic data-type makes it the perfect basis for [Polynomial.js](https://github.com/infusion/Polynomial.js) and [Math.js](https://github.com/josdejong/mathjs).
Convert decimal to fraction
===
The simplest job for fraction.js is to get a fraction out of a decimal:
```javascript
var x = new Fraction(1.88);
var res = x.toFraction(true); // String "1 22/25"
```
Examples / Motivation
===
A simple example might be
```javascript
var f = new Fraction("9.4'31'"); // 9.4313131313131...
f.mul([-4, 3]).mod("4.'8'"); // 4.88888888888888...
```
The result is
```javascript
console.log(f.toFraction()); // -4154 / 1485
```
You could of course also access the sign (s), numerator (n) and denominator (d) on your own:
```javascript
f.s * f.n / f.d = -1 * 4154 / 1485 = -2.797306...
```
If you would try to calculate it yourself, you would come up with something like:
```javascript
(9.4313131 * (-4 / 3)) % 4.888888 = -2.797308133...
```
Quite okay, but yea - not as accurate as it could be.
Laplace Probability
===
Simple example. What's the probability of throwing a 3, and 1 or 4, and 2 or 4 or 6 with a fair dice?
P({3}):
```javascript
var p = new Fraction([3].length, 6).toString(); // 0.1(6)
```
P({1, 4}):
```javascript
var p = new Fraction([1, 4].length, 6).toString(); // 0.(3)
```
P({2, 4, 6}):
```javascript
var p = new Fraction([2, 4, 6].length, 6).toString(); // 0.5
```
Convert degrees/minutes/seconds to precise rational representation:
===
57+45/60+17/3600
```javascript
var deg = 57; // 57°
var min = 45; // 45 Minutes
var sec = 17; // 17 Seconds
new Fraction(deg).add(min, 60).add(sec, 3600).toString() // -> 57.7547(2)
```
Rational approximation of irrational numbers
===
Now it's getting messy ;d To approximate a number like *sqrt(5) - 2* with a numerator and denominator, you can reformat the equation as follows: *pow(n / d + 2, 2) = 5*.
Then the following algorithm will generate the rational number besides the binary representation.
```javascript
var x = "/", s = "";
var a = new Fraction(0),
b = new Fraction(1);
for (var n = 0; n <= 10; n++) {
var c = new Fraction(a).add(b).div(2);
console.log(n + "\t" + a.n + "/" + a.d + "\t" + b.n + "/" + b.d + "\t" + c.n + "/" + c.d + "\t" + x);
if (c.add(2).pow(2) < 5) {
a = c;
x = "1";
} else {
b = c;
x = "0";
}
s+= x;
}
console.log(s)
```
The result is
```
n a[n] b[n] c[n] x[n]
0 0/1 1/1 1/2 /
1 0/1 1/2 1/4 0
2 0/1 1/4 1/8 0
3 1/8 1/4 3/16 1
4 3/16 1/4 7/32 1
5 7/32 1/4 15/64 1
6 15/64 1/4 31/128 1
7 15/64 31/128 61/256 0
8 15/64 61/256 121/512 0
9 15/64 121/512 241/1024 0
10 241/1024 121/512 483/2048 1
```
Thus the approximation after 11 iterations of the bisection method is *483 / 2048* and the binary representation is 0.00111100011 (see [WolframAlpha](http://www.wolframalpha.com/input/?i=sqrt%285%29-2+binary))
I published another example on how to approximate PI with fraction.js on my [blog](http://www.xarg.org/2014/03/precise-calculations-in-javascript/) (Still not the best idea to approximate irrational numbers, but it illustrates the capabilities of Fraction.js perfectly).
Get the exact fractional part of a number
---
```javascript
var f = new Fraction("-6.(3416)");
console.log("" + f.mod(1).abs()); // Same as: Math.abs(f - parseInt(f, 10));
```
Mathematical correct modulo
---
The behaviour on negative congruences is different to most modulo implementations in computer science. Even the *mod()* function of Fraction.js behaves in the typical way. To solve the problem of having the mathematical correct modulo with Fraction.js you could come up with this:
```javascript
var a = -1;
var b = 10.99;
console.log(new Fraction(a)
.mod(b)); // Not correct, usual Modulo
console.log(new Fraction(a)
.mod(b).add(b).mod(b)); // Correct! Mathematical Modulo
```
fmod() impreciseness circumvented
---
It turns out that Fraction.js outperforms almost any fmod() implementation, including JavaScript itself, [php.js](http://phpjs.org/functions/fmod/), C++, Python, Java and even Wolframalpha due to the fact that numbers like 0.05, 0.1, ... are infinite decimal in base 2.
The equation *fmod(4.55, 0.05)* gives *0.04999999999999957*, wolframalpha says *1/20*. The correct answer should be **zero**, as 0.05 divides 4.55 without any remainder.
Parser
===
Any function (see below) as well as the constructor of the *Fraction* class parses its input and reduce it to the smallest term.
You can pass either Arrays, Objects, Integers, Doubles or Strings.
Arrays / Objects
---
```javascript
new Fraction(numerator, denominator);
new Fraction([numerator, denominator]);
new Fraction({n: numerator, d: denominator});
```
Integers
---
```javascript
new Fraction(123);
```
Doubles
---
```javascript
new Fraction(55.4);
```
**Note:** If you pass a double as it is, Fraction.js will perform a number analysis based on Farey Sequences. If you concern performance, cache Fraction.js objects and pass arrays/objects.
The method is really precise, but too large exact numbers, like 1234567.9991829 will result in a wrong approximation. If you want to keep the number as it is, convert it to a string, as the string parser will not perform any further observations. If you have problems with the approximation, in the file `examples/approx.js` is a different approximation algorithm, which might work better in some more specific use-cases.
Strings
---
```javascript
new Fraction("123.45");
new Fraction("123/45"); // A rational number represented as two decimals, separated by a slash
new Fraction("123:45"); // A rational number represented as two decimals, separated by a colon
new Fraction("4 123/45"); // A rational number represented as a whole number and a fraction
new Fraction("123.'456'"); // Note the quotes, see below!
new Fraction("123.(456)"); // Note the brackets, see below!
new Fraction("123.45'6'"); // Note the quotes, see below!
new Fraction("123.45(6)"); // Note the brackets, see below!
```
Two arguments
---
```javascript
new Fraction(3, 2); // 3/2 = 1.5
```
Repeating decimal places
---
*Fraction.js* can easily handle repeating decimal places. For example *1/3* is *0.3333...*. There is only one repeating digit. As you can see in the examples above, you can pass a number like *1/3* as "0.'3'" or "0.(3)", which are synonym. There are no tests to parse something like 0.166666666 to 1/6! If you really want to handle this number, wrap around brackets on your own with the function below for example: 0.1(66666666)
Assume you want to divide 123.32 / 33.6(567). [WolframAlpha](http://www.wolframalpha.com/input/?i=123.32+%2F+%2812453%2F370%29) states that you'll get a period of 1776 digits. *Fraction.js* comes to the same result. Give it a try:
```javascript
var f = new Fraction("123.32");
console.log("Bam: " + f.div("33.6(567)"));
```
To automatically make a number like "0.123123123" to something more Fraction.js friendly like "0.(123)", I hacked this little brute force algorithm in a 10 minutes. Improvements are welcome...
```javascript
function formatDecimal(str) {
var comma, pre, offset, pad, times, repeat;
if (-1 === (comma = str.indexOf(".")))
return str;
pre = str.substr(0, comma + 1);
str = str.substr(comma + 1);
for (var i = 0; i < str.length; i++) {
offset = str.substr(0, i);
for (var j = 0; j < 5; j++) {
pad = str.substr(i, j + 1);
times = Math.ceil((str.length - offset.length) / pad.length);
repeat = new Array(times + 1).join(pad); // Silly String.repeat hack
if (0 === (offset + repeat).indexOf(str)) {
return pre + offset + "(" + pad + ")";
}
}
}
return null;
}
var f, x = formatDecimal("13.0123123123"); // = 13.0(123)
if (x !== null) {
f = new Fraction(x);
}
```
Attributes
===
The Fraction object allows direct access to the numerator, denominator and sign attributes. It is ensured that only the sign-attribute holds sign information so that a sign comparision is only necessary against this attribute.
```javascript
var f = new Fraction('-1/2');
console.log(f.n); // Numerator: 1
console.log(f.d); // Denominator: 2
console.log(f.s); // Sign: -1
```
Functions
===
Fraction abs()
---
Returns the actual number without any sign information
Fraction neg()
---
Returns the actual number with flipped sign in order to get the additive inverse
Fraction add(n)
---
Returns the sum of the actual number and the parameter n
Fraction sub(n)
---
Returns the difference of the actual number and the parameter n
Fraction mul(n)
---
Returns the product of the actual number and the parameter n
Fraction div(n)
---
Returns the quotient of the actual number and the parameter n
Fraction pow(exp)
---
Returns the power of the actual number, raised to an integer exponent.
*Note:* Rational exponents are planned, but would slow down the function a lot, because of a kinda slow root finding algorithm, whether the result will become irrational. So for now, only integer exponents are implemented.
Fraction mod(n)
---
Returns the modulus (rest of the division) of the actual object and n (this % n). It's a much more precise [fmod()](#fmod-impreciseness-circumvented) if you will. Please note that *mod()* is just like the modulo operator of most programming languages. If you want a mathematical correct modulo, see [here](#mathematical-correct-modulo).
Fraction mod()
---
Returns the modulus (rest of the division) of the actual object (numerator mod denominator)
Fraction gcd(n)
---
Returns the fractional greatest common divisor
Fraction lcm(n)
---
Returns the fractional least common multiple
Fraction ceil([places=0])
---
Returns the ceiling of a rational number (rounded up)
Fraction floor([places=0])
---
Returns the floor of a rational number (rounded down)
Fraction round([places=0])
---
Returns the rational number rounded (normal round)
Fraction inverse()
---
Returns the multiplicative inverse of the actual number (n / d becomes d / n) in order to get the reciprocal
boolean equals(n)
---
Check if two numbers are equal
boolean compare(n)
---
Compare two numbers.
```
result < 0: n is greater than actual number
result > 0: n is smaller than actual number
result = 0: n is equal to the actual number
```
boolean divisible(n)
---
Check if two numbers are divisible (n divides this)
double valueOf()
---
Returns a decimal representation of the fraction
String toString()
---
Generates an exact string representation of the actual object, including repeating decimal places of any length.
**Note:** As `valueOf()` and `toString()` are provided, `toString()` is only called implicitly in a real string context. Using the plus-operator like `"123" + new Fraction` will call valueOf(), because JavaScript tries to combine two primitives first and concatenates them later, as string will be the more dominant type. `alert(new Fraction)` or `String(new Fraction)` on the other hand will do what you expect. If you really want to have control, you should call `toString()` or `valueOf()` explicitly!
String toLatex(excludeWhole=false)
---
Generates an exact LaTeX representation of the actual object. You can see a [live demo](http://www.xarg.org/2014/03/precise-calculations-in-javascript/) on my blog.
The optional boolean parameter indicates if you want to exclude the whole part. "1 1/3" instead of "4/3"
String toFraction(excludeWhole=false)
---
Gets a string representation of the fraction
The optional boolean parameter indicates if you want to exclude the whole part. "1 1/3" instead of "4/3"
Array toContinued()
---
Gets an array of the fraction represented as a continued fraction. The first element always contains the whole part.
```javascript
var f = new Fraction('88/33');
var c = f.toContinued(); // [2, 1, 2]
```
Fraction clone()
---
Creates a copy of the actual Fraction object
Options
===
The library should work without configuring anything. However, there is one global option:
```javascript
Fraction.REDUCE = <true|false>
```
It tells Fraction.js whether to reduce the fraction or not.
```javascript
// Normal behavior
var f = Fraction(3, 6);
console.log(f); // 1/2
// Disable fraction reduction
Fraction.REDUCE = false;
var g = Fraction(3, 6);
console.log(g); // 3/6
// Back to normal behavior
Fraction.REDUCE = true;
var h = Fraction(g);
console.log(h); // 1/2
```
Exceptions
===
If a really hard error occurs (parsing error, division by zero), *fraction.js* throws exceptions! Please make sure you handle them correctly.
Installation
===
Installing fraction.js is as easy as cloning this repo or use one of the following commands:
```
bower install fraction.js
```
or
```
npm install --save fraction.js
```
Using Fraction.js with the browser
===
```html
<script src="fraction.js"></script>
<script>
console.log(Fraction("123/456"));
</script>
```
Using Fraction.js with require.js
===
```html
<script src="require.js"></script>
<script>
requirejs(['fraction.js'],
function(Fraction) {
console.log(Fraction("123/456"));
});
</script>
```
Coding Style
===
As every library I publish, fraction.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.
Precision
===
Fraction.js tries to circumvent floating point errors, by having an internal representation of numerator and denominator. As it relies on JavaScript, there is also a limit. The biggest number representable is `|Number.MAX_SAFE_INTEGER / 1|` and the smallest is `|1 / Number.MAX_SAFE_INTEGER|`, with `Number.MAX_SAFE_INTEGER=9007199254740991`.
Testing
===
If you plan to enhance the library, make sure you add test cases and all the previous tests are passing. You can test the library with
```
npm test
```
Copyright and licensing
===
Copyright (c) 2015, Robert Eisele (robert@xarg.org)
Dual licensed under the MIT or GPL Version 2 licenses.

View File

@@ -0,0 +1,31 @@
{
"name": "fraction.js",
"main": "fraction.js",
"version": "4.0.0",
"homepage": "http://www.xarg.org/2014/03/rational-numbers-in-javascript/",
"description": "A rational number library",
"keywords": [
"math", "fraction", "rational", "number", "parser", "rational numbers"
],
"moduleType": [
"amd",
"globals",
"node"
],
"authors": [
"Robert Eisele <robert@xarg.org> (http://www.xarg.org/)"
],
"license": [
"MIT",
"GPL"
],
"repository": {
"type": "git",
"url": "git://github.com/infusion/Fraction.js.git"
},
"ignore": [
"tests",
".travis.yml",
"package.json"
]
}

View File

@@ -0,0 +1,53 @@
/**
* @license Fraction.js v2.7.0 01/06/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
// Another rational approximation, not using Farey Sequences but Binary Search using the mediant
function approximate(p, precision) {
var num1 = Math.floor(p);
var den1 = 1;
var num2 = num1 + 1;
var den2 = 1;
if (p !== num1) {
while (den1 <= precision && den2 <= precision) {
var m = (num1 + num2) / (den1 + den2);
if (p === m) {
if (den1 + den2 <= precision) {
den1 += den2;
num1 += num2;
den2 = precision + 1;
} else if (den1 > den2) {
den2 = precision + 1;
} else {
den1 = precision + 1;
}
break;
} else if (p < m) {
num2 += num1;
den2 += den1;
} else {
num1 += num2;
den1 += den2;
}
}
}
if (den1 > precision) {
den1 = den2;
num1 = num2;
}
return new Fraction(num1, den1);
}

View File

@@ -0,0 +1,23 @@
/**
* @license Fraction.js v2.7.0 01/06/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
// Based on http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fractions/egyptian.html
function egyptian(a, b) {
var res = [];
do {
var t = Math.ceil(b / a);
var x = new Fraction(a, b).sub(1, t);
res.push(t);
a = x.n;
b = x.d;
} while (a !== 0);
return res;
}
console.log("1 / " + egyptian(521, 1050).join(" + 1 / "));

View File

@@ -0,0 +1,112 @@
/**
* @license Fraction.js v2.4.1 01/06/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
var Fraction = require('../fraction.min.js');
/*
We have the polynom f(x) = 1/3x_1^2 + x_2^2 + x_1 * x_2 + 3
The gradient of f(x):
grad(x) = | x_1^2+x_2 |
| 2x_2+x_1 |
And thus the Hesse-Matrix H:
| 2x_1 1 |
| 1 2 |
The inverse Hesse-Matrix H^-1 is
| -2 / (1-4x_1) 1 / (1 - 4x_1) |
| 1 / (1 - 4x_1) -2x_1 / (1 - 4x_1) |
We now want to find lim ->oo x[n], with the starting element of (3 2)^T
*/
// Get the Hesse Matrix
function H(x) {
var z = new Fraction(1).sub(new Fraction(4).mul(x[0]));
return [
new Fraction(-2).div(z),
new Fraction(1).div(z),
new Fraction(1).div(z),
new Fraction(-2).mul(x[0]).div(z),
];
}
// Get the gradient of f(x)
function grad(x) {
return [
new Fraction(x[0]).mul(x[0]).add(x[1]),
new Fraction(2).mul(x[1]).add(x[0])
];
}
// A simple matrix multiplication helper
function matrMult(m, v) {
return [
new Fraction(m[0]).mul(v[0]).add(new Fraction(m[1]).mul(v[1])),
new Fraction(m[2]).mul(v[0]).add(new Fraction(m[3]).mul(v[1]))
];
}
// A simple vector subtraction helper
function vecSub(a, b) {
return [
new Fraction(a[0]).sub(b[0]),
new Fraction(a[1]).sub(b[1])
];
}
// Main function, gets a vector and the actual index
function run(V, j) {
var t = H(V);
//console.log("H(X)");
for (var i in t) {
// console.log(t[i].toFraction());
}
var s = grad(V);
//console.log("vf(X)");
for (var i in s) {
// console.log(s[i].toFraction());
}
//console.log("multiplikation");
var r = matrMult(t, s);
for (var i in r) {
// console.log(r[i].toFraction());
}
var R = (vecSub(V, r));
console.log("X"+j);
console.log(R[0].toFraction(), "= "+R[0].valueOf());
console.log(R[1].toFraction(), "= "+R[1].valueOf());
console.log("\n");
return R;
}
// Set the starting vector
var v = [3, 2];
for (var i = 0; i < 15; i++) {
v = run(v, i);
}

View File

@@ -0,0 +1,68 @@
/**
* @license Fraction.js v2.7.0 01/06/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
// NOTE: This is a nice example, but a stable version of this is served with Polynomial.js:
// https://github.com/infusion/Polynomial.js
var Fraction = require('../fraction.min.js');
function integrate(poly) {
poly = poly.replace(/\s+/g, "");
var regex = /(\([+-]?[0-9/]+\)|[+-]?[0-9/]+)x(?:\^(\([+-]?[0-9/]+\)|[+-]?[0-9]+))?/g;
var arr;
var res = {};
while (null !== (arr = regex.exec(poly))) {
var a = (arr[1] || "1").replace("(", "").replace(")", "").split("/");
var b = (arr[2] || "1").replace("(", "").replace(")", "").split("/");
var exp = new Fraction(b).add(1);
var key = "" + exp;
if (res[key] !== undefined) {
res[key] = {x: new Fraction(a).div(exp).add(res[key].x), e: exp};
} else {
res[key] = {x: new Fraction(a).div(exp), e: exp};
}
}
var str = "";
var c = 0;
for (var i in res) {
if (res[i].x.s !== -1 && c > 0) {
str += ("+");
} else if (res[i].x.s === -1) {
str += ("-");
}
if (res[i].x.n / res[i].x.d !== 1) {
if (res[i].x.d !== 1) {
str += ("" + res[i].x.n + "/" + res[i].x.d + "");
} else {
str += ("" + res[i].x.n);
}
}
str += ("x");
if (res[i].e.n / res[i].e.d !== 1) {
str += ("^");
if (res[i].e.d !== 1) {
str += ("(" + res[i].e.n + "/" + res[i].e.d + ")");
} else {
str += ("" + res[i].e.n);
}
}
c++;
}
return str;
}
var poly = "-2/3x^3-2x^2+3x+8x^3-1/3x^(4/8)";
console.log("f(x): " + poly);
console.log("F(x): " + integrate(poly));

View File

@@ -0,0 +1,797 @@
/**
* @license Fraction.js v4.0.0 09/09/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
/**
*
* This class offers the possibility to calculate fractions.
* You can pass a fraction in different formats. Either as array, as double, as string or as an integer.
*
* Array/Object form
* [ 0 => <nominator>, 1 => <denominator> ]
* [ n => <nominator>, d => <denominator> ]
*
* Integer form
* - Single integer value
*
* Double form
* - Single double value
*
* String form
* 123.456 - a simple double
* 123/456 - a string fraction
* 123.'456' - a double with repeating decimal places
* 123.(456) - synonym
* 123.45'6' - a double with repeating last place
* 123.45(6) - synonym
*
* Example:
*
* var f = new Fraction("9.4'31'");
* f.mul([-4, 3]).div(4.9);
*
*/
(function(root) {
"use strict";
// Maximum search depth for cyclic rational numbers. 2000 should be more than enough.
// Example: 1/7 = 0.(142857) has 6 repeating decimal places.
// If MAX_CYCLE_LEN gets reduced, long cycles will not be detected and toString() only gets the first 10 digits
var MAX_CYCLE_LEN = 2000;
// Parsed data to avoid calling "new" all the time
var P = {
"s": 1,
"n": 0,
"d": 1
};
function createError(name) {
var errorConstructor = function() {
var temp = Error.apply(this, arguments);
temp.name = this.name = name;
this.stack = temp.stack;
this.message = temp.message;
}
var IntermediateInheritor = function() {};
IntermediateInheritor.prototype = Error.prototype;
errorConstructor.prototype = new IntermediateInheritor();
return errorConstructor;
}
var DivisionByZero = Fraction['DivisionByZero'] = createError('DivisionByZero');
var InvalidParameter = Fraction['InvalidParameter'] = createError('InvalidParameter');
function assign(n, s) {
if (isNaN(n = parseInt(n, 10))) {
throwInvalidParam();
}
return n * s;
}
function throwInvalidParam() {
throw new InvalidParameter();
}
var parse = function(p1, p2) {
var n = 0, d = 1, s = 1;
var v = 0, w = 0, x = 0, y = 1, z = 1;
var A = 0, B = 1;
var C = 1, D = 1;
var N = 10000000;
var M;
if (p1 === undefined || p1 === null) {
/* void */
} else if (p2 !== undefined) {
n = p1;
d = p2;
s = n * d;
} else
switch (typeof p1) {
case "object":
{
if ("d" in p1 && "n" in p1) {
n = p1["n"];
d = p1["d"];
if ("s" in p1)
n*= p1["s"];
} else if (0 in p1) {
n = p1[0];
if (1 in p1)
d = p1[1];
} else {
throwInvalidParam();
}
s = n * d;
break;
}
case "number":
{
if (p1 < 0) {
s = p1;
p1 = -p1;
}
if (p1 % 1 === 0) {
n = p1;
} else if (p1 > 0) { // check for != 0, scale would become NaN (log(0)), which converges really slow
if (p1 >= 1) {
z = Math.pow(10, Math.floor(1 + Math.log(p1) / Math.LN10));
p1/= z;
}
// Using Farey Sequences
// http://www.johndcook.com/blog/2010/10/20/best-rational-approximation/
while (B <= N && D <= N) {
M = (A + C) / (B + D);
if (p1 === M) {
if (B + D <= N) {
n = A + C;
d = B + D;
} else if (D > B) {
n = C;
d = D;
} else {
n = A;
d = B;
}
break;
} else {
if (p1 > M) {
A+= C;
B+= D;
} else {
C+= A;
D+= B;
}
if (B > N) {
n = C;
d = D;
} else {
n = A;
d = B;
}
}
}
n*= z;
} else if (isNaN(p1) || isNaN(p2)) {
d = n = NaN;
}
break;
}
case "string":
{
B = p1.match(/\d+|./g);
if (B[A] === '-') {// Check for minus sign at the beginning
s = -1;
A++;
} else if (B[A] === '+') {// Check for plus sign at the beginning
A++;
}
if (B.length === A + 1) { // Check if it's just a simple number "1234"
w = assign(B[A++], s);
} else if (B[A + 1] === '.' || B[A] === '.') { // Check if it's a decimal number
if (B[A] !== '.') { // Handle 0.5 and .5
v = assign(B[A++], s);
}
A++;
// Check for decimal places
if (A + 1 === B.length || B[A + 1] === '(' && B[A + 3] === ')' || B[A + 1] === "'" && B[A + 3] === "'") {
w = assign(B[A], s);
y = Math.pow(10, B[A].length);
A++;
}
// Check for repeating places
if (B[A] === '(' && B[A + 2] === ')' || B[A] === "'" && B[A + 2] === "'") {
x = assign(B[A + 1], s);
z = Math.pow(10, B[A + 1].length) - 1;
A+= 3;
}
} else if (B[A + 1] === '/' || B[A + 1] === ':') { // Check for a simple fraction "123/456" or "123:456"
w = assign(B[A], s);
y = assign(B[A + 2], 1);
A+= 3;
} else if (B[A + 3] === '/' && B[A + 1] === ' ') { // Check for a complex fraction "123 1/2"
v = assign(B[A], s);
w = assign(B[A + 2], s);
y = assign(B[A + 4], 1);
A+= 5;
}
if (B.length <= A) { // Check for more tokens on the stack
d = y * z;
s = /* void */
n = x + d * v + z * w;
break;
}
/* Fall through on error */
}
default:
throwInvalidParam();
}
if (d === 0) {
throw new DivisionByZero();
}
P["s"] = s < 0 ? -1 : 1;
P["n"] = Math.abs(n);
P["d"] = Math.abs(d);
};
var modpow = function(b, e, m) {
for (var r = 1; e > 0; b = (b * b) % m, e >>= 1) {
if (e & 1) {
r = (r * b) % m;
}
}
return r;
};
var cycleLen = function(n, d) {
for (; d % 2 === 0;
d/= 2) {}
for (; d % 5 === 0;
d/= 5) {}
if (d === 1) // Catch non-cyclic numbers
return 0;
// If we would like to compute really large numbers quicker, we could make use of Fermat's little theorem:
// 10^(d-1) % d == 1
// However, we don't need such large numbers and MAX_CYCLE_LEN should be the capstone,
// as we want to translate the numbers to strings.
var rem = 10 % d;
for (var t = 1; rem !== 1; t++) {
rem = rem * 10 % d;
if (t > MAX_CYCLE_LEN)
return 0; // Returning 0 here means that we don't print it as a cyclic number. It's likely that the answer is `d-1`
}
return t;
};
var cycleStart = function(n, d, len) {
var rem1 = 1;
var rem2 = modpow(10, len, d);
for (var t = 0; t < 300; t++) { // s < ~log10(Number.MAX_VALUE)
// Solve 10^s == 10^(s+t) (mod d)
if (rem1 === rem2)
return t;
rem1 = rem1 * 10 % d;
rem2 = rem2 * 10 % d;
}
return 0;
};
var gcd = function(a, b) {
if (!a) return b;
if (!b) return a;
while (1) {
a%= b;
if (!a) return b;
b%= a;
if (!b) return a;
}
};
/**
* Module constructor
*
* @constructor
* @param {number|Fraction} a
* @param {number=} b
*/
function Fraction(a, b) {
if (!(this instanceof Fraction)) {
return new Fraction(a, b);
}
parse(a, b);
if (Fraction['REDUCE']) {
a = gcd(P["d"], P["n"]); // Abuse a
} else {
a = 1;
}
this["s"] = P["s"];
this["n"] = P["n"] / a;
this["d"] = P["d"] / a;
}
/**
* Boolean global variable to be able to disable automatic reduction of the fraction
*
*/
Fraction['REDUCE'] = 1;
Fraction.prototype = {
"s": 1,
"n": 0,
"d": 1,
/**
* Calculates the absolute value
*
* Ex: new Fraction(-4).abs() => 4
**/
"abs": function() {
return new Fraction(this["n"], this["d"]);
},
/**
* Inverts the sign of the current fraction
*
* Ex: new Fraction(-4).neg() => 4
**/
"neg": function() {
return new Fraction(-this["s"] * this["n"], this["d"]);
},
/**
* Adds two rational numbers
*
* Ex: new Fraction({n: 2, d: 3}).add("14.9") => 467 / 30
**/
"add": function(a, b) {
parse(a, b);
return new Fraction(
this["s"] * this["n"] * P["d"] + P["s"] * this["d"] * P["n"],
this["d"] * P["d"]
);
},
/**
* Subtracts two rational numbers
*
* Ex: new Fraction({n: 2, d: 3}).add("14.9") => -427 / 30
**/
"sub": function(a, b) {
parse(a, b);
return new Fraction(
this["s"] * this["n"] * P["d"] - P["s"] * this["d"] * P["n"],
this["d"] * P["d"]
);
},
/**
* Multiplies two rational numbers
*
* Ex: new Fraction("-17.(345)").mul(3) => 5776 / 111
**/
"mul": function(a, b) {
parse(a, b);
return new Fraction(
this["s"] * P["s"] * this["n"] * P["n"],
this["d"] * P["d"]
);
},
/**
* Divides two rational numbers
*
* Ex: new Fraction("-17.(345)").inverse().div(3)
**/
"div": function(a, b) {
parse(a, b);
return new Fraction(
this["s"] * P["s"] * this["n"] * P["d"],
this["d"] * P["n"]
);
},
/**
* Clones the actual object
*
* Ex: new Fraction("-17.(345)").clone()
**/
"clone": function() {
return new Fraction(this);
},
/**
* Calculates the modulo of two rational numbers - a more precise fmod
*
* Ex: new Fraction('4.(3)').mod([7, 8]) => (13/3) % (7/8) = (5/6)
**/
"mod": function(a, b) {
if (isNaN(this['n']) || isNaN(this['d'])) {
return new Fraction(NaN);
}
if (a === undefined) {
return new Fraction(this["s"] * this["n"] % this["d"], 1);
}
parse(a, b);
if (0 === P["n"] && 0 === this["d"]) {
Fraction(0, 0); // Throw DivisionByZero
}
/*
* First silly attempt, kinda slow
*
return that["sub"]({
"n": num["n"] * Math.floor((this.n / this.d) / (num.n / num.d)),
"d": num["d"],
"s": this["s"]
});*/
/*
* New attempt: a1 / b1 = a2 / b2 * q + r
* => b2 * a1 = a2 * b1 * q + b1 * b2 * r
* => (b2 * a1 % a2 * b1) / (b1 * b2)
*/
return new Fraction(
(this["s"] * P["d"] * this["n"]) % (P["n"] * this["d"]),
P["d"] * this["d"]
);
},
/**
* Calculates the fractional gcd of two rational numbers
*
* Ex: new Fraction(5,8).gcd(3,7) => 1/56
*/
"gcd": function(a, b) {
parse(a, b);
// gcd(a / b, c / d) = gcd(a, c) / lcm(b, d)
return new Fraction(gcd(P["n"], this["n"]), P["d"] * this["d"] / gcd(P["d"], this["d"]));
},
/**
* Calculates the fractional lcm of two rational numbers
*
* Ex: new Fraction(5,8).lcm(3,7) => 15
*/
"lcm": function(a, b) {
parse(a, b);
// lcm(a / b, c / d) = lcm(a, c) / gcd(b, d)
if (P["n"] === 0 && this["n"] === 0) {
return new Fraction;
}
return new Fraction(P["n"] * this["n"] / gcd(P["n"], this["n"]), gcd(P["d"], this["d"]));
},
/**
* Calculates the ceil of a rational number
*
* Ex: new Fraction('4.(3)').ceil() => (5 / 1)
**/
"ceil": function(places) {
places = Math.pow(10, places || 0);
if (isNaN(this["n"]) || isNaN(this["d"])) {
return new Fraction(NaN);
}
return new Fraction(Math.ceil(places * this["s"] * this["n"] / this["d"]), places);
},
/**
* Calculates the floor of a rational number
*
* Ex: new Fraction('4.(3)').floor() => (4 / 1)
**/
"floor": function(places) {
places = Math.pow(10, places || 0);
if (isNaN(this["n"]) || isNaN(this["d"])) {
return new Fraction(NaN);
}
return new Fraction(Math.floor(places * this["s"] * this["n"] / this["d"]), places);
},
/**
* Rounds a rational numbers
*
* Ex: new Fraction('4.(3)').round() => (4 / 1)
**/
"round": function(places) {
places = Math.pow(10, places || 0);
if (isNaN(this["n"]) || isNaN(this["d"])) {
return new Fraction(NaN);
}
return new Fraction(Math.round(places * this["s"] * this["n"] / this["d"]), places);
},
/**
* Gets the inverse of the fraction, means numerator and denumerator are exchanged
*
* Ex: new Fraction([-3, 4]).inverse() => -4 / 3
**/
"inverse": function() {
return new Fraction(this["s"] * this["d"], this["n"]);
},
/**
* Calculates the fraction to some integer exponent
*
* Ex: new Fraction(-1,2).pow(-3) => -8
*/
"pow": function(m) {
if (m < 0) {
return new Fraction(Math.pow(this['s'] * this["d"],-m), Math.pow(this["n"],-m));
} else {
return new Fraction(Math.pow(this['s'] * this["n"], m), Math.pow(this["d"], m));
}
},
/**
* Check if two rational numbers are the same
*
* Ex: new Fraction(19.6).equals([98, 5]);
**/
"equals": function(a, b) {
parse(a, b);
return this["s"] * this["n"] * P["d"] === P["s"] * P["n"] * this["d"]; // Same as compare() === 0
},
/**
* Check if two rational numbers are the same
*
* Ex: new Fraction(19.6).equals([98, 5]);
**/
"compare": function(a, b) {
parse(a, b);
var t = (this["s"] * this["n"] * P["d"] - P["s"] * P["n"] * this["d"]);
return (0 < t) - (t < 0);
},
/**
* Check if two rational numbers are divisible
*
* Ex: new Fraction(19.6).divisible(1.5);
*/
"divisible": function(a, b) {
parse(a, b);
return !(!(P["n"] * this["d"]) || ((this["n"] * P["d"]) % (P["n"] * this["d"])));
},
/**
* Returns a decimal representation of the fraction
*
* Ex: new Fraction("100.'91823'").valueOf() => 100.91823918239183
**/
'valueOf': function() {
return this["s"] * this["n"] / this["d"];
},
/**
* Returns a string-fraction representation of a Fraction object
*
* Ex: new Fraction("1.'3'").toFraction() => "4 1/3"
**/
'toFraction': function(excludeWhole) {
var whole, str = "";
var n = this["n"];
var d = this["d"];
if (this["s"] < 0) {
str+= '-';
}
if (d === 1) {
str+= n;
} else {
if (excludeWhole && (whole = Math.floor(n / d)) > 0) {
str+= whole;
str+= " ";
n%= d;
}
str+= n;
str+= '/';
str+= d;
}
return str;
},
/**
* Returns a latex representation of a Fraction object
*
* Ex: new Fraction("1.'3'").toLatex() => "\frac{4}{3}"
**/
'toLatex': function(excludeWhole) {
var whole, str = "";
var n = this["n"];
var d = this["d"];
if (this["s"] < 0) {
str+= '-';
}
if (d === 1) {
str+= n;
} else {
if (excludeWhole && (whole = Math.floor(n / d)) > 0) {
str+= whole;
n%= d;
}
str+= "\\frac{";
str+= n;
str+= '}{';
str+= d;
str+= '}';
}
return str;
},
/**
* Returns an array of continued fraction elements
*
* Ex: new Fraction("7/8").toContinued() => [0,1,7]
*/
'toContinued': function() {
var t;
var a = this['n'];
var b = this['d'];
var res = [];
do {
res.push(Math.floor(a / b));
t = a % b;
a = b;
b = t;
} while (a !== 1);
return res;
},
/**
* Creates a string representation of a fraction with all digits
*
* Ex: new Fraction("100.'91823'").toString() => "100.(91823)"
**/
'toString': function() {
var g;
var N = this["n"];
var D = this["d"];
if (isNaN(N) || isNaN(D)) {
return "NaN";
}
if (!Fraction['REDUCE']) {
g = gcd(N, D);
N/= g;
D/= g;
}
var p = String(N).split(""); // Numerator chars
var t = 0; // Tmp var
var ret = [~this["s"] ? "" : "-", "", ""]; // Return array, [0] is zero sign, [1] before comma, [2] after
var zeros = ""; // Collection variable for zeros
var cycLen = cycleLen(N, D); // Cycle length
var cycOff = cycleStart(N, D, cycLen); // Cycle start
var j = -1;
var n = 1; // str index
// rough estimate to fill zeros
var length = 15 + cycLen + cycOff + p.length; // 15 = decimal places when no repitation
for (var i = 0; i < length; i++, t*= 10) {
if (i < p.length) {
t+= Number(p[i]);
} else {
n = 2;
j++; // Start now => after comma
}
if (cycLen > 0) { // If we have a repeating part
if (j === cycOff) {
ret[n]+= zeros + "(";
zeros = "";
} else if (j === cycLen + cycOff) {
ret[n]+= zeros + ")";
break;
}
}
if (t >= D) {
ret[n]+= zeros + ((t / D) | 0); // Flush zeros, Add current digit
zeros = "";
t = t % D;
} else if (n > 1) { // Add zeros to the zero buffer
zeros+= "0";
} else if (ret[n]) { // If before comma, add zero only if already something was added
ret[n]+= "0";
}
}
// If it's empty, it's a leading zero only
ret[0]+= ret[1] || "0";
// If there is something after the comma, add the comma sign
if (ret[2]) {
return ret[0] + "." + ret[2];
}
return ret[0];
}
};
if (typeof define === "function" && define["amd"]) {
define([], function() {
return Fraction;
});
} else if (typeof exports === "object") {
module["exports"] = Fraction;
} else {
root['Fraction'] = Fraction;
}
})(this);

View File

@@ -0,0 +1,17 @@
/*
Fraction.js v4.0.0 09/09/2015
http://www.xarg.org/2014/03/rational-numbers-in-javascript/
Copyright (c) 2015, Robert Eisele (robert@xarg.org)
Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function(w){function q(a,c){if(!a)return c;if(!c)return a;for(;;){a%=c;if(!a)return c;c%=a;if(!c)return a}}function x(a){for(;0===a%2;a/=2);for(;0===a%5;a/=5);if(1===a)return 0;for(var c=10%a,d=1;1!==c;d++)if(c=10*c%a,2E3<d)return 0;return d}function k(a,c){var d=0,l=1,e=1,k=0,m=0,n=0,h=1,r=1,f=0,g=1,q=1,t=1;if(void 0!==a&&null!==a)if(void 0!==c)d=a,l=c,e=d*l;else switch(typeof a){case "object":if("d"in a&&"n"in a)d=a.n,l=a.d,"s"in a&&(d*=a.s);else if(0 in a)d=a[0],1 in a&&(l=a[1]);else throw new u;
e=d*l;break;case "number":0>a&&(e=a,a=-a);if(0===a%1)d=a;else if(0<a){1<=a&&(r=Math.pow(10,Math.floor(1+Math.log(a)/Math.LN10)),a/=r);for(;1E7>=g&&1E7>=t;)if(d=(f+q)/(g+t),a===d){1E7>=g+t?(d=f+q,l=g+t):t>g?(d=q,l=t):(d=f,l=g);break}else a>d?(f+=q,g+=t):(q+=f,t+=g),1E7<g?(d=q,l=t):(d=f,l=g);d*=r}else if(isNaN(a)||isNaN(c))l=d=NaN;break;case "string":g=a.match(/\d+|./g);"-"===g[f]?(e=-1,f++):"+"===g[f]&&f++;if(g.length===f+1)m=p(g[f++],e);else if("."===g[f+1]||"."===g[f]){"."!==g[f]&&(k=p(g[f++],e));
f++;if(f+1===g.length||"("===g[f+1]&&")"===g[f+3]||"'"===g[f+1]&&"'"===g[f+3])m=p(g[f],e),h=Math.pow(10,g[f].length),f++;if("("===g[f]&&")"===g[f+2]||"'"===g[f]&&"'"===g[f+2])n=p(g[f+1],e),r=Math.pow(10,g[f+1].length)-1,f+=3}else"/"===g[f+1]||":"===g[f+1]?(m=p(g[f],e),h=p(g[f+2],1),f+=3):"/"===g[f+3]&&" "===g[f+1]&&(k=p(g[f],e),m=p(g[f+2],e),h=p(g[f+4],1),f+=5);if(g.length<=f){l=h*r;e=d=n+l*k+r*m;break}default:throw new u;}if(0===l)throw new y;b.s=0>e?-1:1;b.n=Math.abs(d);b.d=Math.abs(l)}function v(a){function c(){}
function d(){var c=Error.apply(this,arguments);c.name=this.name=a;this.stack=c.stack;this.message=c.message}c.prototype=Error.prototype;d.prototype=new c;return d}function p(a,c){if(isNaN(a=parseInt(a,10)))throw new u;return a*c}function e(a,c){if(!(this instanceof e))return new e(a,c);k(a,c);a=e.REDUCE?q(b.d,b.n):1;this.s=b.s;this.n=b.n/a;this.d=b.d/a}var b={s:1,n:0,d:1},y=e.DivisionByZero=v("DivisionByZero"),u=e.InvalidParameter=v("InvalidParameter");e.REDUCE=1;e.prototype={s:1,n:0,d:1,abs:function(){return new e(this.n,
this.d)},neg:function(){return new e(-this.s*this.n,this.d)},add:function(a,c){k(a,c);return new e(this.s*this.n*b.d+b.s*this.d*b.n,this.d*b.d)},sub:function(a,c){k(a,c);return new e(this.s*this.n*b.d-b.s*this.d*b.n,this.d*b.d)},mul:function(a,c){k(a,c);return new e(this.s*b.s*this.n*b.n,this.d*b.d)},div:function(a,c){k(a,c);return new e(this.s*b.s*this.n*b.d,this.d*b.n)},clone:function(){return new e(this)},mod:function(a,c){if(isNaN(this.n)||isNaN(this.d))return new e(NaN);if(void 0===a)return new e(this.s*
this.n%this.d,1);k(a,c);0===b.n&&0===this.d&&e(0,0);return new e(this.s*b.d*this.n%(b.n*this.d),b.d*this.d)},gcd:function(a,c){k(a,c);return new e(q(b.n,this.n),b.d*this.d/q(b.d,this.d))},lcm:function(a,c){k(a,c);return 0===b.n&&0===this.n?new e:new e(b.n*this.n/q(b.n,this.n),q(b.d,this.d))},ceil:function(a){a=Math.pow(10,a||0);return isNaN(this.n)||isNaN(this.d)?new e(NaN):new e(Math.ceil(a*this.s*this.n/this.d),a)},floor:function(a){a=Math.pow(10,a||0);return isNaN(this.n)||isNaN(this.d)?new e(NaN):
new e(Math.floor(a*this.s*this.n/this.d),a)},round:function(a){a=Math.pow(10,a||0);return isNaN(this.n)||isNaN(this.d)?new e(NaN):new e(Math.round(a*this.s*this.n/this.d),a)},inverse:function(){return new e(this.s*this.d,this.n)},pow:function(a){return 0>a?new e(Math.pow(this.s*this.d,-a),Math.pow(this.n,-a)):new e(Math.pow(this.s*this.n,a),Math.pow(this.d,a))},equals:function(a,c){k(a,c);return this.s*this.n*b.d===b.s*b.n*this.d},compare:function(a,c){k(a,c);var d=this.s*this.n*b.d-b.s*b.n*this.d;
return(0<d)-(0>d)},divisible:function(a,c){k(a,c);return!(!(b.n*this.d)||this.n*b.d%(b.n*this.d))},valueOf:function(){return this.s*this.n/this.d},toFraction:function(a){var c,d="",e=this.n,b=this.d;0>this.s&&(d+="-");1===b?d+=e:(a&&0<(c=Math.floor(e/b))&&(d=d+c+" ",e%=b),d=d+e+"/",d+=b);return d},toLatex:function(a){var c,d="",e=this.n,b=this.d;0>this.s&&(d+="-");1===b?d+=e:(a&&0<(c=Math.floor(e/b))&&(d+=c,e%=b),d=d+"\\frac{"+e+"}{"+b,d+="}");return d},toContinued:function(){var a,c=this.n,d=this.d,
e=[];do e.push(Math.floor(c/d)),a=c%d,c=d,d=a;while(1!==c);return e},toString:function(){var a,c=this.n,d=this.d;if(isNaN(c)||isNaN(d))return"NaN";e.REDUCE||(a=q(c,d),c/=a,d/=a);a=String(c).split("");var c=0,b=[~this.s?"":"-","",""],k="",p=x(d),m;a:{m=1;var n;n=p;for(var h=10,r=1;0<n;h=h*h%d,n>>=1)n&1&&(r=r*h%d);n=r;for(h=0;300>h;h++){if(m===n){m=h;break a}m=10*m%d;n=10*n%d}m=0}n=-1;for(var h=1,r=15+p+m+a.length,f=0;f<r;f++,c*=10){f<a.length?c+=Number(a[f]):(h=2,n++);if(0<p)if(n===m)b[h]+=k+"(",k=
"";else if(n===p+m){b[h]+=k+")";break}c>=d?(b[h]+=k+(c/d|0),k="",c%=d):1<h?k+="0":b[h]&&(b[h]+="0")}b[0]+=b[1]||"0";return b[2]?b[0]+"."+b[2]:b[0]}};"function"===typeof define&&define.amd?define([],function(){return e}):"object"===typeof exports?module.exports=e:w.Fraction=e})(this);

View File

@@ -0,0 +1,68 @@
{
"_from": "fraction.js@4.0.0",
"_id": "fraction.js@4.0.0",
"_inBundle": false,
"_integrity": "sha1-c5dOL4tR73CVNtYkzJB4Liu2EnQ=",
"_location": "/fraction.js",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "fraction.js@4.0.0",
"name": "fraction.js",
"escapedName": "fraction.js",
"rawSpec": "4.0.0",
"saveSpec": null,
"fetchSpec": "4.0.0"
},
"_requiredBy": [
"/mathjs"
],
"_resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.0.0.tgz",
"_shasum": "73974e2f8b51ef709536d624cc90782e2bb61274",
"_spec": "fraction.js@4.0.0",
"_where": "/data/node_modules/mathjs",
"author": {
"name": "Robert Eisele",
"email": "robert@xarg.org",
"url": "http://www.xarg.org/"
},
"bugs": {
"url": "https://github.com/infusion/Fraction.js/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "A rational number library",
"devDependencies": {
"mocha": "*"
},
"directories": {
"example": "examples"
},
"engines": {
"node": "*"
},
"homepage": "http://www.xarg.org/2014/03/rational-numbers-in-javascript/",
"keywords": [
"math",
"fraction",
"rational",
"rationals",
"number",
"parser",
"rational numbers"
],
"license": "MIT OR GPL-2.0",
"main": "fraction",
"name": "fraction.js",
"private": false,
"repository": {
"type": "git",
"url": "git://github.com/infusion/Fraction.js.git"
},
"scripts": {
"test": "mocha tests/*.js"
},
"title": "fraction.js",
"version": "4.0.0"
}

View File

@@ -0,0 +1,1448 @@
var assert = require('assert');
var Fraction = require('../fraction');
var tests = [{
set: "foo",
expectError: Fraction.InvalidParameter
}, {
set: " 123",
expectError: Fraction.InvalidParameter
}, {
set: 0,
expect: 0
}, {
set: .2,
expect: "0.2"
}, {
set: .333,
expect: "0.333"
}, {
set: 1.1,
expect: "1.1"
}, {
set: 1.2,
expect: "1.2"
}, {
set: 1.3,
expect: "1.3"
}, {
set: 1.4,
expect: "1.4"
}, {
set: 1.5,
expect: "1.5"
}, {
set: 2.555,
expect: "2.555"
}, {
set: " - ",
expectError: Fraction.InvalidParameter
}, {
set: ".5",
expect: "0.5"
}, {
set: "-.5",
expect: "-0.5"
}, {
set: "123",
expect: "123"
}, {
set: "-123",
expect: "-123"
}, {
set: "123.4",
expect: "123.4"
}, {
set: "-123.4",
expect: "-123.4"
}, {
set: "123.",
expect: "123"
}, {
set: "-123.",
expect: "-123"
}, {
set: "123.4(56)",
expect: "123.4(56)"
}, {
set: "-123.4(56)",
expect: "-123.4(56)"
}, {
set: "123.(4)",
expect: "123.(4)"
}, {
set: "-123.(4)",
expect: "-123.(4)"
}, {
set: "0/0",
expectError: Fraction.DivisionByZero
}, {
set: "9/0",
expectError: Fraction.DivisionByZero
}, {
label: "0/1+0/1",
set: "0/1",
param: "0/1",
expect: "0"
}, {
label: "1/9+0/1",
set: "1/9",
param: "0/1",
expect: "0.(1)"
}, {
set: "123/456",
expect: "0.269(736842105263157894)"
}, {
set: "-123/456",
expect: "-0.269(736842105263157894)"
}, {
set: "19 123/456",
expect: "19.269(736842105263157894)"
}, {
set: "-19 123/456",
expect: "-19.269(736842105263157894)"
}, {
set: "123.(22)123",
expectError: Fraction.InvalidParameter
}, {
set: "+33.3(3)",
expect: "33.(3)"
}, {
set: "3.'09009'",
expect: "3.(09009)"
}, {
set: "123.(((",
expectError: Fraction.InvalidParameter
}, {
set: "123.((",
expectError: Fraction.InvalidParameter
}, {
set: "123.()",
expectError: Fraction.InvalidParameter
}, {
set: null,
expect: "0" // I would say it's just fine
}, {
set: [22, 7],
expect: '3.(142857)' // We got Pi! - almost ;o
}, {
set: "355/113",
expect: "3.(1415929203539823008849557522123893805309734513274336283185840707964601769911504424778761061946902654867256637168)" // Yay, a better PI
}, {
set: "3 1/7",
expect: '3.(142857)'
}, {
set: [36, -36],
expect: "-1"
}, {
set: "9/12",
expect: "0.75"
}, {
set: "0.09(33)",
expect: "0.09(3)"
}, {
set: 1 / 2,
expect: "0.5"
}, {
set: 1 / 3,
expect: "0.(3)"
}, {
set: "0.'3'",
expect: "0.(3)"
}, {
set: "0.00002",
expect: "0.00002"
}, {
set: 7 / 8,
expect: "0.875"
}, {
set: 0.003,
expect: "0.003"
}, {
set: 4,
expect: "4"
}, {
set: -99,
expect: "-99"
}, {
set: "-92332.1192",
expect: "-92332.1192"
}, {
set: '88.92933(12111)',
expect: "88.92933(12111)"
}, {
set: '-192322.823(123)',
expect: "-192322.8(231)"
}, {
label: "-99.12 % 0.09(34)",
set: '-99.12',
fn: "mod",
param: "0.09(34)",
expect: "-0.07(95)"
}, {
label: "0.4 / 0.1",
set: .4,
fn: "div",
param: ".1",
expect: "4"
}, {
label: "1 / -.1",
set: 1,
fn: "div",
param: "-.1",
expect: "-10"
}, {
label: "1 - (-1)",
set: 1,
fn: "sub",
param: "-1",
expect: "2"
}, {
label: "1 + (-1)",
set: 1,
fn: "add",
param: "-1",
expect: "0"
}, {
label: "-187 % 12",
set: '-187',
fn: "mod",
param: "12",
expect: "-7"
}, {
label: "Negate by 99 * -1",
set: '99',
fn: "mul",
param: "-1",
expect: "-99"
}, {
set: [20,-5],
expect: "-4",
fn: "toFraction",
param: true
}, {
set: [-10,-7],
expect: "1 3/7",
fn: "toFraction",
param: true
}, {
set: [21,-6],
expect: "-3 1/2",
fn: "toFraction",
param: true
}, {
set: "10/78",
expect: "5/39",
fn: "toFraction",
param: true
}, {
set: "0/91",
expect: "0",
fn: "toFraction",
param: true
}, {
set: "-0/287",
expect: "0",
fn: "toFraction",
param: true
}, {
set: "-5/20",
expect: "-1/4",
fn: "toFraction",
param: true
}, {
set: "42/9",
expect: "4 2/3",
fn: "toFraction",
param: true
}, {
set: "71/23",
expect: "3 2/23",
fn: "toFraction",
param: true
}, {
set: "6/3",
expect: "2",
fn: "toFraction",
param: true
}, {
set: "28/4",
expect: "7",
fn: "toFraction",
param: true
}, {
set: "105/35",
expect: "3",
fn: "toFraction",
param: true
}, {
set: "4/6",
expect: "2/3",
fn: "toFraction",
param: true
}, {
label: "99.(9) + 66",
set: '99.(999999)',
fn: "add",
param: "66",
expect: "166"
}, {
label: "-82.124 / 66.(3)",
set: '-82.124',
fn: "div",
param: "66.(3)",
expect: "-1.238(050251256281407035175879396984924623115577889447236180904522613065326633165829145728643216080402010)"
}, {
label: "100 - .91",
set: '100',
fn: "sub",
param: ".91",
expect: "99.09"
}, {
label: "381.(33411) % 11.119(356)",
set: '381.(33411)',
fn: "mod",
param: "11.119(356)",
expect: "3.275(997225017295217)"
}, {
label: "13/26 mod 1",
set: '13/26',
fn: "mod",
param: "1.000",
expect: "0.5"
}, {
label: "381.(33411) % 1", // Extract fraction part of a number
set: '381.(33411)',
fn: "mod",
param: "1",
expect: "0.(33411)"
}, {
label: "-222/3",
set: {
n: 3,
d: 222,
s: -1
},
fn: "inverse",
param: null,
expect: "-74"
}, {
label: "inverse",
set: 1 / 2,
fn: "inverse",
param: null,
expect: "2"
}, {
label: "abs(-222/3)",
set: {
n: -222,
d: 3
},
fn: "abs",
param: null,
expect: "74"
}, {
label: "9 % -2",
set: 9,
fn: "mod",
param: "-2",
expect: "1"
}, {
label: "-9 % 2",
set: '-9',
fn: "mod",
param: "-2",
expect: "-1"
}, {
label: "1 / 195312500",
set: '1',
fn: "div",
param: "195312500",
expect: "0.00000000512"
}, {
label: "10 / 0",
set: 10,
fn: "div",
param: 0,
expectError: Fraction.DivisionByZero
}, {
label: "-3 / 4",
set: [-3, 4],
fn: "inverse",
param: null,
expect: "-1.(3)"
}, {
label: "-19.6",
set: [-98, 5],
fn: "equals",
param: '-19.6',
expect: "true" // actually, we get a real bool but we call toString() in the test below
}, {
label: "-19.6",
set: [98, -5],
fn: "equals",
param: '-19.6',
expect: "true"
}, {
label: "99/88",
set: [99, 88],
fn: "equals",
param: [88, 99],
expect: "false"
}, {
label: "99/88",
set: [99, -88],
fn: "equals",
param: [9, 8],
expect: "false"
}, {
label: "12.5",
set: 12.5,
fn: "add",
param: 0,
expect: "12.5"
}, {
label: "0/1 -> 1/0",
set: 0,
fn: "inverse",
param: null,
expectError: Fraction.DivisionByZero
}, {
label: "abs(-100.25)",
set: -100.25,
fn: "abs",
param: null,
expect: "100.25"
}, {
label: "0.022222222",
set: '0.0(22222222)',
fn: "abs",
param: null,
expect: "0.0(2)"
}, {
label: "1.5 | 100.5",
set: 100.5,
fn: "divisible",
param: '1.5',
expect: "true"
}, {
label: "1.5 | 100.6",
set: 100.6,
fn: "divisible",
param: 1.6,
expect: "false"
}, {
label: "(1/6) | (2/3)", // == 4
set: [2, 3],
fn: "divisible",
param: [1, 6],
expect: "true"
}, {
label: "(1/6) | (2/5)",
set: [2, 5],
fn: "divisible",
param: [1, 6],
expect: "false"
}, {
label: "0 | (2/5)",
set: [2, 5],
fn: "divisible",
param: 0,
expect: "false"
}, {
label: "6 | 0",
set: 0,
fn: "divisible",
param: 6,
expect: "true"
}, {
label: "fmod(4.55, 0.05)", // http://phpjs.org/functions/fmod/ (comment section)
set: 4.55,
fn: "mod",
param: 0.05,
expect: "0"
}, {
label: "fmod(99.12, 0.4)",
set: 99.12,
fn: "mod",
param: "0.4",
expect: "0.32"
}, {
label: "fmod(fmod(1.0,0.1))", // http://stackoverflow.com/questions/4218961/why-fmod1-0-0-1-1
set: 1.0,
fn: "mod",
param: 0.1,
expect: "0"
}, {
label: "bignum",
set: [5385020324, 1673196525],
fn: "add",
param: 0,
expect: "3.(218402765927331817761216065160068390651241640607638723132060054929889362518249313241909822876305579226564554334106090735515960983722459021960973771446244188201382978607369507894477607763379738073505740755707103802405996510182807127214180653405313521076073236525518124656635896372065439234641011461579505730804694326029633608042545988433725679653799185364671971213901487154953301137175144443955858681932177692037700114157241630656625945359287666462252543824760812242303694719901477203940523364402755976319039988443676692431571957753139608032594975655952907265331548545978482712901881026796897035152520412986155347172980771042421331827712228842932840779118878459301127224131666183086293464540873344211613157635502500221843336663635492549209065563891247024912390372075390247418784233967973367623387814530633214170702392535748303684768888699431168134896765937282830538988837548536027469935129108638329260216459031911986549219016576669019797300858008894083735919783840096129771725410438561602917505461589456743582467098418101244861239476934725285781955589466694595244871190489712498058170423226285388083745870796617868902160193047257255091418505067717613147684489722449070948195998673855720564564285118868508288349451359277715449474771052372344605485001231400477597812366960300733352288070285108917495510576678970810078630781282551372738477328597129377853566842663625541536431292791502779388093696883574390641290627829865950743592418111195874017249707113753418774282955195594850999346893814520682201392929620147280666866075400198431562006740361835260206508019134213776830548939850326308799)"
}, {
label: "ceil(0.4)",
set: 0.4,
fn: "ceil",
param: null,
expect: "1"
}, {
label: "ceil(0.5)",
set: 0.5,
fn: "ceil",
param: null,
expect: "1"
}, {
label: "ceil(0.23, 2)",
set: 0.23,
fn: "ceil",
param: 2,
expect: "0.23"
}, {
label: "ceil(0.6)",
set: 0.6,
fn: "ceil",
param: null,
expect: "1"
}, {
label: "ceil(-0.4)",
set: -0.4,
fn: "ceil",
param: null,
expect: "0"
}, {
label: "ceil(-0.5)",
set: -0.5,
fn: "ceil",
param: null,
expect: "0"
}, {
label: "ceil(-0.6)",
set: -0.6,
fn: "ceil",
param: null,
expect: "0"
}, {
label: "floor(0.4)",
set: 0.4,
fn: "floor",
param: null,
expect: "0"
}, {
label: "floor(0.4, 1)",
set: 0.4,
fn: "floor",
param: 1,
expect: "0.4"
}, {
label: "floor(0.5)",
set: 0.5,
fn: "floor",
param: null,
expect: "0"
}, {
label: "floor(0.6)",
set: 0.6,
fn: "floor",
param: null,
expect: "0"
}, {
label: "floor(-0.4)",
set: -0.4,
fn: "floor",
param: null,
expect: "-1"
}, {
label: "floor(-0.5)",
set: -0.5,
fn: "floor",
param: null,
expect: "-1"
}, {
label: "floor(-0.6)",
set: -0.6,
fn: "floor",
param: null,
expect: "-1"
}, {
label: "round(0.4)",
set: 0.4,
fn: "round",
param: null,
expect: "0"
}, {
label: "round(0.5)",
set: 0.5,
fn: "round",
param: null,
expect: "1"
}, {
label: "round(0.5, 1)",
set: 0.5,
fn: "round",
param: 1,
expect: "0.5"
}, {
label: "round(0.6)",
set: 0.6,
fn: "round",
param: null,
expect: "1"
}, {
label: "round(-0.4)",
set: -0.4,
fn: "round",
param: null,
expect: "0"
}, {
label: "round(-0.5)",
set: -0.5,
fn: "round",
param: null,
expect: "0"
}, {
label: "round(-0.6)",
set: -0.6,
fn: "round",
param: null,
expect: "-1"
}, {
label: "17402216385200408/5539306332998545",
set: [17402216385200408, 5539306332998545],
fn: "add",
param: 0,
expect: "3.14158765358987"
}, {
label: "99+1",
set: [99, 1],
fn: "add",
param: 1,
expect: "100"
}, {
label: "gcd(5/8, 3/7)",
set: [5, 8],
fn: "gcd",
param: [3, 7],
expect: "0.017(857142)" // == 1/56
}, {
label: "gcd(52, 39)",
set: 52,
fn: "gcd",
param: 39,
expect: "13"
}, {
label: "gcd(51357, 3819)",
set: 51357,
fn: "gcd",
param: 3819,
expect: "57"
}, {
label: "gcd(841, 299)",
set: 841,
fn: "gcd",
param: 299,
expect: "1"
}, {
label: "gcd(2/3, 7/5)",
set: [2, 3],
fn: "gcd",
param: [7, 5],
expect: "0.0(6)" // == 1/15
}, {
label: "lcm(-3, 3)",
set: -3,
fn: "lcm",
param: 3,
expect: "3"
}, {
label: "lcm(3,-3)",
set: 3,
fn: "lcm",
param: -3,
expect: "3"
}, {
label: "lcm(0,3)",
set: 0,
fn: "lcm",
param: 3,
expect: "0"
}, {
label: "lcm(3, 0)",
set: 3,
fn: "lcm",
param: 0,
expect: "0"
}, {
label: "lcm(0, 0)",
set: 0,
fn: "lcm",
param: 0,
expect: "0"
}, {
label: "lcm(200, 333)",
set: 200,
fn: "lcm",
param: 333, expect: "66600"},
{
label: "1 + -1",
set: 1,
fn: "add",
param: -1,
expect: "0"
}, {
label: "3/10+3/14",
set: "3/10",
fn: "add",
param: "3/14",
expect: "0.5(142857)"
}, {
label: "3/10-3/14",
set: "3/10",
fn: "sub",
param: "3/14",
expect: "0.0(857142)"
}, {
label: "3/10*3/14",
set: "3/10",
fn: "mul",
param: "3/14",
expect: "0.06(428571)"
}, {
label: "3/10 / 3/14",
set: "3/10",
fn: "div",
param: "3/14",
expect: "1.4"
}, {
label: "1-2",
set: "1",
fn: "sub",
param: "2",
expect: "-1"
}, {
label: "1--1",
set: "1",
fn: "sub",
param: "-1",
expect: "2"
}, {
label: "0/1*1/3",
set: "0/1",
fn: "mul",
param: "1/3",
expect: "0"
}, {
label: "3/10 * 8/12",
set: "3/10",
fn: "mul",
param: "8/12",
expect: "0.2"
}, {
label: ".5+5",
set: ".5",
fn: "add",
param: 5, expect: "5.5"},
{
label: "10/12-5/60",
set: "10/12",
fn: "sub",
param: "5/60",
expect: "0.75"
}, {
label: "10/15 / 3/4",
set: "10/15",
fn: "div",
param: "3/4",
expect: "0.(8)"
}, {
label: "1/4 + 3/8",
set: "1/4",
fn: "add",
param: "3/8",
expect: "0.625"
}, {
label: "2-1/3",
set: "2",
fn: "sub",
param: "1/3",
expect: "1.(6)"
}, {
label: "5*6",
set: "5",
fn: "mul",
param: 6,
expect: "30"
}, {
label: "1/2-1/5",
set: "1/2",
fn: "sub",
param: "1/5",
expect: "0.3"
}, {
label: "1/2-5",
set: "1/2",
fn: "add",
param: -5,
expect: "-4.5"
}, {
label: "1*-1",
set: "1",
fn: "mul",
param: -1,
expect: "-1"
}, {
label: "5/10",
set: 5.0,
fn: "div",
param: 10,
expect: "0.5"
}, {
label: "1/-1",
set: "1",
fn: "div",
param: -1,
expect: "-1"
}, {
label: "4/5 + 13/2",
set: "4/5",
fn: "add",
param: "13/2",
expect: "7.3"
}, {
label: "4/5 + 61/2",
set: "4/5",
fn: "add",
param: "61/2",
expect: "31.3"
}, {
label: "0.8 + 6.5",
set: "0.8",
fn: "add",
param: "6.5",
expect: "7.3"
}, {
label: "2/7 inverse",
set: "2/7",
fn: "inverse",
param: null,
expect: "3.5"
}, {
label: "neg 1/3",
set: "1/3",
fn: "neg",
param: null,
expect: "-0.(3)"
}, {
label: "1/2+1/3",
set: "1/2",
fn: "add",
param: "1/3",
expect: "0.8(3)"
}, {
label: "1/2+3",
set: ".5",
fn: "add",
param: 3,
expect: "3.5"
}, {
label: "1/2+3.14",
set: "1/2",
fn: "add",
param: "3.14",
expect: "3.64"
}, {
label: "3.5 < 4.1",
set: 3.5,
fn: "compare",
param: 4.1,
expect: "-1"
}, {
label: "3.5 > 4.1",
set: 4.1,
fn: "compare",
param: 3.1,
expect: "1"
}, {
label: "-3.5 > -4.1",
set: -3.5,
fn: "compare",
param: -4.1,
expect: "1"
}, {
label: "-3.5 > -4.1",
set: -4.1,
fn: "compare",
param: -3.5,
expect: "-1"
}, {
label: "4.3 == 4.3",
set: 4.3,
fn: "compare",
param: 4.3,
expect: "0"
}, {
label: "-4.3 == -4.3",
set: -4.3,
fn: "compare",
param: -4.3,
expect: "0"
}, {
label: "-4.3 < 4.3",
set: -4.3,
fn: "compare",
param: 4.3,
expect: "-1"
}, {
label: "4.3 == -4.3",
set: 4.3,
fn: "compare",
param: -4.3,
expect: "1"
}, {
label: "-0.5^-3",
set: -0.5,
fn: "pow",
param: -3,
expect: "-8"
}, {
label: "",
set: -3,
fn: "pow",
param: -3,
expect: "-0.(037)"
}, {
label: "-3",
set: -3,
fn: "pow",
param: 2,
expect: "9"
}, {
label: "-3",
set: -3,
fn: "pow",
param: 3,
expect: "-27"
}, {
label: "0^0",
set: 0,
fn: "pow",
param: 0,
expect: "1"
}, {
label: "2/3^7",
set: [2, 3],
fn: "pow",
param: 7,
expect: "0.(058527663465935070873342478280749885688157293095564700502972107910379515317786922725194330132601737540009144947416552354823959762231367169638774577046181984453589391860996799268404206675811614083219021490626428898033836305441243712848651120256)"
}, {
label: "-0.6^4",
set: -0.6,
fn: "pow",
param: 4,
expect: "0.1296"
}, {
label: "8128371:12394 - 8128371/12394",
set: "8128371:12394",
fn: "sub",
param: "8128371/12394",
expect: "0"
}, {
label: "3/4 + 1/4",
set: "3/4",
fn: "add",
param: "1/4",
expect: "1"
}, {
label: "1/10 + 2/10",
set: "1/10",
fn: "add",
param: "2/10",
expect: "0.3"
}, {
label: "5/10 + 2/10",
set: "5/10",
fn: "add",
param: "2/10",
expect: "0.7"
}, {
label: "18/10 + 2/10",
set: "18/10",
fn: "add",
param: "2/10",
expect: "2"
}, {
label: "1/3 + 1/6",
set: "1/3",
fn: "add",
param: "1/6",
expect: "0.5"
}, {
label: "1/3 + 2/6",
set: "1/3",
fn: "add",
param: "2/6",
expect: "0.(6)"
}, {
label: "3/4 / 1/4",
set: "3/4",
fn: "div",
param: "1/4",
expect: "3"
}, {
label: "1/10 / 2/10",
set: "1/10",
fn: "div",
param: "2/10",
expect: "0.5"
}, {
label: "5/10 / 2/10",
set: "5/10",
fn: "div",
param: "2/10",
expect: "2.5"
}, {
label: "18/10 / 2/10",
set: "18/10",
fn: "div",
param: "2/10",
expect: "9"
}, {
label: "1/3 / 1/6",
set: "1/3",
fn: "div",
param: "1/6",
expect: "2"
}, {
label: "1/3 / 2/6",
set: "1/3",
fn: "div",
param: "2/6",
expect: "1"
}, {
label: "3/4 * 1/4",
set: "3/4",
fn: "mul",
param: "1/4",
expect: "0.1875"
}, {
label: "1/10 * 2/10",
set: "1/10",
fn: "mul",
param: "2/10",
expect: "0.02"
}, {
label: "5/10 * 2/10",
set: "5/10",
fn: "mul",
param: "2/10",
expect: "0.1"
}, {
label: "18/10 * 2/10",
set: "18/10",
fn: "mul",
param: "2/10",
expect: "0.36"
}, {
label: "1/3 * 1/6",
set: "1/3",
fn: "mul",
param: "1/6",
expect: "0.0(5)"
}, {
label: "1/3 * 2/6",
set: "1/3",
fn: "mul",
param: "2/6",
expect: "0.(1)"
}, {
label: "5/4 - 1/4",
set: "5/4",
fn: "sub",
param: "1/4",
expect: "1"
}, {
label: "5/10 - 2/10",
set: "5/10",
fn: "sub",
param: "2/10",
expect: "0.3"
}, {
label: "9/10 - 2/10",
set: "9/10",
fn: "sub",
param: "2/10",
expect: "0.7"
}, {
label: "22/10 - 2/10",
set: "22/10",
fn: "sub",
param: "2/10",
expect: "2"
}, {
label: "2/3 - 1/6",
set: "2/3",
fn: "sub",
param: "1/6",
expect: "0.5"
}, {
label: "3/3 - 2/6",
set: "3/3",
fn: "sub",
param: "2/6",
expect: "0.(6)"
}, {
label: "0.006999999999999999",
set: 0.006999999999999999,
fn: "add",
param: 0,
expect: "0.007"
}, {
label: "1/7 - 1",
set: 1 / 7,
fn: "add",
param: -1,
expect: "-0.(857142)"
}, {
label: "0.0065 + 0.0005",
set: 0.0065,
fn: "add",
param: 0.0005,
expect: "0.007"
}, {
label: "6.5/.5",
set: 6.5,
fn: "div",
param: .5,
expect: "13"
}, {
label: "0.999999999999999999999999999",
set: 0.999999999999999999999999999,
fn: "sub",
param: 1,
expect: "0"
}, {
label: "0.5833333333333334",
set: 0.5833333333333334,
fn: "add",
param: 0,
expect: "0.58(3)"
}, {
label: "1.75/3",
set: 1.75 / 3,
fn: "add",
param: 0,
expect: "0.58(3)"
}, {
label: "3.3333333333333",
set: 3.3333333333333,
fn: "add",
param: 0,
expect: "3.(3)"
}, {
label: "4.285714285714285714285714",
set: 4.285714285714285714285714,
fn: "add",
param: 0,
expect: "4.(285714)"
}, {
label: "-4",
set: -4,
fn: "neg",
param: 0,
expect: "4"
}, {
label: "4",
set: 4,
fn: "neg",
param: 0,
expect: "-4"
}, {
label: "0",
set: 0,
fn: "neg",
param: 0,
expect: "0"
}
];
describe('Fraction', function() {
for (var i = 0; i < tests.length; i++) {
(function(i) {
var action;
if (tests[i].fn) {
action = function() {
return new Fraction(tests[i].set)[tests[i].fn](tests[i].param).toString();
};
} else {
action = function() {
return new Fraction(tests[i].set).toString();
};
}
it(String(tests[i].label || tests[i].set), function() {
if(tests[i].expectError) {
assert.throws(action, tests[i].expectError);
} else {
assert.equal(action(), tests[i].expect);
}
});
})(i);
}
});
describe('JSON', function() {
it("Should be possible to stringify the object", function() {
assert.equal('{"s":1,"n":14623,"d":330}', JSON.stringify(new Fraction("44.3(12)")));
assert.equal('{"s":-1,"n":2,"d":1}', JSON.stringify(new Fraction(-1 / 2).inverse()));
});
});
describe('Arguments', function() {
it("Should be possible to use different kind of params", function() {
// String
var fraction = new Fraction("0.1");
assert.equal("1/10", fraction.n + "/" + fraction.d);
var fraction = new Fraction("6234/6460");
assert.equal("3117/3230", fraction.n + "/" + fraction.d);
// Two params
var fraction = new Fraction(1, 2);
assert.equal("1/2", fraction.n + "/" + fraction.d);
// Object
var fraction = new Fraction({n: 1, d: 3});
assert.equal("1/3", fraction.n + "/" + fraction.d);
// Array
var fraction = new Fraction([1, 4]);
assert.equal("1/4", fraction.n + "/" + fraction.d);
});
});
describe('fractions', function() {
it("Should pass 0.08 = 2/25", function() {
var fraction = new Fraction("0.08");
assert.equal("2/25", fraction.n + "/" + fraction.d);
});
it("Should pass 0.200 = 1/5", function() {
var fraction = new Fraction("0.200");
assert.equal("1/5", fraction.n + "/" + fraction.d);
});
it("Should pass 0.125 = 1/8", function() {
var fraction = new Fraction("0.125");
assert.equal("1/8", fraction.n + "/" + fraction.d);
});
it("Should pass 8.36 = 209/25", function() {
var fraction = new Fraction(8.36);
assert.equal("209/25", fraction.n + "/" + fraction.d);
});
});
describe('constructors', function() {
it("Should pass 0.08 = 2/25", function() {
var tmp = new Fraction({d: 4, n: 2, s: -1});
assert.equal("-1/2", tmp.s * tmp.n + "/" + tmp.d);
var tmp = new Fraction(-88.3);
assert.equal("-883/10", tmp.s * tmp.n + "/" + tmp.d);
var tmp = new Fraction(-88.3).clone();
assert.equal("-883/10", tmp.s * tmp.n + "/" + tmp.d);
var tmp = new Fraction("123.'3'");
assert.equal("370/3", tmp.s * tmp.n + "/" + tmp.d);
var tmp = new Fraction("123.'3'").clone();
assert.equal("370/3", tmp.s * tmp.n + "/" + tmp.d);
var tmp = new Fraction([-1023461776, 334639305]);
tmp = tmp.add([4, 25]);
assert.equal("-4849597436/1673196525", tmp.s * tmp.n + "/" + tmp.d);
});
});
describe('Latex Output', function() {
it("Should pass 123.'3' = \\frac{370}{3}", function() {
var tmp = new Fraction("123.'3'");
assert.equal("\\frac{370}{3}", tmp.toLatex());
});
it("Should pass 1.'3' = \\frac{4}{3}", function() {
var tmp = new Fraction("1.'3'");
assert.equal("\\frac{4}{3}", tmp.toLatex());
});
it("Should pass -1.0000000000 = -1", function() {
var tmp = new Fraction("-1.0000000000");
assert.equal('-1', tmp.toLatex());
});
it("Should pass -0.0000000000 = 0", function() {
var tmp = new Fraction("-0.0000000000");
assert.equal('0', tmp.toLatex());
});
});
describe('Fraction Output', function() {
it("Should pass 123.'3' = 123 1/3", function() {
var tmp = new Fraction("123.'3'");
assert.equal('370/3', tmp.toFraction());
});
it("Should pass 1.'3' = 1 1/3", function() {
var tmp = new Fraction("1.'3'");
assert.equal('4/3', tmp.toFraction());
});
it("Should pass -1.0000000000 = -1", function() {
var tmp = new Fraction("-1.0000000000");
assert.equal('-1', tmp.toFraction());
});
it("Should pass -0.0000000000 = 0", function() {
var tmp = new Fraction("-0.0000000000");
assert.equal('0', tmp.toFraction());
});
it("Should pass 1/-99/293 = -1/29007", function() {
var tmp = new Fraction(-99).inverse().div(293);
assert.equal('-1/29007', tmp.toFraction());
});
it('Should work with large calculations', function() {
var x = Fraction(1123875);
var y = Fraction(1238750184);
var z = Fraction(1657134);
var r = Fraction(77344464613500, 92063);
assert.equal(x.mul(y).div(z).toFraction(), r.toFraction());
});
});
describe('Fraction toContined', function() {
it("Should pass 415/93", function() {
var tmp = new Fraction(415, 93);
assert.equal('4,2,6,7', tmp.toContinued().toString());
});
it("Should pass 0/2", function() {
var tmp = new Fraction(0, 2);
assert.equal('0', tmp.toContinued().toString());
});
it("Should pass 1/7", function() {
var tmp = new Fraction(1, 7);
assert.equal('0,7', tmp.toContinued().toString());
});
it("Should pass 23/88", function() {
var tmp = new Fraction('23/88');
assert.equal('0,3,1,4,1,3', tmp.toContinued().toString());
});
it("Should pass 1/99", function() {
var tmp = new Fraction('1/99');
assert.equal('0,99', tmp.toContinued().toString());
});
it("Should pass 1768/99", function() {
var tmp = new Fraction('1768/99');
assert.equal('17,1,6,14', tmp.toContinued().toString());
});
it("Should pass 1768/99", function() {
var tmp = new Fraction('7/8');
assert.equal('0,1,7', tmp.toContinued().toString());
});
});
describe('Fraction NaN', function() {
for (var i in Fraction.prototype) {
(function(i) {
if (Fraction.prototype[i] instanceof Function)
switch (i) {
case 'toFraction':
case 'toContinued':
case 'toLatex':
case 'valueOf':
case 'divisible':
case 'compare':
case 'equals':
break;
case 'toString':
it("Should pass " + i, function() {
var x = Fraction(NaN)[i]();
assert.equal('NaN', x);
});
break;
case 'add':
case 'sub':
case 'mul':
case 'div':
case 'pow':
case "gcd":
case 'lcm':
case 'equals':
it("Should pass " + i, function() {
var x = Fraction(NaN)[i](NaN);
assert.equal('NaN NaN', x.d + " " + x.d);
});
break;
default:
it("Should pass " + i, function() {
var x = Fraction(NaN)[i]();
assert.equal('NaN NaN', x.d + " " + x.d);
});
}
})(i);
}
});