-
Notifications
You must be signed in to change notification settings - Fork 0
/
chronos.js
102 lines (90 loc) · 2.41 KB
/
chronos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// chronos.js
// version : 0.1.0
// author : Enrico Spinielli
// license : MIT
// chronosjs.com
(function(_) {
var BOGUS = "bogus";
function quotient(m, n) {
return Math.floor(m / n);
};
function mod(a, b) {
return a - (b * quotient(a, b));
};
function amod(a, b) {
return b + mod(a, -b);
};
function next(index, predicate) {
if (predicate(index)) {
return index;
}
else {
return next(index + 1, predicate);
}
};
function final(index, predicate) {
if (! predicate(index)) {
return index - 1;
}
else {
return final(index + 1, predicate);
}
};
function sum(expression, index, predicate) {
if (! predicate(index)) {
return 0;
}
else {
return expression(index) + sum(expression, index + 1, predicate);
}
};
function binary_search(low, high, dir, condition) {
var mid = (low + high) / 2;
if (dir(low, high)) {
return mid;
}
else if (condition(mid)) {
return binary_search(low, mid, dir, condition);
}
else {
return binary_search(mid, high, dir, condition);
}
};
function invert_angular(func, y, low, high) {
var precision = Math.pow(10, -5);
return binary_search(low, high,
function(l, h) {return (h - l) <= precision;},
function(x) {return mod(func(x) - y, 360) < 180;});
};
/************************************
Exposing Chronos
************************************/
// CommonJS module is defined
// check for nodeJS
var hasModule = (typeof module !== 'undefined' && module.exports);
if (hasModule) {
module.exports = chronos;
}
/*global ender:false */
if (typeof ender === 'undefined') {
// here, `this` means `window` in the browser, or `global` on the server
// add `chronos` as a global object via a string identifier,
// for Closure Compiler "advanced" mode
this['chronos'] = chronos;
}
/*global define:false */
if (typeof define === "function" && define.amd) {
define("chronos", [], function () {
return chronos;
});
}
chronos.version = "0.0.1";
chronos.quotient = quotient;
chronos.mod = mod;
chronos.amod = amod;
chronos.next = next;
chronos.final = final;
chronos.sum = sum;
chronos.binary_search = binary_search;
_.chronos = chronos;
})(this);