| 169 | } |
| 170 | |
| 171 | function 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 | |
| 202 | function sign(x) { |
| 203 | if (x === 0) { |