0

I've ported a simple algorithm over from Python3 to Javascript, and surprisingly, I'm getting different answers. The Python code works as expected, and I don't know why the Javascript acts differently.

Here's the Python:

def foo():
    theArray = [1,2,3,4,5,6]
    a = theArray[0]
    b = theArray[1]
    c = theArray[2]
    d = theArray[3]
    e = theArray[4]
    f = theArray[5]
    res = (((a**2 + b**2 - (d - e)^3 + (b//e)^2)/ ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + (c//d)^2))*0.75)
    return res

Python3 Result: 0.32..

Here's the Javascript code:

function foo() {
    theArray = [1,2,3,4,5,6]
    var a, b, c, d, e, f
    a = theArray[0]
    b = theArray[1]
    c = theArray[2]
    d = theArray[3]
    e = theArray[4]
    f = theArray[5]
    res = (((a**2 + b**2 - (d - e)**3 + (b/e)**2)/ ((a**2 + b**2 - (d - e)**3) + c**2 + (f-4)**2 + (c/d)^2))*0.75)
    return res
}

Javascript Result: 0.27..

Using Math.pow() in the Javascript code didn't change anything.

DNburtonguster
  • 367
  • 3
  • 14
  • You're going to want to read up on Javascript and floating point arithmetic – caseyWebb Feb 10 '20 at 22:11
  • I see the floating point issue, but I don't believe that's the issue. If you use different test cases w/different values in the array, the real issue becomes more apparent. – DNburtonguster Feb 10 '20 at 22:17
  • see https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – cghislai Feb 10 '20 at 22:17
  • `b//e` and `c//d` in Python do integer division and round the result down to zero. You probably want `b/e` and `c/d` instead – Stuart Feb 10 '20 at 22:54

2 Answers2

1

First thing I notice is you sometimes use "**" for exponentiation, and sometimes "^". The former is what you should use for both python and javascript (or at least according to w3 schools, but anyway they produce different results for me)
I'm assuming you don't mean to use the bitwise "XOR" operator , but rather an exponent when you write "^", as you've typed different symbols both in python and in js…

When I change all instances of "^" to "**" on my computer, both algorithms return 0.2361

1

Note that they are not the same formulas.

In your python code you have:

res = (((a**2 + b**2 - (d - e)^3 + (b//e)^2) / ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + (c//d)^2))*0.75

Which has (d - e)^3 and (b//e)^2 and (f-4)^2

In your js code you have:

res = (((a**2 + b**2 - (d - e)**3 + (b/e)**2) / ((a**2 + b**2 - (d - e)**3) + c**2 + (f-4)**2 + (c/d)^2))*0.75)

Which instead has (d - e)**3 and (b//e)**2 and (f-4)**2

The XOR operation is a very different operation from exponents.

Also, do note that in python you have lots of integer divides. In javascript the equivalent would be something like:

(Math.floor(b/e))^2

So the correct js formula should be:

res = (((a**2 + b**2 - (d - e)^3 + Math.floor(b/e)^2) / ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + Math.floor(c/d)^2))*0.75
slebetman
  • 109,858
  • 19
  • 140
  • 171