# My system gives wrong results in the tan function. probably my code is wrong

This is my code (i also tried to sin **-1(0,5) that schould give 30 but that was rejected for code error.

``````{% for x in range(0, 90, 10) -%}
{% set y = tan(x)  %}
{{ x }}°   {{ y | round(3) }}
{%- endfor %}
``````

this is the result from devtools.template

``````0°     0.0
10°    0.648
20°    2.237
30°   -6.405
40°   -1.117
50°   -0.272
60°    0.32
70°    1.222
80°    9.004
``````

and this is what it should be:

``````0°	0	                0,000
10°	0,176326980708464	0,176
20°	0,363970234266202	0,364
30°	0,577350269189625	0,577
40°	0,839099631177280	0,840
45°	1	                1,000
50°	1,191753592594210	1,192
60°	1,732050807568877	1,732
70°	2,747477419454622	2,748
80°	5,671281819617710	5,671
``````

I think it is treating the angle as radians.

2 Likes

multiply degrees * 0.27777777777 to get radians

any idea how i could get HA to give me the real tangens?

test if nickrout is correct by multiplying your angle by .277 and then plugging it into the tangent function.

(I’m not proficient with templating at all to help you with that)

1 Like

nope:

``````{% for x in range(0, 90, 10) -%}
{% set r = x * 0.277  %}
{% set y = tan(r)  %}
{{ x }}°   {{ y | round(3) }}
{%- endfor %}
``````

result

``````0°   0.0
10°   -0.39
20°   -0.919
30°   -2.039
40°   -11.817
50°   3.386
60°   1.292
70°   0.6
80°   0.17
``````

am I truly the first to do a sin cos tan calculation in HA?

or is it just my system that produces this crap?

could any of you guys just plug my code into your devtools.template thing and report back?

ok… holdon

Still not working

``````{% for x in range(0, 370, 10) -%}
{% set r = x * 100/360  %}
{% set y = tan(r)  %}
{{ x }}° {{r | round(3) }}  {{ y | round(3) }}
{%- endfor %}
``````
``````0° 0.0  0.0

10° 2.778  -0.381

20° 5.556  -0.891

30° 8.333  -1.924

40° 11.111  -8.617

50° 13.889  3.945

60° 16.667  1.424

70° 19.444  0.677

80° 22.222  0.235

90° 25.0  -0.134
``````

I just found the factor

its 0.017453293

this means HA expects radiant not angles, who the heck did this in the core? someone needs to make a Doku for the sin cos tan functions in HA, namely that they expect radiant instead of what every other calculator on this planet expects.

@avd706 the factor for radiant to angle is different than the one you quoted, where did you get that number from? (i had to first look it up too…)

that ones working correctly (near enough at least, so it does not matter any m ore, I aimed for 5 digit behind the decimal point on 80% then googled the factor and found it to be correct.

``````{% for x in range(0, 91, 10) -%}
{% set r = x * 2 * pi / 360  %} {# this would also work: {% set r = x * 0.017453293  %} #}
{% set y = tan(r)  %}
{{ x }}°   {{ y | round(5) }}  {{ r | round(3) }}
{%- endfor %}
``````

so after this short distraction, that took me half a day to figure out that it was not my code that was wrong, but HA to do something that is unexpected, I now lost my whole code and will start from scratch.

The conversion is 2pi / 360. And radians are the standard in pretty much all technical, physical and mathematical fields, they’re also the standard for trigonometric functions in pretty much every programming language, including Python. So using it in templating makes perfect sense.

1 Like

thank you all for your help !!

too bad one could not declare 2 posts as solution, since the one from nickrout was right, but he did not give any explanation or hint, I have marked my own post with the explanation and the corrected code as the solution.

Drats, I was using degrees * 100/360

@HeyImAlex thanks for the clarification, I do see why it is the way it is.
I modified my code that is marked as the solution with your input to

`{% set r = x * 2 * pi / 360 %}`

I can honestly say, I haven’t ever used radiant in all my life. And any scientific calculator I used could be switched to rad, thats ture, but did never have rad out of the box as the input for sin cos or tan…

But since now I know how to treat HA, Ill wont make that error again.

In other words `x *pi/180`

Radians are the standard way of expressing angles in mathematics and in computer science.

Do not help and do not endear you to other forum users.

is that you could propose a change to the docs, just by clicking the link on the relevant documentation page. I agree it would be a good addition.

shortened formulas wont help most folk understand the issue, as most folks would not have shortened formulas since they left school.

While I highly appreciate the correction, since I learn to better use English, a language that is not my native tongue, Tangens is Latin, and therefore always correct as its the origin. did not research for rad, though.

I do understand why it is the way it is in HA. And I even concur. On the contrary, Im pretty sure if you ask folks what to put behind the “tan” function (or any other of the triangle functions) or what rad is, most folk would give you a blank star as an answer and the rest would put an angel behind the tan function, as that is what most calculators they have worked with would expect. And you probably get 1-2% of folks who use it on a regular base and give the right answer to rad and ask if the tan function is on a calculator or a coding language / scientific use.

And I think one of the key success reasons of HA is, to be doable by most folk and mostly in GUI, and behave in a way even an below average person can handle the setup and operation. So in this instance, as we both agree, a good documentation of this scientific behavior of HA, where it clearly deviates from the behavior what most folks would expect, is required and I will make a suggestion to the docs as you recommended.

you are clearly right, I apologize to all I may have offended. I got carried away with my frustration for seeing funny results from a complex (all in one integrated) formula that was indeed correct aside from the rad thing and not finding any hint as to why my system did give back different results than my calculator or excel had. The Intel memory bug from decades ago comes to mind you look at the difference in results and think WTF did I just do wrong and start taking your formula apart, one by one… Loosing a days worth of formula and code development due to a stupid copy paste error did not improve my mode either.

So sorry again.

And thanks for the help.

cheers Manne

Not offended, and your offer to amend the docs is very appreciated.