Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Data Types

From my understanding JavaScript doesn't have a ints or floats, but just a Number type which is formatted as a double-precision 64-bit floating point value, but JavaScript also has typed arrays which can be many types including: Int32Array, Uint32Array, and Float32Array.

So my question is: do typed arrays underneath just use the Number type with some bit operation wrapper functions, or does it actually use some other data type? And if they do use some other type, then is it possible to actually create your own int and float types by wrapping typed arrays.

like image 348
Vivek Avatar asked Oct 20 '22 03:10

Vivek


1 Answers

So my question is: do typed arrays underneath just use the Number type with some bit operation wrapper functions, or does it actually use some other data type?

Typed arrays do not use the number type. For example new Int32Array(10) will create an array of ten 32-bit ints. Hence it would indeed allocate 40 bytes of space for your array.

Internally any integer you store in the array will only occupy 32-bits (4-bytes) of space. However when reading the data the int will be coerced into a JavaScript number (the primitive, not the object - hence not capitalized). Thus there's no way to read an int into JavaScript.

The JavaScript number data type is a double-precision floating point number. Hence it can represent smaller data types fine. However it cannot represent 64-bit integers, because it is itself a 64-bit floating point number. This is the reason we don't have a Int64Array or a Uint64Array.

And if they do use some other type, then is it possible to actually create your own int and float types by wrapping typed arrays.

Yes, it is possible. However you would have to define your own functions for addition, subtraction, coercion, etc. For example, this is what I would do:

var Num = defclass({
    constructor: function (array) {
        this.constructor = function () {
            this.array = new array(arguments);
        };

        return defclass(this);
    },
    toValue: function () {
        return this.array[0];
    },
    toString: function () {
        return this.array[0];
    },
    plus: function (that) {
        return new this.constructor(this + that);
    }
});

var Int8 = new Num(Int8Array);
var Uint8 = new Num(Uint8Array);
var Int16 = new Num(Int16Array);
var Uint16 = new Num(Uint16Array);
var Int32 = new Num(Int32Array);
var Uint32 = new Num(Uint32Array);
var Float32 = new Num(Float32Array);
var Float64 = new Num(Float64Array);

You can use it as follows:

var a = new Int32(Math.pow(2, 31) - 1); // 2147483647
var b = new Int32(1);
var c = a.plus(b);                      // -2147483648

The defclass function is defined as follows:

function defclass(prototype) {
    var constructor = prototype.constructor;
    constructor.prototype = prototype;
    return constructor;
}

Everything put together:

var Num = defclass({
    constructor: function (array) {
        this.constructor = function () {
            this.array = new array(arguments);
        };

        return defclass(this);
    },
    toValue: function () {
        return this.array[0];
    },
    toString: function () {
        return this.array[0];
    },
    plus: function (that) {
        return new this.constructor(this + that);
    }
});

var Int8 = new Num(Int8Array);
var Uint8 = new Num(Uint8Array);
var Int16 = new Num(Int16Array);
var Uint16 = new Num(Uint16Array);
var Int32 = new Num(Int32Array);
var Uint32 = new Num(Uint32Array);
var Float32 = new Num(Float32Array);
var Float64 = new Num(Float64Array);

var a = new Int32(Math.pow(2, 31) - 1); // 2147483647
var b = new Int32(1);
var c = a.plus(b);                      // -2147483648

alert(a + " + " + b + " = " + c);

function defclass(prototype) {
    var constructor = prototype.constructor;
    constructor.prototype = prototype;
    return constructor;
}
like image 81
Aadit M Shah Avatar answered Oct 23 '22 00:10

Aadit M Shah