main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
845 bytes
|
Rev | Line | |
---|
[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var MAX_ITER = 1075; // 1023+52 (subnormals) => BIAS+NUM_SIGNFICAND_BITS-1
|
---|
| 4 | var maxBits = 54; // only 53 bits for fraction
|
---|
| 5 |
|
---|
| 6 | module.exports = function fractionToBitString(x) {
|
---|
| 7 | var str = '';
|
---|
| 8 | if (x === 0) {
|
---|
| 9 | return str;
|
---|
| 10 | }
|
---|
| 11 | var j = MAX_ITER;
|
---|
| 12 |
|
---|
| 13 | var y;
|
---|
| 14 | // Each time we multiply by 2 and find a ones digit, add a '1'; otherwise, add a '0'..
|
---|
| 15 | for (var i = 0; i < MAX_ITER; i += 1) {
|
---|
| 16 | y = x * 2;
|
---|
| 17 | if (y >= 1) {
|
---|
| 18 | x = y - 1; // eslint-disable-line no-param-reassign
|
---|
| 19 | str += '1';
|
---|
| 20 | if (j === MAX_ITER) {
|
---|
| 21 | j = i; // first 1
|
---|
| 22 | }
|
---|
| 23 | } else {
|
---|
| 24 | x = y; // eslint-disable-line no-param-reassign
|
---|
| 25 | str += '0';
|
---|
| 26 | }
|
---|
| 27 | // Stop when we have no more decimals to process or in the event we found a fraction which cannot be represented in a finite number of bits...
|
---|
| 28 | if (y === 1 || i - j > maxBits) {
|
---|
| 29 | return str;
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 | return str;
|
---|
| 33 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.