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




msg:4572120
 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:4572126
 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:4572133
 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:4572239
 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  
 

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