[Include] Numlib - Math library for huge numbers (larger than 32-bits)
#1

Numlib

THIS LIBRARY IS NO LONGER MAINTAINED.

The source code is available here.

What is this and why would I use it?
Numlib is a SA-MP library for dealing with numbers greater than 2 147 483 647,
which means numbers greater than 32-bit numbers. The library allows you to
perform basic mathematical operations (add and subtract at the moment) with
integers in form of strings, which is the reason why this library is able to
handle almost any numbers.

So if you need to deal with big numbers, greater than one billion, this library
might be useful for you.

Functions

add:
Код:
Add two integers
	
	Parameters:
		number1[]	An integer represented as a char array
		number2[]	An integer represented as a char array
	
	Returns:
		The sum of number1 and number2 as a char array
subtract:
Код:
Subtract two integers
	
	Parameters:
		number1[]	An integer represented as a char array
		number2[]	An integer represented as a char array
	
	Returns:
		The remainder of substraction of number1 and number2 as a char array
multiply
Код:
Multiply two numbers

	Parameters:
		number1[]	An integer represented as a char array
		number2[]	An integer represented as a char array

	Returns:
		The result of the multiplication as a char array
num_power
Код:
Raise number (char array) to the power of another number (32-bit integer)

	Parameters:
		number1[]	An integer represented as a char array
		power		A 32-bit integer
		
	Returns:
		The result of the power as a char array
divide
Код:
Divide a number (char array) by another number (32-bit integer)

	Parameters:
		divident[]	An integer represented as a char array
		divisor		A 32-bit integer
		
	Returns:
		The result of the division as a char array. The result is always an integer, and the remainder can be obtained using get_remainder function.
get_remainder
Код:
Get the remainder of a division. Works similarly to modulus (%).

	Parameters:
		divident[]	An integer represented as a char array
		divisor		A 32-bit integer
		
	Returns:
		The remainder of the division as a 32-bit integer
is_greater
Код:
Compares two char arrays (as if they were numbers) and determines whether the first one is greater than the second or not
	
	Parameters:
		number1[]	An integer represented as a char array
		number2[]	An integer represented as a char array
	
	Returns:
		boolean: true or false
is_less
Код:
Compares two char arrays (as if they were numbers) and determines whether the first one is smaller than the second or not
	
	Parameters:
		number1[]	An integer represented as a char array
		number2[]	An integer represented as a char array
	
	Returns:
		boolean: true or false
Example
pawn Код:
new integer1[20], integer2[20];

integer1 = "1000000000000000000";
integer2 = "5320010493843209492";

printf("Result: %s", add(integer1, integer2));

// Output: Result: 6320010493843209492
Reply
#2

Doesn't it performs same as these functions?
pawn Код:
floatadd(number1, number2);
floatsub(number1, number2);
floatmul(number1, number2);
Reply
#3

No, floatadd/floatsub/floatmul are not able to deal with as large numbers as this library is.

I ran this code:
pawn Код:
printf("%f", floatadd(5847309582938543028530492543895348504854390, 542384320000000000000000000000000000));
printf("%s", add("5847309582938543028530492543895348504854390", "542384320000000000000000000000000000"));
The output was:
Код:
[19:09:30] -502366336.000000
[19:09:30] 5847310125322863028530492543895348504854390
Adding two positive numbers rarely end up in negative solution, which is what happened with floatadd.
Reply
#4

Lordz: They (float(add/sub/mul)) can support only 32 bits numbers.

Seems awesome, great work.
Reply
#5

Looks awesome
Reply
#6

EDIT: (xD) hmm over 32bit , interseting ...
Reply
#7

Quote:
Originally Posted by Niko_boy
Посмотреть сообщение
hmm 32bit , interseting ...
Over 32-bits..
Reply
#8

It's a good idea, I don't really like the fact that you need to use a cell for every digit though.
Reply
#9

I know, but it's the best solution I was able to come up with. Integers on SA-MP simply cannot hold more than a 32-bit number, so there aren't really options - at least as far as I know.

But if you (or anyone) is able to find a more effective method, please share it here or fork the project.
Reply
#10

Do you need any help with multiplication/division though?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)