Raised This Month: $12 Target: $400
 3% 

Float Modulo Operations


  
 
 
Thread Tools Display Modes
Author Message
Anpheus
Senior Member
Join Date: Aug 2004
Old 04-23-2005 , 12:20   Float Modulo Operations
#1

Edit: I changed this page for arithmatic based modulo. Should be a bit faster. And I must note that as with all floating point operations, accuracy may vary.

Code:
/* forbidden operations */ forward operator%(Float:oper1, Float:oper2); forward operator%(Float:oper1, oper2); forward operator%(oper1, Float:oper2);

This is incorrect... Assuming you only want to return unsigned values in the range [0, oper2)...
Code:
stock Float:operator%(Float:num, Float:modulo)     return num - modulo * floatround(num / modulo); stock Float:operator%(Float:num, modulo)     return num % float(modulo); stock Float:operator%(num, Float:modulo)     return float(num) % modulo;

Now, if you want 'simple' support for signed values, use this.

To get a signed byte, a range of [-128 to 12*: a % -256.0
Code:
stock Float:operator%(Float:num, Float:modulo) {     new Float:x = num / modulo;     if (modulo < 0) {   //Act as though it's a wrapping value (signed)         return (x - floatround(x+0.5,floatround_floor)) * modulo     } //else:     return num - modulo * floatround(-x,floatround_floor); } stock Float:operator%(Float:num, modulo)     return num % float(modulo); stock Float:operator%(num, Float:modulo)     return float(num) % modulo;
*: This is using mathematical notation for ranges, as in:
[a, b] denotes that the value can be anywhere between a and b, and also equal to a or b.
(a, b) denotes that the value can only be between a and b, not equal to either.
Thus, [-128, 12 means the value can be equal to -128, and anywhere between -128 and 128. It cannot be equal to 128, however. Also, floatround(127.5 % 256) would be equivalent to floatround(127.5) % 128.
Anpheus is offline
Anpheus
Senior Member
Join Date: Aug 2004
Old 04-27-2005 , 17:27  
#2

No matter what I do, I can't get AMX to compile a plugin with mixed-tag modulo operations without crashing.

Is this something coded in the compiler?

Oh, here's an example:
Code:
#include <amxmodx> #include <engine> public plugin_init() {     new Float:test = 12345.67890 % 0.123456789;     new Float:test_two = test % test;     client_print(0, print_chat, "%f^n", test);     client_print(0, print_chat, "%f^n", test_two);     return PLUGIN_HANDLED; }

That compiles... this does not:

Code:
#include <amxmodx> #include <engine> public plugin_init() {     new Float:test = 12345.67890 % 0.123456789;     new Float:test_two = test % test;     new Float:test_three = test % floatround(test); //  new Float:test_four = floatround(test) % test;     client_print(0, print_chat, "%f^n", test);     client_print(0, print_chat, "%f^n", test_two);     client_print(0, print_chat, "%f^n", test_three); //  client_print(0, print_chat, "%f^n", test_four);     return PLUGIN_HANDLED; }

Or...

Code:
#include <amxmodx> #include <engine> public plugin_init() {     new Float:test = 12345.67890 % 0.123456789;     new Float:test_two = test % test; //  new Float:test_three = test % floatround(test);     new Float:test_four = floatround(test) % test;     client_print(0, print_chat, "%f^n", test);     client_print(0, print_chat, "%f^n", test_two); //  client_print(0, print_chat, "%f^n", test_three);     client_print(0, print_chat, "%f^n", test_four);     return PLUGIN_HANDLED; }
Anpheus is offline
Johnny got his gun
Veteran Member
Join Date: Jan 2004
Location: Tokyo
Old 05-01-2005 , 17:06  
#3

You should never do modulus operations using decimals.
Johnny got his gun is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:41.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode