[INC] Math functions [Updated - 02/08/2010] -
ettans - 06.02.2010
Quote:
Mathematical functions
* Written by Ettans (ettans@purgatoryserver.com)
* Last modified 02/07/2010
* If you use this include, please give proper credit.
|
This include (na_math.inc) is written for SA:MP and VC:MP (PAWN server), adding additional mathematical functions
for any scripter to use. For an example, you can use these functions to add mathematical minigames to your servers and reward
players when they answer correctly.
Note: I'm really looking forward to decent suggestions, as I intend to keep this updated.
Setup:
Extract na_math_include.zip and copy the na_math.inc to your /pawno/includes/ folder. Open your gamemode and add #include <na_math> to the top. You're all done!
Functions:
Description: Determines the greatest common divider of two numbers using Euclid's algorithm
Example usage: euclid_gcd(6,12);
// Will return 6
Example usage: euclid_gcd(25,5);
// Will return 5
Description: Determines the least common multiple of two numbers using Euclid's algorithm
Example usage: euclid_lcm(4,
;
// Will return 8
Example usage: euclid_lcm(12,6);
// Will return 12
Description: Returns the absolute value of an integer
Example usage: abs(4);
// Will return 4
Example usage: abs(-6);
// Will return 6
Description: Returns the value of E^x, where E is Euler's constant and x is the number passed to it
Example usage: exp(-1);
// Will return 0.3678794
Example usage: exp(5);
// Will return 148.4131591
Description: Convert a degree to a radian number
Example usage: deg2rad(6);
// Will return 0.1047198
Example usage: deg2rad(10);
// Will return 0.1745329
Example usage: deg2rad(10.5);
// Will return 0.1832596
Description: Convert a radian number to a degree
Example usage: rad2deg(2);
// Will return 114.59156
Example usage: rad2deg(5);
// Will return 286.4789
Example usage: rad2deg(2.4);
// Will return 137.509872
Example commands:
Code:
printf("Euler's constant (1) %f",exp(1));
printf("5 Degrees to radians %f",deg2rad(10));
Code:
/*
* Requires you to have string pre-defined somewhere in your script, mostly at the top of OnPlayerCommandText.
* Simply copy these commands to your OnPlayerCommandText callback.
*/
// Greatest common divider
if(strcmp(cmd, "/euclid_gcd", true) == 0)
{
new val_x1 = strval(cmdtext[12]);
new val_x2 = strval(cmdtext[14]);
format(string,sizeof(string),"(Greatest common divider) Result: %d",euclid_gcd(val_x1,val_x2));
SendClientMessage(playerid,0xFFFFFFFF,string);
return 1;
}
// Least common multiple
if(strcmp(cmd, "/euclid_lcm", true) == 0)
{
new val1 = strval(cmdtext[12]);
new val2 = strval(cmdtext[14]);
format(string,sizeof(string),"(Least common multiple) Result: %d",euclid_lcm(val1,val2));
SendClientMessage(playerid,0xFFFFFFFF,string);
return 1;
}
// Absolute value
if(strcmp(cmd, "/abs", true) == 0)
{
new val = strval(cmdtext[9]);
format(string,sizeof(string),"Absolue value: %d",abs(val));
SendClientMessage(playerid,0xFFFFFFFF,string);
return 1;
}
Download link of the include: uploadFFS (Contains the include and a Readme document)
Re: [INC] Math functions -
Karlip - 06.02.2010
nice
Re: [INC] Math functions -
[WSM]Deadly_Evil - 06.02.2010
Nice Include its helpful for making Mathematic Games
Re: [INC] Math functions -
¤Adas¤ - 06.02.2010
Nice. The "abs" can be simplier:
pawn Code:
stock abs(value) return value < 0 ? -value : value;
Re: [INC] Math functions -
ettans - 06.02.2010
Oh, thanks for that Adas. Haven't used that kind of style in PAWN. :P
Re: [INC] Math functions -
¤Adas¤ - 06.02.2010
No problem.
When you are doing something, try to simplify it as possible. My style.
Re: [INC] Math functions -
»»»Hakam - 06.02.2010
So what is this exactly, I didn't understand.
~Hakam.
Re: [INC] Math functions -
ettans - 06.02.2010
Read the functions part again.
Re: [INC] Math functions -
»»»Hakam - 06.02.2010
Ah. Now I got it. I tested it also! Good Job.
~Hakam.
Re: [INC] Math functions [Updated - 02/08/2010] -
ettans - 08.02.2010
Updated! Added 3 new functions:
- exp(Float:num);
- deg2rad(Float:deg);
- rad2deg(Float:rad);