homepage Welcome to WebmasterWorld Guest from
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

Mathematic Equations not coming out right

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:

What it should be:

What the code produces:

What did I do wrong?

Any and all help is appreciated.



WebmasterWorld Senior Member 5+ Year Member

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

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.


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

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.


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

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.

Global Options:
 top home search open messages active posts  

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

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved