• Welcome to Vice City Multiplayer.
 

[INC] Some math functions

Started by Ettans, February 06, 2010, 01:44:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ettans

QuoteMathematical functions

* Written by Ettans ([email protected])
* 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:

  • euclid_gcd(a,b);
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

  • euclid_lcm(a,b);
Description: Determines the least common multiple of two numbers using Euclid's algorithm

Example usage: euclid_lcm(4,8); // Will return 8
Example usage: euclid_lcm(12,6); // Will return 12

  • abs(a);
Description: Returns the absolute value of an integer

Example usage: abs(4); // Will return 4
Example usage: abs(-6); // Will return 6

  • exp(Float:num);
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

  • deg2rad(Float:deg);
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

  • rad2deg(Float:rad);
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:

> May need editing, as it's meant for SA:MP.


printf("Euler's constant (1) %f",exp(1));
printf("5 Degrees to radians %f",deg2rad(10));


/*
* 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)

[AoD]NC

Good, but how can this be useful in game?

QuotePM >>> To register type /c register YOUR PASS and what is the absolute value of -2
:D

Ettans

Math games and a lot of other areas.

Boss


Ettans

As in minigames? Don't tell me this is the first time you hear about something like that...  :-\

Skirmant

Quote from: Ettans on February 06, 2010, 02:41:48 PM
Math games and a lot of other areas.

The only "math games" I know are Poker, Chess and Monopoly ;D
There all about strategy mostly. I would like to see a REAL math game on a vcmp server.
I know advance mIRC, average C++, basic Pawn & Squirrel, very basic Java Script.

Pussycat_Dolls

math games,yeah I've noticed that in Sa:MP,but I never tried to answer....
Assassins Secret Society o_O

SilenusShar

this is just what we need, why doesn't pawn have these as standard libraries though ?

thijn

I don't see why this could be usefull


SilenusShar

Quote from: thijn on February 07, 2010, 09:54:14 AM
I don't see why this could be usefull

how are you calculating, distance, height, speed, velocity, altitude and any other maths calculations thijn? with thin air ?

if its not using any maths functions, please share it with us...

[AoD]NC

Distance, height or speed dont need so advanced maths. It needs only +, -, * and / :p.

Ettans

Euclid's algorithm or absolute value of a number is definately NOT advanced mathematics, in fact, it's basic and you learn it in pre-high. To the people who don't understand the need of basic mathematical functions in PAWN: If you have no need for them, then of course you don't see the potential use of them. In a 3D game, math has a very important part and you can use it to create highly-complex functions.

Ettans

Updated the first post! Added 3 new functions: exp, deg2rad and rad2deg.

Javi

I've not done anything with Pawn yet, but I'm curious, is there a way of controling decimals to a division? This is what I mean:

2 / 3 = 0.666666...

Is there a way of making it return 2 decimals as an example?

Boss

Quote from: Forze on February 08, 2010, 01:30:10 PM
I've not done anything with Pawn yet, but I'm curious, is there a way of controling decimals to a division? This is what I mean:

2 / 3 = 0.666666...

Is there a way of making it return 2 decimals as an example?
I didn't quite understand what do you mean, but you can decide the amount of numbers following the dot like this:

format(str,sizeof(str),"%.1f",1.6666); // formats str as "1.6"


Or a mathematical way:

new Float:fl = 1.6666;
fl = fl*10; // fl is 16.666
fl = floatround(fl); // fl is 17
fl = fl/10; // fl is 1.7