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

uint (Operations with unsigned int)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
client21
Senior Member
Join Date: Apr 2013
Old 01-31-2021 , 13:18   uint (Operations with unsigned int)
Reply With Quote #1

uint v1.0.3

As soon as the value exceeds int limit (2147483647), SourcePawn starts to have problems, example:
PHP Code:
int x 2147483650 2// Must be: 1073741825
PrintToServer("%%u = %u\n%%d = %d\n%u > 1000 = %s"xxx1000 "yes" "no"); 
Result: all values are incorrect + comparison does not work:
Code:
%u = 3221225473
%d = -1073741823
3221225473 > 1000 = no
Now you can:
PHP Code:
int x uint(2147483650'/'2);
PrintToServer("%u"x); // 1073741825
PrintToServer("3221225473 > 1000 = %s"uintcmp(32212254731000) > "yes" "no"); // 3221225473 > 1000 = yes 
Before:
PHP Code:
FormatTime(ssizeof(s), "%d.%m.%Y - %H:%M:%S"2147483648);
// plugin crash with error: [SM] Exception reported: Invalid time format or buffer too small 
Now:
PHP Code:
FormatUTime(ssizeof(s), "%d.%m.%Y - %H:%M:%S"2147483648);
PrintToServer(s); // 19.01.2038 - 05:14:08 
FormatTime safe version:

PHP Code:
stock void FormatTime_(char[] bufferint maxlength, const char[] formatint stamp=-1) {
    
FormatTime(buffermaxlengthformatstamp >= -stamp 2147483647); // but int limit = bad

uint.inc
PHP Code:
#define UINT_SIZE 12

/**
 * Math operations with unsigned int ('+', '-', '*', '/', '%')
 * 
 * Example:
 * int i = uint(2147483650, '/', 2);
 * PrintToServer("%u", i); // 1073741825
 * 
 * Note:
 * unsigned int limit (4294967295) is not violated:
 * 4294967295 + 1 = 4294967295
 * 0 - 1 = 0
 * 4294967295 * 2 = 4294967295
 */
native int uint(int aint opint b);

/**
 * Comparison two unsigned int values
 * 
 * Example:
 * bool b = uintcmp(5, 3) >= 0; // true, if 5 >= 3 (instead of operator >=, use any. 0 must be always).
 */
native int uintcmp(int aint b);

/**
 * Replacement FormatTime, because int limit and:
 * 
 * char s[128];
 * FormatTime(s, sizeof(s), "%d.%m.%Y - %H:%M", 2147483648);
 * Plugin crash with error: [SM] Exception reported: Invalid time format or buffer too small
 */
native bool FormatUTime(char[] bufferint maxlength, const char[] formatint stamp=0);

// Convert unsigned int to string (IntToString can't) (return: Number of cells written)
stock int UIntToString(int valuechar[] sint size) {
    return 
FormatEx(ssize"%u"value);
}

// Extract unsigned int from the field (SQL_FetchInt can't)
stock int SQL_FetchUInt(Handle resultsint fieldDBResult &result=DBVal_Errorint error=0)
{
    
char s[UINT_SIZE];
    return 
SQL_FetchString(resultsfieldssizeof(s), result) && result == DBVal_Data StringToInt(s) : error;

Changelog

v1.0.3
Function FormatUTime is faster at 46.72% (cycles for 4294967295 was: 73005, now: 49759)
Attached Files
File Type: zip uint.zip (21.1 KB, 82 views)
File Type: zip uint v1.0.3.zip (22.0 KB, 57 views)

Last edited by client21; 02-01-2021 at 09:58.
client21 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 01-31-2021 , 13:44   Re: uint (Operations with unsigned int)
Reply With Quote #2



Want learn more about C++

thx
Bacardi is offline
kossolax
AlliedModders Donor
Join Date: Jan 2008
Location: Belgium
Old 02-02-2021 , 12:54   Re: uint (Operations with unsigned int)
Reply With Quote #3

Hello,

Are you comparing this below with FormatTime or vs previous version of your extension?
Quote:
Function FormatUTime is faster at 46.72% (cycles for 4294967295 was: 73005, now: 49759)

Thanks,
Steve.
kossolax is offline
client21
Senior Member
Join Date: Apr 2013
Old 02-02-2021 , 21:59   Re: uint (Operations with unsigned int)
Reply With Quote #4

Quote:
Originally Posted by kossolax View Post
Are you comparing this below with FormatTime or vs previous version of your extension?
This is a comparison with the previous version FormatUTime v1.0.2 (uint.zip). Standart function FormatTime is faster, but I don't think that's a problem. I would be glad if someone could suggest a better solution.

Last edited by client21; 02-02-2021 at 22:01.
client21 is offline
Reply


Thread Tools
Display Modes

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 00:37.


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