Data update
This commit is contained in:
parent
796d366b97
commit
35bcdeebf8
504 changed files with 7045 additions and 610 deletions
|
|
@ -1,48 +1,75 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
"use strict";
|
||||
|
||||
const main = () =>
|
||||
showJSON(
|
||||
map( // Using a tolerance epsilon of 1/10000
|
||||
n => showRatio(approxRatio(0.0001)(n)),
|
||||
[0.9054054, 0.518518, 0.75]
|
||||
)
|
||||
);
|
||||
|
||||
// Epsilon -> Real -> Ratio
|
||||
// ---------------- APPROXIMATE RATIO ----------------
|
||||
|
||||
// approxRatio :: Real -> Real -> Ratio
|
||||
const approxRatio = eps => n => {
|
||||
const
|
||||
gcde = (e, x, y) => {
|
||||
const _gcd = (a, b) => (b < e ? a : _gcd(b, a % b));
|
||||
return _gcd(Math.abs(x), Math.abs(y));
|
||||
},
|
||||
c = gcde(Boolean(eps) ? eps : (1 / 10000), 1, n);
|
||||
return Ratio(
|
||||
Math.floor(n / c), // numerator
|
||||
Math.floor(1 / c) // denominator
|
||||
);
|
||||
};
|
||||
const approxRatio = epsilon =>
|
||||
n => {
|
||||
const
|
||||
c = gcdApprox(
|
||||
0 < epsilon
|
||||
? epsilon
|
||||
: (1 / 10000)
|
||||
)(1, n);
|
||||
|
||||
return Ratio(
|
||||
Math.floor(n / c),
|
||||
Math.floor(1 / c)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
// gcdApprox :: Real -> (Real, Real) -> Real
|
||||
const gcdApprox = epsilon =>
|
||||
(x, y) => {
|
||||
const _gcd = (a, b) =>
|
||||
b < epsilon
|
||||
? a
|
||||
: _gcd(b, a % b);
|
||||
|
||||
return _gcd(Math.abs(x), Math.abs(y));
|
||||
};
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
// Using a tolerance of 1/10000
|
||||
[0.9054054, 0.518518, 0.75]
|
||||
.map(
|
||||
compose(
|
||||
showRatio,
|
||||
approxRatio(0.0001)
|
||||
)
|
||||
)
|
||||
.join("\n");
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
// A function defined by the right-to-left
|
||||
// composition of all the functions in fs.
|
||||
fs.reduce(
|
||||
(f, g) => x => f(g(x)),
|
||||
x => x
|
||||
);
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// Ratio :: Int -> Int -> Ratio
|
||||
const Ratio = (n, d) => ({
|
||||
type: 'Ratio',
|
||||
'n': n, // numerator
|
||||
'd': d // denominator
|
||||
type: "Ratio",
|
||||
n,
|
||||
d
|
||||
});
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// showJSON :: a -> String
|
||||
const showJSON = x => JSON.stringify(x, null, 2);
|
||||
|
||||
// showRatio :: Ratio -> String
|
||||
const showRatio = nd =>
|
||||
nd.n.toString() + '/' + nd.d.toString();
|
||||
`${nd.n.toString()}/${nd.d.toString()}`;
|
||||
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue