Floats
From SA-MP Wiki
Floats are a type of tagged variable in PAWN and they are "floating point numbers", hence the name Float. This basically means that they support decimal places, such as 1.2, 1.1, 5.32, 64.21 - where as normal integers (untagged) don't support decimal places.
Contents |
Initializing a Float variable
To initialize a Float variable, all you have to do is assign it the Float tag:
new Float:fMyFloat;
You can now assign it floating point numbers:
new Float:fMyFloat; /* All of the assignments below are valid examples. */ fMyFloat = 5.3; fMyFloat = 5.33; fMyFloat = 5.395293; new Float:fMyHugeFloat = 9995499.24; new Float:fMyFloatPI = 3.1415926535; /* ... */
Some examples
Get the players position
//Create variables for each coordinate. new Float:X, Float:Y, Float:Z; //Assign each variable the players position. GetPlayerPos( playerid, X, Y, Z );
Dropping a player
//Define our offset to drop them at. const Float:DROP_HEIGHT = 50.0; //Create a new Float array to store each coordinate into. new Float:a_fPlayerPos[ 3 ]; //Assign the newly created array each coordinate (X, Y, Z). GetPlayerPos( playerid, a_fPlayerPos[ 0 ], a_fPlayerPos[ 1 ], a_fPlayerPos[ 2 ] ); //Now that we have the position of the player, we can set their Z position to their current one, plus the offset. SetPlayerPos( playerid, a_fPlayerPos[ 0 ], a_fPlayerPos[ 1 ], (a_fPlayerPos[ 2 ] + DROP_HEIGHT) );
Creating a sine wave of objects
const nObjModel = 1337; const nSteps = 100 ; const nAmp = 15 ; const Float:nFreq = 0.03; //Make a new array, assign the players position to it. new Float:cPos[ 3 ]; GetPlayerPos( playerid, cPos[ 0 ], cPos[ 1 ], cPos[ 2 ] ); //Run for the specified amount of iterations. for( new i = (0); i != nSteps; i++ ) { //The formula to calculate the position of the object. new Float:formula = (floatsin(i*nFreq)*nAmp); //Create an object in place of the sine wave iteration. CreateObject( nObjModel, cPos[ 0 ], cPos[ 1 ] + i, ( cPos[ 2 ] + formula ), 0, 0, 0 ); }
Why are Floats important?
Floats are important because they are used through SA-MP's native functions, mostly for the coordinate system of GTA:SA. Floats are however, used for other things than just the coordinate system - such as the player's health, armour, vehicle health, velocity, rotation and lots more.
Converting floats
Most of the time floats need to be converted into a different type, such as an integer or even a string. Here are a couple of conversion samples:
Floats and Strings
Float -> String
A Float can be converted to a string by using %f in format:
new Float:fMyFloat = 53.155; new szStr[ 32 ]; format( szStr, sizeof szStr, "%f", fMyFloat ); //szStr is now "53.155".
It should be noted that if you want to convert a float to a string to a certain amount of decimal places, you'd specify it between the % and the f of %f, in the format %.(number of decimal places)f:
new Float:fMyFloat = 64.532115; new szStr[ 32 ]; format( szStr, sizeof szStr, "%.2f", fMyFloat ); //To 2 decimal places //szStr is now "64.53". format( szStr, sizeof szStr, "%.3f", fMyFloat ); //To 3 decimal places //szStr is now "64.532". format( szStr, sizeof szStr, "%.4f", fMyFloat ); //To 4 decimal places //szStr is now "64.5321".
By default, if no precision is set (It's just %f), then a string will only hold a floating point value to a maximum of 6 decimal places.
String -> Float
A string can be converted to a Float using the function floatstr:
new szStr[ 4 ] = "5.1"; new Float:fMyFloat = floatstr( szStr ); //fMyFloat is now 5.1.
Floats and Integers
Float -> Integer
Converting a float to an integer is quite easy, as an integer doesn't have a tag and a float does. All we have to do is change the tag:
new Float:fMyFloat = 1.3; new nSomeInt = _:fMyFloat; //nSomeInt is now 1.
You can also convert floats to integers with floatround:
new Float:fMyFloat = 1.3; new nSomeInt = floatround( fMyFloat ); //nSomeInt is now 1.
Integer -> Float
Converting an integer to a Float can be done various ways:
new nSomeInt = 5; new Float:fMyFloat; //By changing the tag: fMyFloat = Float:nSomeInt; //By using float(): fMyFloat = float( nSomeInt ); //Or just by assigning it: fMyFloat = nSomeInt; //fMyFloat is now 5.0.
The float library
The float library can be included into your script by inserting the following code:
#include <float>
However, this is quite useless, as a_samp includes this library by default; and it is probably already included.
Float library functions
Here is a list of functions provided by the float library:
- float - Converts integer to a float.
- floatabs - Return the absolute value of a float.
- floatadd - Adds two floats together.
- floatcmp - Compares two floats.
- floatcos - Gets cosine from angle.
- floatdiv - Divides two floats.
- floatfract - Returns the fractional portion of a float.
- floatlog - Gets logarithm.
- floatmul - Multiplies two floats.
- floatpower - Raises given value to a power of exponent.
- floatround - Rounds float to integer.
- floatsin - Gets sine from angle.
- floatsqroot - Calculates square root of given value.
- floatstr - Converts string to float.
- floatsub - Subtracts two floats.
- floattan - Returns tangent from angle.
Documentation
The full documentation for the float library can be found here: