Raised This Month: $32 Target: $400
 8% 

[EXTENSION] BigInteger compute with huge Integers


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
dordnung
Veteran Member
Join Date: Apr 2010
Old 01-22-2014 , 13:03   [EXTENSION] BigInteger compute with huge Integers
Reply With Quote #1

Because there is not much to say about i'll make it short:

This extensions allows you to compute with Big Integers. A BigInt is represented by a Handle.

The most important functions:

PHP Code:
/* Creates a new BigInt from an integer */
native Handle:BigInt_Create(integer);

/* Creates a new BigInt from a string */
native Handle:BigInt_CreateFromString(const String:str[], base 10);




/* Converters to primitive integer types */
native BigInt_ToInt(Handle:bigint);

/* Converters to primitive string types */
native BigInt_ToString(Handle:bigintString:str[], maxlengthbase 10);




/* Get the sign of a BigInt */
native BigInt_Sign:BigInt_GetSign(Handle:bigint);




/* Compares two BigInts. */
native BigInt_CmpRes:BigInt_CompareTo(Handle:bigintHandle:bigint2);


/* Compares a BigInt with an integer. */
stock BigInt_CmpRes:BigInt_CompareToInt(Handle:bigintinteger)


/* Compares a BigInt with a string. */
stock BigInt_CmpRes:BigInt_CompareToString(Handle:bigint, const String:str[])




/* Negates a BigInt. */
native Handle:BigInt_Negate(Handle:bigintBigInt_Method:method BigInt_RETURN_NEW);


/* Computes a BigInt from two BigInts */
stock Handle:BigInt_Compute(Handle:bigintHandle:bigint2BigInt_Comp:compBigInt_Method:method BigInt_RETURN_NEW)


/* Computes a BigInt from a BigInt and an Integer. */
stock Handle:BigInt_ComputeInt(Handle:bigintintegerBigInt_Comp:compBigInt_Method:method BigInt_RETURN_NEW)


/* Computes a BigInt from a BigInt and a String. */
stock Handle:BigInt_ComputeString(Handle:bigint, const String:str[], BigInt_Comp:compBigInt_Method:method BigInt_RETURN_NEW)


/* Computes a BigInt from an integer and a string */
stock Handle:BigInt_ComputeIntStr(int, const String:str[], BigInt_Comp:comp)


/* Computes a BigInt from two Integers. */
stock Handle:BigInt_ComputeInts(integerinteger2BigInt_Comp:comp)


/* Computes a BigInt from two strings. */
stock Handle:BigInt_ComputeStrs(const String:str[], const String:str2[], BigInt_Comp:comp)




/* Computes a Bit operation from two BigInts */
stock Handle:BigInt_ComputeBit(Handle:bigintHandle:bigint2BigInt_CompBit:compBit)


/* Computes a Bit operation from a BigInt and an Integer */
stock Handle:BigInt_ComputeBitInt(Handle:bigintintegerBigInt_CompBit:compBit)


/* Computes a Bit operation from a BigInt and an String. */
stock Handle:BigInt_ComputeBitString(Handle:bigint, const String:str[], BigInt_CompBit:compBit)



/* Shift a given BigInt to the left. */
native Handle:BigInt_BitShiftLeft(Handle:bitsshift);

/* Shift a given BigInt to the right. */
native Handle:BigInt_BitShiftRight(Handle:bitsshift);




/* Returns the greatest common divisor of a and b. */
native Handle:BigInt_GCD(Handle:aHandle:b);


/* Extended Euclidean algorithm. */
native BigInt_Euclidean(Handle:mHandle:n, &Handle:g, &Handle:r, &Handle:s);


/* Returns the multiplicative inverse of x modulo n. */
native Handle:BigInt_ModInv(Handle:xHandle:n);


/* Returns (base ^ exponent) % modulus. */
native Handle:BigInt_ModExp(Handle:baseHandle:exponentHandle:modulus); 

Example:


Add two Strings

PHP Code:
new String:integer[512];

new 
Handle:bigint BigInt_ComputeStrs("5451215498798""2222145678974"BigInt_ADD);

BigInt_ToString(bigintintegersizeof(integer));

PrintToServer("5451215498798 + 2222145678974 = %s"integer); 

Calculate Steamid64 from Steamid

PHP Code:
convertSteamid(const String:steam[])
{
    new 
String:steamid[64];
    
decl String:parts[3][16];

    
ExplodeString(steam":"partssizeof(parts), sizeof(parts[]))

    new 
Handle:bigint BigInt_ComputeStrs("76561197960265728"parts[1], BigInt_ADD);
    new 
Handle:bigint2 BigInt_ComputeIntStr(2parts[2], BigInt_MULTIPLY);

    
BigInt_Compute(bigintbigint2BigInt_ADDBigInt_OVERRIDE);

    
BigInt_ToString(bigintsteamidsizeof(steamid));

    
PrintToServer("Community ID of Steamid %s is %s"steamsteamid);

    
CloseHandle(bigint);
    
CloseHandle(bigint2);


For more examples see sample.sp!


Have fun with it!

Sourcecode on Github
Attached Files
File Type: zip binarys.zip (104.1 KB, 249 views)
File Type: inc bigint.inc (18.5 KB, 226 views)
File Type: sp Get Plugin or Get Source (sample.sp - 353 views - 4.0 KB)
__________________

Last edited by dordnung; 10-29-2017 at 19:37.
dordnung is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 01-22-2014 , 13:20   Re: [EXTENSION] BigInteger Sourcemod
Reply With Quote #2

I was actually going to inquire about this today. Without this extension, what is the largest integer that Sourcemod can work with? I was interested in, coincidentally, changing my steam64 output to an int, rather than a string.
__________________

Last edited by ddhoward; 01-22-2014 at 13:21.
ddhoward is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-22-2014 , 14:00   Re: [EXTENSION] BigInteger Sourcemod
Reply With Quote #3

Quote:
Originally Posted by ddhoward View Post
I was actually going to inquire about this today. Without this extension, what is the largest integer that Sourcemod can work with? I was interested in, coincidentally, changing my steam64 output to an int, rather than a string.
2^31 - 1 or 2147483647 (and the smallest is -2147483648).
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 01-22-2014 at 14:00.
Powerlord is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 01-22-2014 , 19:48   Re: [EXTENSION] BigInteger compute with huge Integers
Reply With Quote #4

Why not return the bits in an array of cells rather than a string? Or would that not be useful?
Then you could just shift them into a long long or something?
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.

Last edited by friagram; 01-22-2014 at 19:51.
friagram is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 01-22-2014 , 22:51   Re: [EXTENSION] BigInteger compute with huge Integers
Reply With Quote #5

Quote:
Originally Posted by friagram View Post
Why not return the bits in an array of cells rather than a string?
What's the difference?
__________________

Last edited by Impact123; 01-22-2014 at 22:58.
Impact123 is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 01-22-2014 , 23:35   Re: [EXTENSION] BigInteger compute with huge Integers
Reply With Quote #6

Quote:
Originally Posted by Impact123 View Post
What's the difference?
You can do math on the returned values instead of chopping by arbitrary place N everytime.
KyleS is offline
V1SoR
Member
Join Date: Jan 2011
Old 01-23-2014 , 07:59   Re: [EXTENSION] BigInteger compute with huge Integers
Reply With Quote #7

Steam Community ID calculator(within int32)
PHP Code:
SteamIDToCommunityID(String:CommunityID[], size, const String:SteamID[])
{
    
decl String:buffer[3][32];
    
ExplodeString(SteamID":"buffer332);
    new 
accountID StringToInt(buffer[2]) * StringToInt(buffer[1]);

    
IntToString((accountID 60265728), CommunityIDsize);

    if (
accountID >= 39734272)
    {
        
strcopy(CommunityIDsizeCommunityID[1]);
        
Format(CommunityIDsize"765611980%s"CommunityID);
    }
    else
    {
        
Format(CommunityIDsize"765611979%s"CommunityID);
    }


Last edited by V1SoR; 01-23-2014 at 08:01.
V1SoR is offline
dordnung
Veteran Member
Join Date: Apr 2010
Old 01-23-2014 , 08:16   Re: [EXTENSION] BigInteger compute with huge Integers
Reply With Quote #8

I know that you can calculate it with 32bit. That was just an example...
__________________

Last edited by dordnung; 02-28-2017 at 23:20.
dordnung is offline
Electr000999
Senior Member
Join Date: Aug 2011
Old 02-24-2017 , 02:29   Re: [EXTENSION] BigInteger compute with huge Integers
Reply With Quote #9

Code:
native Handle:BigInt_CreateFromString(const String:str[], base = 10);
this native crash if i try input any string variable instead constants..

Code:
char sString[65];
StrCat(sString, sizeof(sString), "6767676798909809");
Handle BigInt_Test = BigInt_CreateFromString(sString);
Code:
Program terminated with signal SIGABRT, Aborted.
#0  0xf775cbe9 in __kernel_vsyscall ()
[Current thread is 1 (Thread 0xf7439700 (LWP 108068))]
#0  0xf775cbe9 in __kernel_vsyscall ()
#1  0xf747cea9 in raise () from /lib/i386-linux-gnu/libc.so.6
#2  0xf747e407 in abort () from /lib/i386-linux-gnu/libc.so.6
#3  0xf76ad662 in __gnu_cxx::__verbose_terminate_handler () at /valve/build/targets/src/gcc-4.6.1/libstdc++-v3/libsupc++/vterminate.cc:95
#4  0xf76abc89 in __cxxabiv1::__terminate (handler=0xf76ad585 <__gnu_cxx::__verbose_terminate_handler()>) at /valve/build/targets/src/gcc-4.6.1/libstdc++-v3/libsupc++/eh_terminate.cc:40
#5  0xf76abcb8 in std::terminate () at /valve/build/targets/src/gcc-4.6.1/libstdc++-v3/libsupc++/eh_terminate.cc:50
#6  0xf76abdf1 in __cxxabiv1::__cxa_throw (obj=0x11b913a0, tinfo=0xf76ee708 <typeinfo for char const*>, dest=0x0) at /valve/build/targets/src/gcc-4.6.1/libstdc++-v3/libsupc++/eh_throw.cc:83
#7  0xe6faad22 in BigUnsignedInABase::BigUnsignedInABase(std::string const&, unsigned short) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/extensions/bigint.ext.so
#8  0xe6fa8464 in stringToBigUnsigned(std::string const&) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/extensions/bigint.ext.so
#9  0xe6fa85d9 in stringToBigInteger(std::string const&) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/extensions/bigint.ext.so
#10 0xe6fae7a6 in BigInt_CreateFromString(SourcePawn::IPluginContext*, int const*) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/extensions/bigint.ext.so
#11 0xe8cc5331 in BoundNativeCallback(sp_context_s*, int (*)(SourcePawn::IPluginContext*, int const*), int*) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/bin/sourcepawn.jit.x86.so
#12 0x00000020 in ?? ()
#13 0x0ab3a578 in ?? ()
#14 0xe8cd3911 in JITX86::InvokeFunction(BaseRuntime*, JitFunction*, int*) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/bin/sourcepawn.jit.x86.so
#15 0x1186b340 in ?? ()
but why we cannot work with string variables? this a weirld..

Last edited by Electr000999; 02-24-2017 at 02:33.
Electr000999 is offline
Send a message via Skype™ to Electr000999
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 02-24-2017 , 08:05   Re: [EXTENSION] BigInteger compute with huge Integers
Reply With Quote #10

Quote:
Originally Posted by Electr000999 View Post
Code:
native Handle:BigInt_CreateFromString(const String:str[], base = 10);
this native crash if i try input any string variable instead constants..

Code:
char sString[65];
StrCat(sString, sizeof(sString), "6767676798909809");
Handle BigInt_Test = BigInt_CreateFromString(sString);
Code:
Program terminated with signal SIGABRT, Aborted.
#0  0xf775cbe9 in __kernel_vsyscall ()
[Current thread is 1 (Thread 0xf7439700 (LWP 108068))]
#0  0xf775cbe9 in __kernel_vsyscall ()
#1  0xf747cea9 in raise () from /lib/i386-linux-gnu/libc.so.6
#2  0xf747e407 in abort () from /lib/i386-linux-gnu/libc.so.6
#3  0xf76ad662 in __gnu_cxx::__verbose_terminate_handler () at /valve/build/targets/src/gcc-4.6.1/libstdc++-v3/libsupc++/vterminate.cc:95
#4  0xf76abc89 in __cxxabiv1::__terminate (handler=0xf76ad585 <__gnu_cxx::__verbose_terminate_handler()>) at /valve/build/targets/src/gcc-4.6.1/libstdc++-v3/libsupc++/eh_terminate.cc:40
#5  0xf76abcb8 in std::terminate () at /valve/build/targets/src/gcc-4.6.1/libstdc++-v3/libsupc++/eh_terminate.cc:50
#6  0xf76abdf1 in __cxxabiv1::__cxa_throw (obj=0x11b913a0, tinfo=0xf76ee708 <typeinfo for char const*>, dest=0x0) at /valve/build/targets/src/gcc-4.6.1/libstdc++-v3/libsupc++/eh_throw.cc:83
#7  0xe6faad22 in BigUnsignedInABase::BigUnsignedInABase(std::string const&, unsigned short) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/extensions/bigint.ext.so
#8  0xe6fa8464 in stringToBigUnsigned(std::string const&) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/extensions/bigint.ext.so
#9  0xe6fa85d9 in stringToBigInteger(std::string const&) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/extensions/bigint.ext.so
#10 0xe6fae7a6 in BigInt_CreateFromString(SourcePawn::IPluginContext*, int const*) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/extensions/bigint.ext.so
#11 0xe8cc5331 in BoundNativeCallback(sp_context_s*, int (*)(SourcePawn::IPluginContext*, int const*), int*) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/bin/sourcepawn.jit.x86.so
#12 0x00000020 in ?? ()
#13 0x0ab3a578 in ?? ()
#14 0xe8cd3911 in JITX86::InvokeFunction(BaseRuntime*, JitFunction*, int*) () from /home/l4d2_marsus_versus/serverfiles/left4dead2/addons/sourcemod/bin/sourcepawn.jit.x86.so
#15 0x1186b340 in ?? ()
but why we cannot work with string variables? this a weirld..
My guess, the third-party code being used for BigInteger threw a C++ exception in BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base)
__________________
WildCard65 is offline
Reply



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 02:36.


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