MCPcopy
hub / github.com/Theodeus/tuna / fmod

Function fmod

tuna.js:171–200  ·  view source on GitHub ↗
(x, y)

Source from the content-addressed store, hash-verified

169}
170
171function fmod(x, y) {
172 // http://kevin.vanzonneveld.net
173 // * example 1: fmod(5.7, 1.3);
174 // * returns 1: 0.5
175 var tmp, tmp2, p = 0,
176 pY = 0,
177 l = 0.0,
178 l2 = 0.0;
179
180 tmp = x.toExponential().match(/^.\.?(.*)e(.+)$/);
181 p = parseInt(tmp[2], 10) - (tmp[1] + "").length;
182 tmp = y.toExponential().match(/^.\.?(.*)e(.+)$/);
183 pY = parseInt(tmp[2], 10) - (tmp[1] + "").length;
184
185 if (pY > p) {
186 p = pY;
187 }
188
189 tmp2 = (x % y);
190
191 if (p < -100 || p > 20) {
192 // toFixed will give an out of bound error so we fix it like this:
193 l = Math.round(Math.log(tmp2) / Math.log(10));
194 l2 = Math.pow(10, l);
195
196 return (tmp2 / l2).toFixed(l - p) * l2;
197 } else {
198 return parseFloat(tmp2.toFixed(-p));
199 }
200}
201
202function sign(x) {
203 if (x === 0) {

Callers 1

tuna.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected