sin pi

Hi,

Why isn’t:

sin pi

equal to zero in Satimage osax?

Edited: ah, it’s the accuracy of pi. Does anyone know how to make it come out to zero and still maintain the other values ( by the other values I mean like sin (pi/2)?

Thanks,
kel

Hello.

I think you’ll have to perform a test outside of the sin command verb, if you are to retain the functionality of sin. :slight_smile:

[applescriptset a to (sin pi)
if (abs a) < 2.0E-16 then set a to 0



[b]Edit[/b]

I convoluted a with the abs verb, so it works both ways.

I was thinking of just working with the first quadrant might do it, but need to transform this to the second, third and forth quadrants. It’s been a while using trig and math in general. My friend borrowed my books and threw them away.

Edited: because sin (pi/2) results in 1.0.

See here:

sin -(pi / 2)

→ -1.0

Nevermind. I remember now. Say you had an angle of (5/6)*pi, then that’s the same as an angle of (1/6)*pi whose sine is the same but cosine is opposite in sign.

Thanks anyway,

If precision is really important in an AppleScript trig calculation you are better off using bc -l, where the -l switch brings in better precision and some trig functions. See the man page for bc. Pi is not included among its constants, but can be defined to 10 decimal places within bc by using pi =$(echo "scale = 10; 4*a(1) | bc -l) where a() is arctangent (returns radians) in your script. This relies on the fact that arctan(1) = pi/4

Hello.

Sorry about your books, I recommend Thomas Calculus 10th edtion second hand from Amazon.

But, really. Wikipedia excels as math: just have a look at: this page and this page and this page and this… :slight_smile:

As Adam stated: bc is here.
And you really have 40 decimals under the hood with bc, setting the scale to 40, but you get a long way with 15 signifcant digits you have available with AppleScript.

And if you use atan2 under applescript, you get the angle back, adjusted for quadrant, cool that it is implemented in Satimage.osax.

Hi McUsr,

Your links aren’t working but i can search wikipedia. I don’t think I ever did that for math books. :slight_smile:

Still thinking about your statements about precision. bc sounds good. I’ve heard about that in unix. In AppleScirpt I think I only get 12 digits. Is the other 4 digits hidden?

Thanks,
kel

Hello.

The digits aren’t hidden really, but if you want more than 12 digits precision in the script then you’ll have to use e-notation, like so: 2E-16 is a number with 16 digits, but the precision is only 15.5 digits, so 1E15 is precise, 1E-16 is not.

Hi McUsr,

I passed out yesterday and woke up this morning! :smiley:

I was thinking that I might just do something like this probably expand it for theta > pi:

set theta to pi
if theta > (pi / 2) and theta ≤ pi then
	set theta to pi - theta
end if
sin (theta)

I’ve searched for good free trig and college algebra books, but couldn’t find any in the past. Those basic books are expensive nowadays and they’re soft cover books.

Have a good day,
kel

Hello.

It is of course best to have one book, instead of googling. Last time I looked, I found a full Calculus book out there, in PDF. Later on, I bought one in Hardcover, which is a nice thing to have.

Penney, and Thomas Calculus can be gotten in hardcover, and I have heard that they are cheap as second hand on Amazon, and maybe e-bay.

Hi McUsr,

I still have my other math books. I just lent out the two basic ones College Algebra and Trigonometry. But, those are the ones I used to use most for reference besides the Linear Algebra books. Wikipedia has a good quick reference and that might be good enough although it doesn’t cover everything I think.

Thanks,
kel

Hello.

I can recommend Marvin Marcus An introduction to Pascal and Precalculus. It covers fundamental trigonometry pretty good, but it is not for reference. The programming examples are also fun, and easy to convert to AppleScript. It is old, and should cost you around a dollar + package and handling online. You can also find it at google books.

You’ll also see that Wikipedia covers a whole lot of it, if not everything, but the problem can be finding precisely what you are looking for, that is at least my experience. I was looking for instance for the half-angle formula, for a long while, while I should have been searching for the half-side formula. :slight_smile:

Hello.

I forgot to post this script that computes the number of digits that is used for Math. I have stolen it from Jean-Meeus Astronomical Algorithms page. 17. Hopefully this comes to use with Maverick! :slight_smile: ( But I guess I would have heard it by now, if AppleScript starts using 32 digits (or more!)).

# accuracy test
# 15.6 internal digits
set x to 1
set j to 0
repeat while true
	set x to x * 2
	if x + 1 ≠ x then
		set j to j + 1
	else
		exit repeat
	end if
end repeat
set numdigits to (j * 0.30103) as text

The constant 0.30103 is log10 2 (the base 10 logarithm of 2 ).

Hello.

Now, that is a conversion, I’d rather do outside of the sine function. But I have discovered that many “professional” packages do map the argument of the trigonometric functions down into to a range of 360 in degrees, I guess they do that for getting higher precisision, or avoiding some sort of overflow, or maybe they think they do that mapping faster than for instance the sine function. Anyway, I think it is a smart thing to do. :slight_smile:

Edit
Such preparations of the argument to the trigonometric functions, has of course something to do with the anticipated magnitude of them, nothing you would do if you thought the arguments to be within a couple of thousand degrees (in radians).

Hi McUsr,

One of the hard things about this to me is that cosine doesn’t work also, but at different angles. I was checking if python had the same problem with pi and came up with this tester:

set t to "#
from math import *
exp1 = exp(1e-5) - 1
exp2 = expm1(1e-5)
print exp1, exp2, sin(pi/2), sin(pi), cos(pi/2), cos(pi)
#"
set cmd to "python -c " & quoted form of t
do shell script cmd

(*
>>> from math import exp, expm1
>>> exp(1e-5) - 1  # gives result accurate to 11 places
1.0000050000069649e-05
>>> expm1(1e-5)    # result accurate to full precision
1.0000050000166668e-05
*)

I just put that exponent stuff in there because was wondering how they could get full precision. :confused:

Edited: oops, should have deleted that result from another test.

Have a good day, :slight_smile:
kel

Hello.

I made a couple of handlers for you, that aligns the result of the arcus function back to the right quadrant.

I also made a handler that identifies the right quadrant of the unity circle an angle is in.

This was just some thoughts about it. It is so much easier when you sit with your pocket calculator, and does the calculations, because then you know which quadrant the result is supposed to be in.

The sine is the true problem child here, because sometimes you’ll have to add 180, other times, 360, and there is no clue in the size of the returned angle of the arcus function, therefore it is best to get the quadrant, or define it, up front, and then use the cosalign, and sinalign, which you can view as counterparts to the atan2 handler.

set tangle to 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

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

Edit

I added a constant value for testing for negative values with Satimage.osax in the sinalign handler, in order to get
correct results for 180Ë™.

Hi McUsr,

I don’t understand your script. right now. Maybe because I’m distracted. When I change tangle, I just get the angle back. I’ll look into your script more.

Wow, that was fast. I was stuck on where to start and end a quadrant.:slight_smile: I was thinking about other ways to do this too. interesting stuff.

Thanks,
kel

ps. One thing I noticed when running python in the Terminal is that you can see the significant places. But, when it’s returned from the do shell script, the same significant places are lessened. I could probably return the value as string.

Forget what I wrote in the last post about not understanding why I get the angle back. I see now.

Thanks,
kel

I should have looked at what cosalign was at from the beginning. I was thinking that it might be from Satimage.osax. :slight_smile:

Still trying to figure out how to get results for cosine and sine at the different angles.

Later,
kel