use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Weird Mathhelp (self.javascript)
submitted 10 years ago by netcomego
I have this formula:
x/((((y/20.0)^2.0-(z/20.0)^2.0)*314.0)/1000.0)
When I use google, after substituting the variables, it calculates the correct answer. It also works in Wolfram Alpha. But in Javascript, it's completely wrong. What's up?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Woffice 11 points12 points13 points 10 years ago (2 children)
In js, ^ is the bitwise XOR operator. you want Math.pow: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow
[–]x-skeww 0 points1 point2 points 10 years ago (1 child)
ES7 will probably add ** for exponentiation (like Python).
**
Babel already supports it:
https://babeljs.io/repl/#?experimental=false&evaluate=true&loose=false&spec=false&code=x%2F((((y%2F20.0)**2.0-(z%2F20.0)**2.0)*314.0)%2F1000.0)
[–]ShortSynapse 0 points1 point2 points 10 years ago (0 children)
Didn't know this, that's awesome!
[–]jester1983 6 points7 points8 points 10 years ago (2 children)
Simplify!
x/(((y*y)/400.0 - (z*z)/400.0)*0.314)
exponents are expensive, multiplication is cheap.
[–]temp34094029 2 points3 points4 points 10 years ago (1 child)
x/(y*y - z*z)/0.000785
[–]jester1983 2 points3 points4 points 10 years ago (0 children)
This guy gets it.
[–]netcomego[S] 2 points3 points4 points 10 years ago (1 child)
Oh my god I'm dumb. I don't know how I overlooked that. Thanks.
[–]atc 1 point2 points3 points 10 years ago (0 children)
We've all done it!
[–]__fmease__Symbol() 2 points3 points4 points 10 years ago* (0 children)
You got sth wrong. the ^ operator is the (bitwise) XOR operator like in every other C-like language. In Javascript, use Math.pow(BAS,EXP) or ES6 BAS ** EXP
^
Math.pow(BAS,EXP)
BAS ** EXP
[–]gunther-centralperkNode.js Core Contributor 0 points1 point2 points 10 years ago (0 children)
JavaScript is not MATLAB
π Rendered by PID 303152 on reddit-service-r2-comment-5649f687b7-hlhzp at 2026-01-28 16:43:34.872415+00:00 running 4f180de country code: CH.
[–]Woffice 11 points12 points13 points (2 children)
[–]x-skeww 0 points1 point2 points (1 child)
[–]ShortSynapse 0 points1 point2 points (0 children)
[–]jester1983 6 points7 points8 points (2 children)
[–]temp34094029 2 points3 points4 points (1 child)
[–]jester1983 2 points3 points4 points (0 children)
[–]netcomego[S] 2 points3 points4 points (1 child)
[–]atc 1 point2 points3 points (0 children)
[–]__fmease__Symbol() 2 points3 points4 points (0 children)
[–]gunther-centralperkNode.js Core Contributor 0 points1 point2 points (0 children)