I am a very mathematically oriented programmer, and I happen to be doing a lot of java script these days. I am really disappointed in the math aspects of javascript:
- the Math object is almost a joke because it has so few methods
- you can’t use ^ for exponentiation
- the + operator is very limited, you cant add array’s of numbers or do scalar multiplication on arrays
Now I have written some pretty basic extensions to the Math object and have considered writing a library of advanced Math features, amazingly there doesn’t seem to be any sort of standard library already out even for calculus, although there is one for vectors and matricies I was able find.
The notation for working with vectors and matricies is really bad when you can’t use the + operator on arrays, and you cant do scalar multiplication. For example, here is a hideous expression for subtracting two vectors, A – B:
Math.vectorAddition(A,Math.scalarMultiplication(-1,B));
I have been looking for some kind of open-source project to contribute to for awhile, and even though my C++ is a bit rusty I would very much like to get into the code for V8 engine and extend the + operator to work on arrays, to get scalar multiplication to work, and possibly to get the ^ operator to work for exponentiation. These things would greatly enhance the utility of any mathematical javascript framework.
I really don’t know how to get involved in something like the V8 engine other than download the code and start working on it. Of course I’m afraid that since V8 is chrome specific, that without browser cross-compatibility a fundamental change of this type is likely to be rejected for V8. I was hoping someone could either tell me why this is a bad idea, or else give me some pointers about how to proceed at this point to get some kind of approval to add these features.
Thanks!
6
Your code isn’t too likely to find its way into V8. It’s a language change, and language changes happen only rarely, and if they do, it’s usually because a company, rather than a person or even a community, is pushing for them.
So … unless you want to train your VM muscles, I’d try and stay within the language.
You’ve hit on the two things that actually bum me out about JS sometimes. Wimpy numbers and no operator overloading. One of JS’s strengths however is ease of creating a less tedious interface than the perhaps more verbose/self-documenting interfaces from core functionality (or any icky interface you don’t want to deal with for that matter). Here’s a simple chaining addition interface I whipped up. I think I’d rather just add a subtract method and chain as necessary but when it sees a ‘-‘ it will subtract the next vector arg and its args are also overloaded to handle dropping an interface vector object directly in so stuff like this should work:
var vec1 = $v([1,2,3]);
vec1.val();//[1,2,3];
vec1.add([1,1,1],[1,1,1]).val();// [3,4,5]
vec1.add('-',[2,2,2]).val();// [1,2,3]
var vec2 = $([3,2,1]).add(vec1,[1,1,1],'-',[2,2,2]).val(); // [5,5,5]
And the func:
function $v(vector){ //produce basic chaining add/subtract wrapper
return new VectorInterface(vector);
function VectorInterface(vectorVal){
if(!validateVector(vectorVal)){
vectorVal = [0,0,0]; //no idea if you would want this as a default
}
//vectorVal is persistent
//I use function hoisting to keep interface declaration clean
this.val = val;
this.add = add;
//this.subtract = subtract; //so we can subtract without cheesy '-' args later
//public methods
function val(){
return vectorVal;
}
function add(){
var
argLen = arguments.length,
mod = 1
;
for(var i=0; i < argLen; i++){
var thisArg = arguments[i];
if(thisArg.constructor.name === VectorInterface){
thisArg = thisArg.val();
}
if(validateVector(thisArg)){
vectorVal[0]+= mod * thisArg[0];
vectorVal[1]+= mod * thisArg[1];
vectorVal[2]+= mod * thisArg[2];
mod = 1;
}
else if(thisArg='-'){
mod=-1;
}
}
return this; //make it chainable for addition of other methods
}
//internal
function validateVector(vec){
if((vec !== undefined) && (vec.length !== undefined) && ( vec.length === 3 )){
return true;
}
else { return false; }
}
};
}
It wouldn’t be that hard to add other operator methods or further overload the args to identify arrays of array vectors and chew through them as appropriate when added to the args.