Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Mathematic Equations not coming out right

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:

What it should be:

What the code produces:

What did I do wrong?

Any and all help is appreciated.
2:31 am on May 9, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

I just used your code and it works as expected
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.
3:52 am on May 9, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

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.
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()

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

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

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