### Re: Trig and Maptools

Posted:

**Fri Jul 03, 2009 11:00 pm**Someone, or perhaps a lot of someones, should develop a Lib:Math token to share.

Page **2** of **3**

Posted: **Fri Jul 03, 2009 11:00 pm**

Someone, or perhaps a lot of someones, should develop a Lib:Math token to share.

Posted: **Sat Jul 04, 2009 12:27 am**

And copy it to the wiki with the appropriate links from the Math category.

Posted: **Sat Jul 04, 2009 6:29 am**

zEal wrote:Is there any way of having it spit out more?Akodo Makama wrote:I just copypasted what MatLab spit out at me.

1/2*Pi - x -1/6*x^3

-3/40*x^5

-5/112*x^7

-35/1152*x^9

-63/2816*x^11

-231/13312*x^13

-143/10240*x^15

-6435/557056*x^17

-12155/1245184*x^19

-46189/5505024*x^21

-88179/12058624*x^23

-676039/104857600*x^25

-1300075/226492416*x^27

-5014575/973078528*x^29

-9694845/2080374784*x^31

-100180065/23622320128*x^33

-116680311/30064771072*x^35

-2268783825/635655159808*x^37

-1472719325/446676598784*x^39

-34461632205/11269994184704*x^41

-67282234305/23639499997184*x^43

-17534158031/6597069766656*x^45

-514589420475/206708186021888*x^47

-8061900920775/3448068464705536*x^49

-5267108601573/2392537302040576*x^51

-61989816618513/29836347531329536*x^53

e < 10^-30 good enough (10^-20 only needs up to term 37)? The recurrence formula is:

[code]N^2 * u(N) + (-n^2 – 3n – 2) * u(n+2) = 0

U(0)=1/2*pi(), U(1) = -1, n>=0[code], should you require a higher order equation

Posted: **Sat Jul 04, 2009 6:56 am**

More than enough, thanks a lot.

Posted: **Sun Jul 05, 2009 1:16 am**

zEal wrote:More than enough, thanks a lot.

I missed something earlier. My error values were only for values 'close' to (within 0.25 of) the origin (0 in this case).

The error at 0 is 0 after only the pi()/2 term

But the error at 0.5 needs 37 terms to drop the error to 7.8e-15

To get e-15 errors at 0.9, we need 131 terms

Even then (131 terms) the error at 1 would still be 5e-2. That's 0.05, or 1/20.

I should have looked closer, as this is a well known problem with Taylor polynomial expansions. All the coefficients have the same sign. That's bad news for extreme input values (close to 1 or -1). There is a fix (use multiple expansions from different central points with weighted averaging or piecewise definition), but it's a bit ugly (as acos(x) is undefined at x>1), and I'll need time to figure out how what we'll need for e-15 errors from [-1..1]

All the other expansions likely suffer from the same problem.

Posted: **Sun Jul 05, 2009 6:14 am**

Ok, given asin, acos is simple

acos(x) = 2 * asin( sqrt( (1-x)/2 ) )

You'd think I'd remember little things like that. It was only 10 years ago I used these things on a regular basis.

acos(x) = 2 * asin( sqrt( (1-x)/2 ) )

You'd think I'd remember little things like that. It was only 10 years ago I used these things on a regular basis.

Posted: **Mon Jul 06, 2009 12:27 am**

I remember a proof on an exam that was worth extra credit. I didn't even know where to start:

e * i^pi + 1 = 0

I think that was calc3, IIRC. Fortunately, I don't use that kind of higher math at all on a regular basis.

e * i^pi + 1 = 0

I think that was calc3, IIRC. Fortunately, I don't use that kind of higher math at all on a regular basis.

Posted: **Mon Jul 06, 2009 3:20 am**

Azhrei wrote:I remember a proof on an exam that was worth extra credit. I didn't even know where to start:

e * i^pi + 1 = 0

I think that was calc3, IIRC. Fortunately, I don't use that kind of higher math at all on a regular basis.

You mean e^(i*pi()) +1 =0 , right?

Posted: **Tue Oct 13, 2009 12:04 am**

This will pick a random angle between 1 and 90, and give you a good approximation of sine and cosine for that angle:

And to answer the previous poster's question about why you would want this: I put together a Macro for doing a scatter function for Warhammer 40K. By those rules, you get an angle and a distance to move the token. So I needed to create a macro that would determine a random angle and distance, and then move a selected token by that amount in the right direction.

Code: Select all

`[h: myAngle = 1d90]`

[h: myRadians = divide(myAngle, 57.2958)]

[h: sinAngle = myRadians - divide(power(myRadians, 3),6)]

[h: sinAngle = sinAngle + divide(power(myRadians, 5),120)]

[h: sinAngle = sinAngle - divide(power(myRadians, 7),5040)]

[h: cosAngle = 1 - divide(power(myRadians, 2),2)]

[h: cosAngle = cosAngle + divide(power(myRadians, 4),24)]

[h: cosAngle = cosAngle - divide(power(myRadians, 6),720)]

Angle: [myAngle]<br>

SIN(Angle): [sinAngle]<br>

COS(Angle): [cosAngle]<br>

And to answer the previous poster's question about why you would want this: I put together a Macro for doing a scatter function for Warhammer 40K. By those rules, you get an angle and a distance to move the token. So I needed to create a macro that would determine a random angle and distance, and then move a selected token by that amount in the right direction.

Posted: **Sat Mar 27, 2010 10:35 am**

RPTroll wrote:Someone, or perhaps a lot of someones, should develop a Lib:Math token to share.

Well, since you asked ...

I included all the trig & math functions in this thread:

- pi
- degrees
- radians
- sine
- cosine
- factorial
- modulo

- robust
*eval*() - my custom wrapper for
*number*(), which returns 0 (or specified default value) if the argument is**not**a number. - my custom
*math.list*(). Requires 2 arguments: function name and a string list, or json array, of values. It lets you perform math functions like max, sum, etc. on a list of values. ex. math.list( 'sum', valueList ).

[EDIT: aliasMask has kindly posted a larger version with even more functions, without the custom 'extras' included here. His versions are probably more accurate.

Newer version (v2.1).

]

Posted: **Sat Mar 27, 2010 1:34 pm**

great work there biodude!

Also, just while I'm typing I was wondering whether there were any functions in MT that can assess whether a number is odd or even? I can't find anything on the list of functions.

(not meaning to de-rail but it seems that it's a fairly good match as it may be a another useful math udf)

Mal

Also, just while I'm typing I was wondering whether there were any functions in MT that can assess whether a number is odd or even? I can't find anything on the list of functions.

(not meaning to de-rail but it seems that it's a fairly good match as it may be a another useful math udf)

Mal

Posted: **Sat Mar 27, 2010 3:57 pm**

determining if a number is even or odd is a common task, but also fairly trivial:

which is why I don't think anyone has ever written an entire UDF for it. Would such a thing be useful to people?

Code: Select all

`[H: evenOrOdd = if( floor( number / 2 )==( number / 2 ) , "even", "odd" )] `

which is why I don't think anyone has ever written an entire UDF for it. Would such a thing be useful to people?

Posted: **Wed Jun 23, 2010 1:05 am**

I took the lib Biodude made and added some stuff. I did remove the eval and list sum functions though. This is my own personal version, so feel free to use and modify as you will. Here's a list of functions:

**Lib:Math**

**onCampaignLoad -**Loads all the functions as UDFs in the form math.*function***angleToSlope(angle): slope -**Converts the angle formed by the hypotenuse and x-axis of a right triangle to a slope value.**cos(value): radians -**Cosine**sin(value): radians -**Sine**tan(value): radians -**Tangent**toDegrees(radians): degrees -**Convert radians to degrees**factorial(start[,end,step]) -**Does factors from start to end and allows skips with step parameter (defaults, end = 0, step = -1)**toRadians(degrees): radians -**Convert degrees to radians**arcCos(value): radians -**Inverse Cosine**arcSin(value): radians -**Inverse Sine**arcTan(value): radians -**Inverse Tangent**cahAngle(adjacent,hypotenuse): degrees -**Returns the angle opposite the Adjacent and Hypotenuse of a right triangle (SOACAHTOA)**slopeToAngle(slope): degrees -**Returns the arcTangent of a slope**sohAngle(opposite,hypotenuse): degrees -**Returns the angle opposite the Opposite and Hypotenuse of a right triangle (SOACAHTOA)**toaAngle(opposite,adjacent): degrees -**Returns the angle opposite the Opposite and Adjacent of a right triangle (SOACAHTOA)**div(number,divisor): integer -**Returns the integer of the number divided by the divisor**mod(number,divisor): integer -**Returns the integer of the remainder of the number divided by the divisor**val(string): number -**Forces a string value to a number where if invalid returns 0**pi(): number -**Returns the value of PI to 20 decimal places**isOdd(integer): boolean -**Returns 1 or 0 if integer is odd or even (respectively). If not an integer returns ""

Posted: **Wed Jun 23, 2010 1:42 am**

Careful, aliasmask ... you might get highschoolers taking trig stopping by RPTools and downloading that, even if they have no interest in playing actual RPGs via MapTool!

Posted: **Wed Jun 23, 2010 1:49 am**

Ha! But they'll have to learn the MACRO language first. Doing this brought back some of my highschool/college day memories.

And for any highschoolers... go to these pages:

That's where I got most of my formulas

And for any highschoolers... go to these pages:

- http://www.cc.gatech.edu/gvu/multimedia/nsfmmedia/graphics/elabor/math/mathfaq_lines.html
- http://en.wikipedia.org/wiki/Trigonometric_functions.
- http://en.wikipedia.org/wiki/Inverse_trigonometric_functions

That's where I got most of my formulas