1

I have an assignment to make a program that can calculate the area beneath a graph in a certain area. My function is f(x)=3+(tan(x))^2 I have turned this into javascript so it looks like this:

var y = 3 + Math.pow(Math.tan((x)*(180/Math.PI)), 2) 

This is giving the right result at certain x-values. But sometimes, eg. at x = 3.4, it gives a strange number like 16 where I expected something around 3 or 4.

I have read that it can be caused by the problem with floating point behavior, but it should not give that big a difference? I don't understand it. :(

I use a for loop to change the x value and and array to store the values.

3
  • 6
    Math.tan((x)*(180/Math.PI)) Uh? Why are you converting to degrees there? Math.tan() expects radians. Commented Mar 12, 2015 at 11:04
  • Oh wow.. I must have misunderstood somethin I read in another thread. Thanks! I will edit Commented Mar 12, 2015 at 11:14
  • It worked... Of course. It's quite obvious to me now. Thank you so much! I'm so happy it was so simple! Thank you! :) Commented Mar 12, 2015 at 11:18

1 Answer 1

1

x*180/π converts an angle x, given in radians, into degrees. But that's almost certainly wrong here: The JavaScript trigonometric functions expect their arguments in radians. So if your angle is in radians, you don't need any conversion at all, and if it is in degrees, you should convert the other way round, namely x*π/180.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.