Welcome to WebmasterWorld Guest from 54.227.20.250 register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts, Become a Pro Member
 Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

# JavaScript and AJAX Forum

 Tweet
Mathematic Equations not coming out right
twprogrammers

Msg#: 4572118 posted 1:47 am on May 9, 2013 (gmt 0)

Here is the equations:
[ip-programs.com ] (sorry, it did not allow the square root symbol, so put it in an image)

Here is my code to figuring calculating them:
 var y = (d * h)/Math.sqrt(Math.pow(w,2)+Math.pow(h,2)); var x = (w/h)*y;

Here are the sample numbers:
d=4.7
h=3
w=5

What it should be:
y=~2.41
x=~4.03

What the code produces:
y=~2.06
x=~3.43

What did I do wrong?

Any and all help is appreciated.

Dijkgraaf

Msg#: 4572118 posted 2:31 am on May 9, 2013 (gmt 0)

I just used your code and it works as expected
y=2.4181300505093746
x=4.0302167508489575
So either you've not set the sample number correctly, or there is something specific to the browser or operating system that you are using that is causing the issue.

lucy24

Msg#: 4572118 posted 3:52 am on May 9, 2013 (gmt 0)

Ooh, what fun. I'm sure you've already figured out that the error is in y: If you manually set y=2.06, you'll get the x value you report.

So the next step is to backtrack and figure out how to arrive at your erroneous y from the given starting values.

:: pause to extract calculator from beneath cat ::

Are you getting

y = 2.057983021710106 ?

That's what you get if d is set to 4 instead of 4.7. There are undoubtedly lots of other ways to get the same result-- but this one's a promising start because it suggests that somewhere else in your code, d is getting converted to Math.floor(d). May as well start with the easiest possibility.

twprogrammers

Msg#: 4572118 posted 9:29 am on May 9, 2013 (gmt 0)

Here is how d is set:
 var d = parseInt(document.getElementById("d").value,10);

where the element with the id of "d" is a textbox with the value of "4.7"

So is that where my error is coming from?

EDIT: Where I used parseInt() I should have used parseFloat()

Changed:
 var d = parseInt(document.getElementById("d").value,10);

to:
 var d = parseFloat(document.getElementById("d").value);

And it worked like a charm. Thanks for pointing me in the right direction.

 Global Options: top home search open messages active posts  Tweet

 Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX