Results 1 to 8 of 8

Thread: is JavaScript good for math?

  1. #1
    Join Date
    Jun 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default is JavaScript good for math?

    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?

  2. #2
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    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

  3. #3
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    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++.

  4. #4
    Join Date
    Feb 2007
    Location
    Colorado, USA
    Posts
    3,716
    Mentioned
    51 Post(s)
    Quoted
    624 Post(s)

    Default

    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

    Code:
    #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);
    }
    Code:
    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);
        }
    }
    Code:
     ~/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
    Last edited by grats; 03-17-2015 at 11:58 PM.
    The only true authority stems from knowledge, not from position.

    You can contact me via matrix protocol: @grats:grats.win or you can email me at the same domain, any user/email address.

  5. #5
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by bg5 View Post
    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:
    python Code:
    v0 = 111111112222222223333333333444444444444455555555555
    v1 = 555555555554444444444444333333333322222222211111111
    print v0 + v1
    print math.sqrt(v0 + v1)
    >> 66666666777666666777777766677777776666667776666666 6
    >> 2.58198889962e+25

    Python makes it simple to work with big numbers.
    Last edited by slacky; 03-18-2015 at 12:55 AM.
    !No priv. messages please

  6. #6
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

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

    Why not use Java? http://docs.oracle.com/javase/6/docs...igInteger.html OR http://docs.oracle.com/javase/7/docs...igDecimal.html

    Ex:

    Java Code:
    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..
    Last edited by Brandon; 03-18-2015 at 12:42 AM.
    I am Ggzz..
    Hackintosher

  7. #7
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    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.
    javascript Code:
    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.
    Last edited by slacky; 03-18-2015 at 04:05 AM. Reason: spelling
    !No priv. messages please

  8. #8
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    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).
    Last edited by Brandon; 03-18-2015 at 03:51 AM.
    I am Ggzz..
    Hackintosher

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •