NO

Recent Posts

Pages: [1] 2 3 ... 10
1
User contributions / Re: Big integer arithmetic library
« Last post by cosh on Today at 10:21:16 PM »
Hi, fellows
I finished a big integer calculator.
You guys may download it here: https://github.com/coshcage/pbint/blob/main/Samples/05-24-24_13-09.c
The calculator supports +-*/^(power) and brackets.
I also paste this code here:
Code: [Select]
/*
 * Name:        05-24-24_13-09.c
 * Description: A big integer calculator.
 * Author:      cosh.cage#hotmail.com
 * License:     GPLv3.
 */
#include <stdio.h>
#include <string.h>
#include "pbm.h"
#define MAXLEN 100

P_BINT CatchFirstNumber();
P_BINT CalcBrackets();
P_BINT CalcAddtional();
P_BINT CalcAdvanced();
P_BINT CalcPrimary();

char * expr;

P_BINT CalcPrimary()
{
P_BINT x, y = NULL, r;
int c;
r = pbkCreateBint(0);
x = CalcAdvanced(expr);
c = expr[0];
while ('+' == c || '-' == c)
{
++expr;
y = CalcAdvanced(expr);
if ('+' == c)
{
pbkAddBint(r, x, y);
pbkMoveBint(x, r);
}
else if ('-' == c)
{
pbkSubtractBint(r, x, y);
pbkMoveBint(x, r);
}
c = expr[0];
}
if (expr[0] == ')')
++expr;
pbkDeleteBint(r);
if (NULL != y)
pbkDeleteBint(y);
return x;
}

P_BINT CalcAdvanced()
{
P_BINT x, y = NULL, r, k;
int c;
r = pbkCreateBint(0);
k = pbkCreateBint(0);
x = CalcAddtional(expr);
c = expr[0];
while ('*' == c || '/' == c)
{
++expr;
y = CalcAdvanced(expr);
if ('*' == c)
{
pbkMultiplyBint(r, x, y);
pbkMoveBint(x, r);
}
else if (c == '/')
{
pbkDivideBint(r, k, x, y);
pbkMoveBint(x, r);
}
c = expr[0];
}
pbkDeleteBint(r);
pbkDeleteBint(k);
if (NULL != y)
pbkDeleteBint(y);
return x;
}

P_BINT CalcAddtional()
{
P_BINT x, y, r;
r = pbkCreateBint(0);
x = CalcBrackets(expr);
if ('^' == expr[0])
{
_ub n;
++expr;
y = CalcAddtional(expr);
n = GETABS(pbkBintToIb(y));
pbmBintPower(r, x, n);
pbkDeleteBint(y);
pbkDeleteBint(x);
return r;
}
return x;
}

P_BINT CalcBrackets()
{
if ('(' == expr[0])
{
++expr;
return CalcPrimary(expr);
}
else
{
return CatchFirstNumber(expr);
}
}

P_BINT CatchFirstNumber()
{
size_t i;
P_BINT r;
P_BNUM t;
char szT[2] = { 0 };
char szTar[MAXLEN] = { 0 };
r = pbkCreateBint(0);
t = pbkCreateBnum(10);
for (i = 0; expr[0] != 0 && i < MAXLEN; ++expr, ++i)
{
if (expr[0] >= '0' && expr[0] <= '9')
{
szT[0] = expr[0];
strcat(szTar, szT);
}
else
{
pbkDecimalSzToBnum(t, szTar);
pbkDecimalBnumToBint(r, t);
pbkDeleteBnum(t);
return r;
}
}
pbkDecimalSzToBnum(t, szTar);
pbkDecimalBnumToBint(r, t);
pbkDeleteBnum(t);
return r;
}

int main(int argc, char ** argv)
{
char szExpr[MAXLEN] = { 0 };
while (EOF != scanf("%s", szExpr))
{
P_BINT r;
P_BNUM t;
expr = szExpr;
t = pbkCreateBnum(10);
r = CalcPrimary();
pbkBintToDecimalBnum(t, r);
printf("= ");
pbkPrintBnum(t);
pbkDeleteBnum(t);
printf("\n");
}
}
2
User contributions / Re: Big integer arithmetic library
« Last post by cosh on Today at 08:15:53 PM »
hello cosh
if you implement a multiply BigInt by 32-bit integer then your factorial would be about 3 times faster

And this is the code to do factorial:
Code: [Select]
_boolean pbmUbFactorial(P_BINT r, _ub n)
{
BINT R = { 0 }, N = { 0 };

pbkInitBint(&R, 0);
pbkInitBint(&N, 0);

SETFLAG(r, 1);
r->data[0] = 1;

SETFLAG(&N, 1);

while (n - 1)
{
if (!pbkMoveBint(&R, r))
{
pbkFreeBint(&R);
pbkFreeBint(&N);
return FALSE;
}
N.data[0] = n; // Please notice here, it is directly insert a 32-bit integer to bigint.
if (!pbkMultiplyBint(r, &N, &R))
{
pbkFreeBint(&R);
pbkFreeBint(&N);
return FALSE;
}
--n;
}

pbkFreeBint(&R);
pbkFreeBint(&N);
return TRUE;
}
3
User contributions / Re: In-memory relational database
« Last post by cosh on Today at 08:01:44 PM »
Now svimrdb is dedicated to a high-performance in-memory database.
Compare to sqlite in-memory database svimrdb runs 6 times faster than sqlite.
Here are the codes that I used to test them:
Code: [Select]
#include <stdio.h>

#include "sqlite3.h"


int main() {

    sqlite3 * db;

    char * errMsg = 0;

    int rc;

    //rc = sqlite3_open("test.db", &db);
    rc = sqlite3_open(":memory:", &db);

    if (rc) {

        fprintf(stderr, "%s\n", sqlite3_errmsg(db));

        return 1;

    }

    char * sql = "CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY)";

    rc = sqlite3_exec(db, sql, 0, 0, &errMsg);

    if (rc != SQLITE_OK) {

        fprintf(stderr, "%s\n", errMsg);

        sqlite3_free(errMsg);

        return 2;

    }

    sqlite3_exec(db, "BEGIN TRANSACTION", 0, 0, 0);



    for (int i = 1; i <= 10000; i++) {

        char insertSql[100];

        sprintf(insertSql, "INSERT INTO data (id) VALUES (%d)", i);

        rc = sqlite3_exec(db, insertSql, 0, 0, &errMsg);

        if (rc != SQLITE_OK) {

            fprintf(stderr, "%s\n", errMsg);

            sqlite3_free(errMsg);

            sqlite3_exec(db, "ROLLBACK", 0, 0, 0);

            return 3;

        }

    }



    sqlite3_exec(db, "COMMIT", 0, 0, 0);


    sqlite3_close(db);



    printf("done!\n");



    return 0;

}
Code: [Select]
#include "svimrdb.h"
#include <stdio.h>

int main()
{
int i, * p = &i;
P_TRANS ptrans;
P_TABLE ptbl;
P_ARRAY_Z parrhdr;
P_ARRAY_Z parrg;

parrg = strCreateArrayZ(1, sizeof(void *));

parrhdr = strCreateArrayZ(1, sizeof(TBLHDR));
((TBLHDR *)strLocateItemArrayZ(parrhdr, sizeof(TBLHDR), 0))->ct = CT_INTEGER;
((TBLHDR *)strLocateItemArrayZ(parrhdr, sizeof(TBLHDR), 0))->phsh = NULL;
((TBLHDR *)strLocateItemArrayZ(parrhdr, sizeof(TBLHDR), 0))->cr = CR_PRIMARY_KEY;
((TBLHDR *)strLocateItemArrayZ(parrhdr, sizeof(TBLHDR), 0))->strname = "number";

strInsertItemArrayZ(parrg, &p, sizeof(void *), 0);

ptrans = siBeginTransaction();

ptbl = siCreateTable(ptrans, "Student", parrhdr);

while (TRUE != siTrylock(ptrans, ptbl, LT_S)) // Share lock.
;

while (TRUE != siTrylock(ptrans, ptbl, LT_X)) // Write lock.
;

for (i = 0; i < 10000; ++i)
{
if (TRUE != siInsertIntoTableBase(ptrans, ptbl, NULL, parrg))
{
printf("%s, %d\n", "failed!", i);
siRollbackTransaction(NULL, ptrans);
siReleaseAllTransaction();
return 1;
}
}

siCommitTransaction(ptrans);

siDeleteTable(NULL, ptbl);

siReleaseAllTransaction();

strDeleteArrayZ(parrg);

strDeleteArrayZ(parrhdr);

return 0;
}
svimrdb:
real    0m0.018s
user    0m0.004s
sys     0m0.000s
sqlite:
done!

real    0m0.064s
user    0m0.031s
sys     0m0.000s
 8)
4
User contributions / Re: Big integer arithmetic library
« Last post by cosh on Today at 02:55:05 PM »
cosh
I think that you misunderstood me about "multiply BigInt by 32-bit integer", anyway, here's a very simple factorial function to illustrate my point

Hello, jack
It's my mistake. I misunderstood you. However, I think multiply BigInt by 32-bit integer won't accelerate this(pbint) algorithm's speed. Cuz, theoretically, pbkMultiplyBint needs two operations: 1st is add, 2nd is left shift. For this method, the complexity of multiplying by a bigint is similar to multiplying by a 32-bit int. I tested your package(Thank you for providing it.), And 2000! by using pbkMultiplyBint only needs 0.001s but converting from base 2 Bint to base 10 Bnum needs more time. Your package performs also well, that is 0.002s.

Thank you for your reply, jack.
John C. Cage.
5
User contributions / Re: Big integer arithmetic library
« Last post by jack on Yesterday at 04:35:19 PM »
cosh
I think that you misunderstood me about "multiply BigInt by 32-bit integer", anyway, here's a very simple factorial function to illustrate my point
6
User contributions / Re: Big integer arithmetic library
« Last post by cosh on Yesterday at 01:27:04 PM »
hello cosh  :)
I ran your test, if compiled with -O2 the time was about .0043, if compiled with -O3 .0058, it's not uncommon that -O3 is a bit slower than -O2
but I don't like the license at all, LGPL for a library is ok but not GPL, although I much prefer the MIT type of license

Yes, it is unusually that -O3 optimization shows a poorer speed than -O2.
I will consider altering license after your notice in the future. Thank you. Now, I think GPLv3 is better for this library.
7
User contributions / Re: Big integer arithmetic library
« Last post by cosh on Yesterday at 01:22:07 PM »
hello cosh
if you implement a multiply BigInt by 32-bit integer then your factorial would be about 3 times faster

Hello, jack
In the file pbk.h there are some macros to define block integers:
/* Predefined data type. */
typedef int                _ib;      /* Integer block. */
typedef unsigned int       _ub;      /* Unsigned integer block. */
typedef long long          _idb;     /* Integer double block. */
typedef unsigned long long _udb;     /* Unsigned integer double block. */

On a 64-bit machine, int usually be a 32bits integer.
8
User contributions / Re: Big integer arithmetic library
« Last post by cosh on Yesterday at 01:19:37 PM »
pbint.def
Code: [Select]
LIBRARY pbint.dll
EXPORTS
pbkInitBint
pbkReallocBint
pbkFreeBint
pbkCreateBint
pbkDeleteBint
pbkMoveBint
pbkCopyBint
pbkIbToBint
pbkBintToIb
pbkCompareBint
pbkAddBint
pbkSubtractBint
pbkLeftShiftBint
pbkRightShiftBint
pbkMultiplyBint
pbkDivideBint
pbkInitBnum
pbkReallocBnum
pbkFreeBnum
pbkCreateBnum
pbkDeleteBnum
pbkMoveBnum
pbkIbToBnum
pbkDecimalSzToBnum
pbkPrintBnum
pbkBintToDecimalBnum
pbkDecimalBnumToBint
pbmBintPower
pbmUbFactorial
pbmBintSquareRoot
pbmBintGreatestCommonDivisor
pbxLoadBint
pbxSaveBint

Hi, TimoVJL
Thanks for your def file, it is useful to export functions for a dll.
9
User contributions / Re: Big integer arithmetic library
« Last post by jack on May 22, 2024, 04:53:55 PM »
hello cosh
if you implement a multiply BigInt by 32-bit integer then your factorial would be about 3 times faster
10
User contributions / Re: Big integer arithmetic library
« Last post by jack on May 22, 2024, 02:44:35 PM »
hello cosh  :)
I ran your test, if compiled with -O2 the time was about .0043, if compiled with -O3 .0058, it's not uncommon that -O3 is a bit slower than -O2
but I don't like the license at all, LGPL for a library is ok but not GPL, although I much prefer the MIT type of license
Pages: [1] 2 3 ... 10