Welcome to WebmasterWorld Guest from 107.20.75.63

Forum Moderators: open

Message Too Old, No Replies

Mathematic Equations not coming out right

twprogrammers

1:47 am on May 9, 2013 (gmt 0)

New User

joined:May 6, 2013
posts:32

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

2:31 am on May 9, 2013 (gmt 0)

Senior Member

joined:May 31, 2005
posts:1108

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

3:52 am on May 9, 2013 (gmt 0)

Senior Member from US

joined:Apr 9, 2011
posts:12702

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

9:29 am on May 9, 2013 (gmt 0)

New User

joined:May 6, 2013
posts:32

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.