5

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.

1

2 Answers 2

3

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;
}

Sign up to request clarification or add additional context in comments.

1 Comment

While possible, this solution will of course be horribly inefficient. For int32 and uint32 there are simpler and cheaper ways to do a coercion, namely x|0 and x>>0.
0

JavaScript allows you to work with three primitive data types:

Numbers eg. 123, 120.50 etc.

Strings of text e.g. "This text string" etc.

Boolean e.g. true or false.

JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value.

In addition to these primitive data types, JavaScript supports a composite data type known as object.

but you can declare typed array like bellow:

// create an 8-byte ArrayBuffer
      var b = new ArrayBuffer(8);

      // create a view v1 referring to b, of type Int32, starting at
      // the default byte index (0) and extending until the end of the buffer
      var v1 = new Int32Array(b);

      // create a view v2 referring to b, of type Uint8, starting at
      // byte index 2 and extending until the end of the buffer
      var v2 = new Uint8Array(b, 2);

      // create a view v3 referring to b, of type Int16, starting at
      // byte index 2 and having a length of 2
      var v3 = new Int16Array(b, 2, 2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.