sin pi

I made some changes to see results:

set tangle to pi -- changed this from 0
set t1 to 1
set aq to id_quad(tangle)
set r1 to dtr(tangle)

if t1 = 1 then
	set c1 to cos r1
	
	set r1 to acos c1
	
	set dt to rtd(r1)
	
	set df to cosalign(dt, aq)
else if t1 = 2 then
	set c1 to sin r1
	
	set r1 to asin c1
	
	set dt to rtd(r1)
	set df to sinalign(dt, aq)
end if

{sin (r1), cos (r1)} -- added this for result

on rtd(d)
	return ((d * 180) / pi)
end rtd

on dtr(r)
	return ((r * pi) / 180)
end dtr

to cosalign(d, q)
	if (q > 2) then
		return (360 - d)
	else
		return d
	end if
end cosalign

to sinalign(d, q)
	if (d + 2.0E-14) < 0 then
		if q = 4 then
			return 360 + d
		else # q = 3 
			return 180 + (-1 * d)
		end if
	else if q = 2 then
		return (180 - d)
	else
		return d
	end if
end sinalign

on id_quad(d)
	set d to d mod 360
	if d < 0 then set d to d + 360
	if d ≥ 0 and d < 90 then
		return 1
	else if d ≥ 90 and d < 180 then
		return 2
	else if d ≥ 180 and d < 270 then
		return 3
	else if d ≥ 270 and d < 360 then
		return 4
	end if
end id_quad

→ {0.054803665149, 0.998497149864}
I’m doing something wrong here.

I just thought of something. What if you intercept the sin or cos. Something like this:

set T to pi
sin T

on sin (T)
	set T to pi - T
	continue sin (T)
end sin

Hello.

You may do that if the angle in radians > pi/2, but what if it is bigger than pi?

And you can try log df at the end of the script (after the if block) to see the result, or return df.

The tangle should be in degrees, because the dtr() handler changes it into radians.

HI McUsr,

I didn’t know it was in derees, but I get the same thing. Either sine or cosine is not what it should be.

{1.0, 6.12323399573677E-17}

That’s why I’m trying to interrupt the calls to sin or cos to adjust separately. I’m still looking at what is the definition of a quadrant also. :slight_smile:

gl,
kel

Hello.

A circle is divided into 360 degrees, which can be parted into four counter clockwise quadrants, the first from [0…90> the second from [90…180>, third from [180…270> and the fourth from [270…360>. That would be on a unity circle: a circle with a radius of 1.

It seems to me that if your argument to the last call was pi/2 or 90, and the handlers was sin pi/2, cos pi/2. or that the argument was pi, and the handlers that were fed that value was cos pi, and sin pi.

Both results would be correct, as 6.12323399573677E-17 is very equal to zero.

Yes, and cos should be zero while it is not. But If you shift cos then you can get 0 I think.

Hi McUsr,

It’s not zero, but it can be I think. But I just can’t figure out how to make everything work right now. Need to think.

Later,
kel

Hi McUsr,

Think I almost have it except getting the cosine of the negative angles:

property T : missing value
set T to 3 * pi / 2
set T to T mod (2 * pi)
-- map the angle to  first or fourth quadrant if needed
if (abs T) > (pi / 2) then
	set T to pi - T
end if
{sin T, sin ((pi / 2) - T)} -- second result is wrong for 3*pi/2 because T is negative

Hello.

I just wrote this handler, that maps a quantity to within the range of a modulus, (10,24,360,2pi, pi, etc. ), you can use that inside the call to sin, so you get a positive quantity, say in the range 0…pi.

set T to -3 * pi / 2
set m to (asin (sin mapWithinModulus(T, pi))) * 180 / pi

on mapWithinModulus(aDecNumber, aModulus)
	set aDecNumber to aDecNumber mod aModulus
	if aDecNumber < 0 then set aDecNumber to aDecNumber + aModulus
	return aDecNumber
end mapWithinModulus

Hi McUsr,

Thanks for the script. I’ll check it out.

Meanwhile, I finally got the script working for sine and cosine. It probably needs some fixing up.

property T : missing value
property the_sign : 1 -- for cosine

set T to 3 * pi / 2
set T to T mod (2 * pi)
-- map to first or fourth quadrant
if T > (pi / 2) and T ≤ 3 * pi / 2 then
	set the_sign to -1
	set T to pi - T
end if
set cos_angle to (pi / 2) - (abs T)

{sin T, the_sign * (sin cos_angle)} -- second result is cosine of original angle

The second result is cosine of the original angle.

Thanks for everything,
kel

Fixed a bug with the cosine:

property T : missing value

set results_list to {}
repeat with n from 0 to 8
	set the_sign to 1
	set T to n * pi / 2
	set T to T mod (2 * pi)
	-- map to first or fourth quadrant
	if T > (pi / 2) and T ≤ 3 * pi / 2 then
		set the_sign to -1
		set T to pi - T
	end if
	set cos_angle to (pi / 2) - (abs T)
	set end of results_list to {sin T, the_sign * (sin cos_angle)} -- second result is cosine of original angle
end repeat
results_list

Edited: fixed bug for negative angle values less than -pi.

set results_list to {}
repeat with n from -12 to 12
	
	set T to n * pi / 4
	
	-- reduce the angle to < 2pi
	set T to T mod (2 * pi)
	
	if T < 0 then -- change to positive angle
		set T to T + 2 * pi
	end if
	
	-- map to first or fourth quadrant with -pi/2 ≤ T ≤ pi/2
	set cos_sign to 1
	if T > (pi / 2) and T ≤ (3 * pi / 2) then
		set cos_sign to -1
		set T to pi - T
	end if
	
	set cos_angle to (pi / 2) - (abs T)
	set end of results_list to {cos_sign * (sin cos_angle), sin T} -- first result is cosine of original angle
	delay 0.2
	--say (n as string)
end repeat
results_list

Edited the script. Something is not right with my AppleScript Editor or Satimage osax. The script hangs sometimes. Added the delay.

Hello Kel.

I have been pondering this a little, and still I find the best way, to rework expressions, so that I can use atan2 to return the result. But if you know your result is to lie within 0.180, then you can, as you know replace sin x with cos 90 -x, since cosinus, returns an ok value for the range 0…180 whereas sin returns from −90 degrees to 90 degrees, due to the 90 degrees shift in period. And of course the opposite when you know you are looking for an angle in the range −90 to 90 degrees. Otherwise, you can deduce it by looking at the sign of both sin and cos for the angle. (expensive, but never fails.)

sin = +, cos = + first quadrant.
sin = +, cos = − second quadrant
sin = -, cos = - third quadrant
sin = -, cos = + fourth qudarant.

Have a nice day Kel! :slight_smile:

Hi McUsr,

The reason I used sine is because sine works for -pi/2 through pi/2 where it is -1 and 1 respectively and including at 0 angle. In other words it works on the y axis and positive x part of the x axis. So, sine doesn’t work riight only at pi. On the other hand cosine doesn’t work right at both -pi/2 and p/2. Hence, no matter what quadrant you choose for using cosine, it doesn’t work on at least one of the axis bordering any quadrant.

Still working on studying and incorporating your modulus script and thinking about maybe using degrees. But anyway, to use Satimage osax and many others, degrees still needs to be converted to radians.

I was rereading Wikipedias section on Trigonometry and change my mind. It is quite complete. Btw, I never heard of side-angle formulas either. To me it was always half-angle.

Thanks a lot,
kel