[Include] float2.inc - Additional operators for floats
#1

Introduction
This (really) small include adds some additional operators to simplify working with floats.


Modulus operator for floats
When you're trying to use modulus operator with floats, you will get an error like these:
Код:
error 004: function "operator%(Float:,Float:)" is not implemented
error 004: function "operator%(_:,Float:)" is not implemented
error 004: function "operator%(Float:,_:)" is not implemented
This include fixes these errors and adds support of modulus operator for floats, so operations like this will work properly:
pawn Код:
printf("123.456 %% 22.33 = %f", 123.456 % 22.33);
printf("666 %% 22.33 = %f", 666 % 22.33);
printf("123.456 %% 14 = %f", 123.456 % 14);
Alternatively, you can use:
pawn Код:
native Float:floatmod(Float:oper1, Float:oper2);
Easy float -> integer conversion
I'm not 100% sure if I should leave this feature in, as it removes "tag mismatch" warnings in some cases and it might cause some confusion. It adds a possibility to assign float values to a non-float variable:

pawn Код:
new Float:val1 = 33.123, val2;
val2 = val1;
printf("val2 = %i", val2); // val2 = 33
Example script
pawn Код:
new Float:val1 = 123.456, Float:val2 = 3.333, val3 = 16;
   
printf("floatmod");
printf("val1 %% val2 = %f", val1 % val2);
printf("val1 %% val3 = %f", val1 % val3);
printf("val3 %% val2 = %f", val3 % val2);
   
printf("----------");
   
printf("floatround");
printf("val1 = %f", val1);
printf("val3 = %i", val3);
val3 = val1;
printf("val3 = %i", val3);
Outputs:
Код:
[14.07.2015 12:00:12] floatmod
[14.07.2015 12:00:12] val1 % val2 = 0.134994
[14.07.2015 12:00:12] val1 % val3 = 11.456001
[14.07.2015 12:00:12] val3 % val2 = 2.667999
[14.07.2015 12:00:12] ----------
[14.07.2015 12:00:12] floatround
[14.07.2015 12:00:12] val1 = 123.456001
[14.07.2015 12:00:12] val3 = 16
[14.07.2015 12:00:12] val3 = 123
Download Sorry for the lack of explanations, I'm not confident in my english. I tried to compensate it by adding a lot of examples.
Reply
#2

Updated a little bit:
  • floatmod (%) returns FLOAT_NAN if oper2 is 0.0. It would have caused infinite loops.
  • FLOAT_(POS_)INFINITY, FLOAT_NEG_INFINITY and FLOAT_NAN defined in the include. The include should be included after YSI as it has #if defined checks, while YSI doesn't.
Reply
#3

But where i should use it and for what?
Reply
#4

If you didn't understand, what it's for, then you probably won't need it. Modulus operator is used to find the remainder of a division. You probably learnt it in ~fourth grade.

Quote:

17 / 4 = 4
4 * 4 = 16
17 - 16 = 1
---
17 % 4 = 1

In PAWN (also in the latest version at CompuPhase - downloaded the latest package just to check this out), modulus is forbidden/unimplemented.
Reply
#5

But for what is then created function floatround, its so simple to use and it doesnt need some extra include.
Reply
#6

pawn Код:
new Float:val1 = 33.123, val2;
val2 = val1;
printf("val2 = %i", val2); // val2 = 33
variable = Float:value is actually a mirroring operator for floatround. The line which allows assigning float values to integer variables is here:

pawn Код:
native operator=(Float:oper) = floatround;
These codes are equivalent:
pawn Код:
new Float:val1 = 33.123, val2;
val2 = val1;
printf("val2 = %i", val2); // val2 = 33
pawn Код:
new Float:val1 = 33.123, val2;
val2 = floatround(val1);
printf("val2 = %i", val2); // val2 = 33
I actually don't recommend using this part of the include and i'm still thinking of removing it as it might cause confusion and I prefer to see the tag mismatch warning.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)