Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorter encoding for numbers (breaking change) #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions codec/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// We endpad mantissa with enough zero to exceed mantissa precision.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is for the old code...

// Then negative numbers' mantissa and exponent are flipped (nines' complement)

var END = '_'

exports.encode = function (number) {
if (isNaN(number)) { return "DaN"; }
if (number === 0) { return "FE 0M0"; }
Expand All @@ -14,8 +16,8 @@ exports.encode = function (number) {

var splitScientificNotation = number.toExponential().split('e');
var exponent = Number(splitScientificNotation[1]) + 500;
var mantissa = splitScientificNotation[0] + (splitScientificNotation[0].indexOf('.') === -1 ? '.' : '') + '0'.repeat(20);
var encoded = 'E' + padStart(String(exponent), 3) + 'M' + String(mantissa);
var mantissa = splitScientificNotation[0]
var encoded = 'E' + padStart(String(exponent), 3) + 'M' + String(mantissa) + (number > 0 ? '' : END)
if (number > 0) {
return 'F' + encoded;
} else {
Expand All @@ -30,7 +32,7 @@ exports.decode = function (encoded) {

var isNegative = encoded[0] === 'D';
var splitEncoded = (isNegative ? flip(encoded) : encoded).slice(2).split('M');
return Number((isNegative ? '-':'') + splitEncoded[1] + 'e' + String(Number(splitEncoded[0])-500));
return Number((isNegative ? '-':'') + splitEncoded[1].replace(END,'') + 'e' + String(Number(splitEncoded[0])-500));
}

function flip(number) {
Expand All @@ -49,3 +51,4 @@ function flip(number) {
function padStart (str, count) {
return (' ').repeat(count - str.length).substr(0,count) + str;
};

5 changes: 4 additions & 1 deletion test/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var randArray = function (opt, depth) {
if (opt.depth != 0 && Math.random() < Math.max(0, opt.depth-depth)/opt.depth) {
array.push(randArray(opt, depth + 1));
} else {
var dice = Math.floor(Math.random() * 5)
var dice = Math.floor(Math.random() * 6)
if(dice === 0) {
array.push(randString())
}
Expand All @@ -33,6 +33,9 @@ var randArray = function (opt, depth) {
if(dice === 4) {
array.push((Math.random()*2 - 1)*1e10);
}
if(dice === 5) {
array.push(~~(Math.random()*1000))
}
}
}
return array;
Expand Down