Tan () function returns wrong value

I'm having a problem with some code, but I can't find the error. I am trying to calculate the distance indicated in red in the sketch below.

My code returns value: -41.63

Correct value: 3.75

My code:

return round(6.5 * tan(30),2);

      

Based on: http://php.net/manual/en/function.tan.php

enter image description here

I thought it would be a simple task, but I hit the wall - I see no error. Hopefully some of you can point me in the right direction.

Thank,

Kenneth

+3


source to share


2 answers


The arg parameter is in radians.

tan ()

You send the number of degrees and the function expects Radians, so first convert your value from Degrees to Radians and you will get the expected result.



return round(6.5 * tan(deg2rad(30)),2);   //3.75

      

Fiddle

+5


source


Your current value is the number of degrees and you need to convert it to radians first. Use the function deg2rad()

inside tan()

to convert the value from degree to radium.



deg2rad()

: converts a number in degrees to the equivalent of a radian

+2


source







All Articles