SRL Archive - Java Help and Tutorials

is JavaScript good for math?


rafik

2015-03-17 17:30:50

I plan to work on a program (big integer factoring) that will do a lot of computations, mostly subtraction and addition, sometimes multiplication, also some logarithm and square root, and of course a lot of sorting. Another particularity is that the digits it is going to handle might be in hundreds. The question is if JavaScript is suitable for this task and if it is not what programming language should I look for?

rj

2015-03-17 17:58:26

Do you mean good at math, as in they have a lot of math related functions for you, or as in a fast language(which you might not need)?

i'm not sure what language has a lot of good math related functions (well.. mathlab) but I know c++ is of a decent speed

bg5

2015-03-17 18:10:22

I'm not an experienced programmer, but I think JS is rather poor choice for big computation work. Javascript code is interpreted by JS Engine built-in in Web browser, so it will never be such efficient as native languages. I would suggest Fortran or just c++.

grats

2015-03-17 18:23:55

javascript is great for math and has great math functions but it is just that, a scripting language.. so it isn't "amazingly fast"

it is very browser reliant when getting insane amounts of data, you'll find webkit with V8 engine will fly through stuff others will struggle, so chromium/chrome is what you'd want in this case

however with such a huge amount of math, I would say to choose a different language. because you're relying on a browser probably and that's going to cause problems especially with how much numbers you're going to possibly be spitting out

complex problems like this:
https://www.google.com/search?q=sqrt%28cos%28x%29%29*cos%28300x%29%2Bsqrt%28abs%28x%29%29-0.7%29*%284-x*x%29^0.01%2C+sqrt%286-x^2%29%2C+-sqrt%286-x^2%29+from+-4.5+to+4.5&ie=utf-8&oe=utf-8
are no problem

but massive amounts of simple equations usually lead to huge lag or crashing.


either one of these would prob explode a browser


#include <stdio.h>
#include <time.h>

int dividable(int a, int b);
int isPrime(int primeNumber);


main() {
clock_t start = clock(), diff;
int i;
int primes = 0;
for (i = 2; i < 10000000; i++) {
if (isPrime(i)) {
primes += 1;
}
}
diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("%d primes in %d seconds %d milliseconds\n", primes, msec/1000, msec%1000);
}

int isPrime(int primeNumber) {
int i;
for (i = 2; i < primeNumber; i++) {
if (i*i > primeNumber) {
break;
}
if (dividable(primeNumber, i)) {
return 0;
}
}
return 1;
}

int dividable(int a, int b) {
return (a%b == 0);
}

class Prime {

public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int i;
int primes = 0;
for (i = 2; i < 10000000; i++) {
if (isPrime(i)) {
primes += 1;
}
}
long totalTime = System.currentTimeMillis() - startTime;
System.out.printf("%d primes in %d seconds %d milliseconds\n", primes, totalTime / 1000, totalTime % 1000);
}

static boolean isPrime(int primeNumber) {
int i;
for (i = 2; i < primeNumber; i++) {
if (i * i > primeNumber) {
return true;
}
if (isDividable(primeNumber, i)) {
return false;
}
}
return true;
}

static boolean isDividable(int a, int b) {
return (a % b == 0);
}
}


~/d/c> gcc -o prime prime.c
~/d/c> ./prime
664579 primes in 8 seconds 786 milliseconds
~/d/c> javac prime.java
~/d/c> java -cp . Prime
664579 primes in 5 seconds 721 milliseconds

slacky

2015-03-17 20:09:47

From: bg5
I'm not an experienced programmer, but I think JS is rather poor choice for big computation work. Javascript code is interpreted by JS Engine built-in in Web browser, so it will never be such efficient as native languages. I would suggest Fortran or just c++.

Meh.. All of the current popular JS engines are JIT-compiled (V8, SpiderMonkey, etc..). Just about all JIT compilers manage to efficiently compile math-statements to native code (when it's needed), eg as soon as you start looping (and doing a few iterations) it will get compiled (assuming a tracing JIT). Once such operations are compiled to native code they will usually perform at the same level as C (gcc with -O0), and sometimes better.

However. TS is talking about huge numbers. Numbers that wont fit in to the CPU no matter how hard you push, so performance will be iffy no matter what.
So… Just pick any random language.. that allows you to do regular ops on giant numbers, and has builtins that also works with big numbers. I don't think you should care to much about the performance as long as it's fast enough.
Without really thinking about what the TS plan on doing with them numbers, I would blindly suggest Python (maybe with the PyPy interpreter), as it's simple.. but JS works FINE, tho afaik there is no builtin big integer support, so it might get a bit complicated.

Edit:

v0 = 111111112222222223333333333444444444444455555555555
v1 = 555555555554444444444444333333333322222222211111111
print v0 + v1
print math.sqrt(v0 + v1)
>> 666666667776666667777777666777777766666677766666666
>> 2.58198889962e+25

Python makes it simple to work with big numbers.

Brandon

2015-03-17 20:31:11

For the math you describe, Javascript isn't optimal.

Why not use Java? http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html OR http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

Ex:


BigInteger a("111111112222222223333333333444444444444455555555555", 10); //Base10
BigInteger b("11111111222222222", 10); //Base10

BigInteger c = a.add(b); //addition.

//-----------------------------------------------------------------------------//

BigDecimal d("143252352352222222222222299999999.415125151"); //Base10
BigDecimal e("14352352352.523523529"); //Base10

BigDecimal f = d.subtract(e); //subtraction

Do whatever arithmetic you want.. Otherwise: https://gmplib.org/

I wouldn't choose Javascript for large computational code.

Javascript uses FLOAT internally and it's computationally expensive (floating point arithmetic). There exists no such type as "int" or "long" in Javascript. JIT will not fix this..

slacky

2015-03-17 21:23:21

From: Brandon
Javascript uses FLOAT internally and it's computationally expensive (floating point arithmetic). There exists no such type as "int" or "long" in Javascript. JIT will not fix this..

I don't really ever use JS, or know much about it, but would not some bitshifting ensure that the numbers are integers? And so the compiler can do integer-operations.

function int(n) {
if (n & (1 << 31))
return n | ~0xFFFFFFFF;
return n & 0xFFFFFFFF;
}
function main() {
var number = 46555555.9;
console.log(int(number) * int(-54422)); //at this point both numbers should be treated as integers
}
I would be rather surprised if this yields fmul. Id think it would do a "native int" multiplication. This adds some unwanted overhead, could maybe do n|0 instead, to ensure int.

….

If TS is using Node.js he can use a libgmp wrapper to do the computations.

Brandon

2015-03-17 23:34:50

From: slacky
I don't really ever use JS, or know much about it, but would not some bitshifting ensure that the numbers are integers? And so the compiler can do integer-operations.


Maybe.. It should I guess.. Probably even without all of that if it's smart enough. But trust issues and the fact that not all browsers use V8 comes into play.

I can't get the V8 engine to compile on Windows with the –disassembler=on flag else I'd disassemble it and test. So hard to compile on Windows.

Either way, I don't trust javascript enough to put my faith into it for applications requiring high performance (See Jagex's WebGL/Javascript).


EDIT: Apparently V8 uses 32-bit integers whenever it can. Not sure about the arithmetic performed on it though (division specifically).