# Why using math.sin() and got a wrong result? (SOLVED)

**URL:** <https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091>\
**Category:** Questions\
**Created:** [June 11, 2017, 10:08am UTC](https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091 "2017-06-11T10:08:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![solariyoung](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/solariyoung/32/3705_2.png) [@solariyoung](https://forum.defold.com/u/solariyoung)\
**Post date:** [June 11, 2017, 10:08am UTC](https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091/1 "2017-06-11T10:08:54Z")

</div>

Hi guys  
I am learning defold engine serval days ago, and today i met a problem that  
the math.sin() function output a wrong result.

the code is

```lua
function update(self, dt)
	-- Add update code here
	-- Remove this function if not needed
		self.position = go.get_position()
		self.x= self.position.x
		self.y= self.position.y
		self.t = self.t + dt 
		--self.dx = math.cos(self.t * self.speed) * self.radius 
		--self.dy = math.sin(self.t * self.speed) * self.radius
		self.dx = math.cos(self.radius) *self.t *self.speed
		self.dy = math.sin(self.radius) *self.t *self.speed
		self.pos = vmath.vector3() 
		print(dt)
		print(math.cos(self.radius))
		print(math.sin(self.radius))
		print(self.dx)
		print(self.dy)
		self.pos.x = self.x + self.dx 
    	self.pos.y = self.y + self.dy
    	go.set_position(self.pos) 
    	
	
end

```

but the result was a different at the angle 30.86°  
sin(30.86 °) should be ≈ 0.5129 but the output was a negative value

```lua
DEBUG:SCRIPT: vmath.quat(0, 0, 0, 1)
INFO:DLIB: SSDP: Started on address 192.168.31.161
DEBUG:SCRIPT: 30.867730175872
DEBUG:SCRIPT: 0.016666667535901
DEBUG:SCRIPT: 0.85346587444752
DEBUG:SCRIPT: -0.52114873227662

```

so i came here to look for help, is it any wrong with my code? thanks!

---

<div class="post-metadata">

**Author:** ![sergey.lerg](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sergey.lerg/32/3970_2.png) [@sergey.lerg](https://forum.defold.com/u/sergey.lerg)\
**Post date:** [June 11, 2017, 10:13am UTC](https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091/2 "2017-06-11T10:13:48Z")

</div>

Normally, you don’t put radius value into sin() or cos(). Usually the equation for a radial positioning of something looks like this:

```lua
local angle = math.pi
local x = math.cos(angle) * radius
local y = math.sin(angle) * radius

```

Angle should be in radians.

---

<div class="post-metadata">

**Author:** ![solariyoung](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/solariyoung/32/3705_2.png) [@solariyoung](https://forum.defold.com/u/solariyoung)\
**Post date:** [June 11, 2017, 10:44am UTC](https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091/3 "2017-06-11T10:44:56Z")

</div>

thanks sergey

actually the radius variable is the angle  
i did not change the variable name

---

<div class="post-metadata">

**Author:** ![andreas.strangequest](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andreas.strangequest/32/3622_2.png) [@andreas.strangequest](https://forum.defold.com/u/andreas.strangequest)\
**Post date:** [June 11, 2017, 7:10pm UTC](https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091/4 "2017-06-11T19:10:10Z")

</div>

Maybe clear up the code and console output before posting as there is several weird things that I dont understand.  
First thing you print is deltatime (dt) and that is certainly not first in the output (neither the quat or 30.86..)  
Then you show is a line of math.cos with self.radius but nowhere in the code is self.radius set or altered which really confuses me. How am I supposed to read the output to begin with? Where do you print the angle?  
Try print(self.radius) and print(math.sin(self.radius))

---

<div class="post-metadata">

**Author:** ![solariyoung](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/solariyoung/32/3705_2.png) [@solariyoung](https://forum.defold.com/u/solariyoung)\
**Post date:** [June 12, 2017, 3:19am UTC](https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091/5 "2017-06-12T03:19:57Z")

</div>

thanks

finally i found where my error from

i have to transform the angle to radius  
so my code should be  
math.cos(math.rad(self.radius))

and the result was correct

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 12, 2017, 4:20am UTC](https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091/6 "2017-06-12T04:20:15Z")

</div>

> [@solariyoung](#):
>
> i have to transform the angle to radius

Good that you got it working. All math.\* functions expect angles expressed in radians. The same goes for the vmath.\* functions.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 12, 2017, 8:13am UTC](https://forum.defold.com/t/why-using-math-sin-and-got-a-wrong-result-solved/9091/7 "2017-06-12T08:13:17Z")

</div>

I think there might be a slight error in naming here which can be confusing for some readers.

If the “radius” is an angle, I think it’s better to name it so (e.g. “self.angle”) since [**radius**](https://en.wikipedia.org/wiki/Radius) is the distance from the center of a circle to it’s edge (i.e. not an angle)

What might cause this confusion is the name [**radians**](https://en.wikipedia.org/wiki/Radian), which is a unit of measurement of angles. (Note that your fix suggests that your angle was measured in **degrees** ).
