[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


Messages In This Thread
float2.inc - Additional operators for floats - by kristo - 14.07.2015, 09:36
Re: float2.inc - Additional operators for floats - by kristo - 22.07.2015, 17:32
Re: float2.inc - Additional operators for floats - by CodeStyle175 - 23.07.2015, 09:20
Re: float2.inc - Additional operators for floats - by kristo - 23.07.2015, 09:32
Re: float2.inc - Additional operators for floats - by CodeStyle175 - 23.07.2015, 09:36
Re: float2.inc - Additional operators for floats - by kristo - 23.07.2015, 09:42

Forum Jump:


Users browsing this thread: 1 Guest(s)