var value = 9007199254740991; //max safe int (53bit)var bin= value.toString(2); //go binaryvar pad=new Array(64-bin.length).join('0') //pad to 64 bit bin = pad+bin;var low = parseInt(bin.substring(0, 32), 2);var high = parseInt(bin.substring(32), 2);var longInt = flatbuffers.Long.create(low,high)console.log(bin) //000000000011111111111111111111111111111111111111111111111111111console.log(low.toString(2)) //111111111111111111111console.log(low) //2097151console.log(high.toString(2)) //11111111111111111111111111111111console.log(high) //4294967295console.log(longInt.toFloat64()) //9223372032564003000 ):
/**
* @param {number} high
* @param {number} low
* @returns {flatbuffers.Long}
*/
flatbuffers.Long.create = function(low, high) {
// Special-case zero to avoid GC overhead for default values
return low == 0 && high == 0 ? flatbuffers.Long.ZERO : new flatbuffers.Long(low, high);
};
/**
* @returns {number}
*/
flatbuffers.Long.prototype.toFloat64 = function() {
return this.low + this.high * 0x100000000;
};
var value=Math.pow(2,53); //max safe int (53bit) 9007199254740992
var bin= value.toString(2); //go binary
var pad=new Array(65-bin.length).join('0') //could skip this and check for length...but this catches 32 bit and 64 bit
var high = parseInt(bin.substring(0, 32), 2);
var low = parseInt(bin.substring(32), 2);
var longInt = flatbuffers.Long.create(low,high)
console.log(longInt.toFloat64()) //9007199254740992
flatbuffers.Long.fromFloat64 = function(value)...
--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Yeah messing with large ints is a mess in js. The only ways I’ve seen involve working with the binary representation either as a string or as an ArrayBuffer. I’m not sure what’s more efficient I’ll do some research/testing when I have a chance. The biginteger library seems to be the most common way to work with them so I’ll check out what would be the most compatible. I would think bitwise shifting would be the best, but I can’t get it to work correctly, from my research it seems js converts anything you perform a bit operation on to a 32bit int…I have no idea why, but it makes it difficult to get the correct result without having to then convert it to a string anyway. I was trying to avoid ArrayBuffer since it’s a ES15 thing, that’s your call on how far back you want it to be compatible.
Sam
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.