Skip to content

Commit

Permalink
fixing multiplication, adding pi-benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
maitag committed Oct 30, 2020
1 parent 574ab5f commit 4ec4940
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ haxelib install hxp
haxelib run hxp --install-hxp-alias
```

then simple call `hpx test hl neko ...` or
`hpx help` into projectfolder to see more targetspecific options.
then simple call `hxp test hl neko ...` or
`hxp help` into projectfolder to see more targetspecific options.


## Synopsis
Expand Down Expand Up @@ -115,6 +115,7 @@ var b:BigInt = 7;
trace(a + b); // 10
// increment and decrement
// This works not with haxes operationoverloads yet if argument or result is 0 (because of 'null') !
trace(++a); // 3
trace(a++); // 4
trace( a ); // 5
Expand Down Expand Up @@ -194,6 +195,8 @@ Let me know if something's mising ~^

## Todo

- fixing increment and decrement (++,--) problem with zero value/result
or replace this by .inc() and .dec() functions
- `haxelib run` command for invoking hxp testscripts
- fixing output with leading zeros
- optional exponential notation for decimals
Expand Down
32 changes: 32 additions & 0 deletions benchmarks/CalcPi.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import haxe.Timer;

// calculate Pi with https://en.wikipedia.org/wiki/Machin-like_formula
class CalcPi {
static public function main():Void {
var digits:BigInt = 300;
var time = Timer.stamp();
var pi:BigInt = calc(digits);
haxe.Log.trace('Pi = 3.${pi.toString().substr(1)}\n\t\t\t' + Std.int((Timer.stamp() - time)*1000) + "\tms" , #if (haxe_ver >= "4.0.0") null #else {fileName:"",lineNumber:0,className:"",methodName:"",customParams:[]} #end);
}

static public function calc(digits:BigInt):BigInt {
var calcdigits:BigInt = digits + 10;
var calcpi:BigInt = 4 * ( 4 * arctanbyd(5, calcdigits) - arctanbyd(239, calcdigits) );
calcpi = calcpi / ("10000000000":BigInt);
return(calcpi);
}

static public function arctanbyd(d:Int, digits:BigInt):BigInt {
// calculate arctan(1/d) = 1/d - 1/(3*d^3) + 1/(5*d^5) - 1/(7*d^7) + ...
var arc:BigInt = (10:BigInt).pow(digits) / d;
var part:BigInt = arc;
var n:BigInt = 0;
while (part != 0) {
n = n + 1;
part = part / (-d * d);
arc = arc + part / (2 * n + 1);
}
return(arc);
}

}
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"license": "MIT",
"tags": ["math", "arbitrary", "precision", "integer", "bigint"],
"description": "pure haxe implementation for arbitrary-precision integer",
"version": "0.1.0",
"version": "0.1.1",
"classPath": "src/",
"releasenote": "coded once, runs everywhere ;)",
"releasenote": "fixing multiplication, adding pi-benchmark",
"contributors": ["maitag"],
"dependencies": {}
}
46 changes: 32 additions & 14 deletions src/BigInt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,27 @@ abstract BigInt(LittleIntChunks) from LittleIntChunks {

@:op(A++) static function opIncrementAfter(a:BigInt):BigInt {
if (a == null) {
a = BigInt.fromInt(1);
a = BigInt.fromInt(1); // TODO: Can't do "++" for 0 (null can not be changed)
return null;
}
var ret = a.copy();
if (a.isNegative) subtractLittle(a, 1, 0);
if (a.isNegative) {
if (a == -1) a = null; // TODO: Can't do "++" for 0 (null can not be changed)
else subtractLittle(a, 1, 0);
}
else addLittle(a, 1, 0);
return ret;
}

@:op(++A) static function opIncrementBefore(a:BigInt):BigInt {
if (a == null) {
a = BigInt.fromInt(1);
a = BigInt.fromInt(1); // TODO: Can't do "++" for 0 (null can not be changed)
return BigInt.fromInt(1);
}
if (a.isNegative) subtractLittle(a, 1, 0);
if (a.isNegative) {
if (a == -1) a = null; // TODO: Can't do "++" for 0 (null can not be changed)
subtractLittle(a, 1, 0);
}
else addLittle(a, 1, 0);
return a.copy();
}
Expand Down Expand Up @@ -299,22 +305,31 @@ abstract BigInt(LittleIntChunks) from LittleIntChunks {

@:op(A--) static function opDecrementAfter(a:BigInt):BigInt {
if (a == null) {
a = BigInt.fromInt(-1);
a = BigInt.fromInt(-1); // TODO: Can't do "--" for 0 (null can not be changed)
return null;
}
var ret = a.copy();
if (a.isNegative) addLittle(a, 1, 0);
else subtractLittle(a, 1, 0);
else {
if (a == 1) a = null; // TODO: Can't do "--" for 0 (null can not be changed)
else subtractLittle(a, 1, 0);
}
return ret;
}

@:op(--A) static function opDecrementBefore(a:BigInt):BigInt {
if (a == null) {
a = BigInt.fromInt(-1);
a = BigInt.fromInt(-1); // TODO: Can't do "--" for 0 (null can not be changed)
return BigInt.fromInt(-1);
}
if (a.isNegative) addLittle(a, 1, 0);
else subtractLittle(a, 1, 0);
else {
if (a == 1) {
a = null; // TODO: Can't do "--" for 0 (null can not be changed)
return null;
}
else subtractLittle(a, 1, 0);
}
return a.copy();
}

Expand Down Expand Up @@ -441,8 +456,8 @@ abstract BigInt(LittleIntChunks) from LittleIntChunks {
}
}
//if (c.length > e) b = b + c.splitHigh(e);
//if (c.length > e) b = _add(b, c.splitHigh(e));
if (c.length > e) {
if (c.length > e) b = _add(b, c.splitHigh(e));
/* if (c.length > e) {
var ch = c.splitHigh(e);
if (ch != null) {
if (b == null) b = ch;
Expand All @@ -451,6 +466,7 @@ abstract BigInt(LittleIntChunks) from LittleIntChunks {
else b = addLong(ch, b);
}
}
*/
}

if (b == null) {
Expand All @@ -465,15 +481,16 @@ abstract BigInt(LittleIntChunks) from LittleIntChunks {
}
}
//if (b.length > e) a += b.splitHigh(e);
//if (b.length > e) a = _add(a, b.splitHigh(e));
if (b.length > e) {
if (b.length > e) a = _add(a, b.splitHigh(e));
/* if (b.length > e) {
var bh = b.splitHigh(e);
if (bh != null) {
if (a == null) a = bh;
else if (a.length > bh.length) addLong(a, bh);
else a = addLong(bh.copy(), a);
}
}
*/
}

if (a != null) for (i in 0...a.length) littleIntChunks.push(a.get(i));
Expand Down Expand Up @@ -593,7 +610,7 @@ abstract BigInt(LittleIntChunks) from LittleIntChunks {
var e = b.length - 1;
var r:BigInt;
var x:LittleInt = b.get(e);
var q:BigInt = divFast(a.splitHigh(e) , x);
var q:BigInt = divFast(a.splitHigh(e), x);

do {
r = a - (q * b);
Expand All @@ -602,9 +619,10 @@ abstract BigInt(LittleIntChunks) from LittleIntChunks {
q = q - divFast(r.splitHigh(e), x);
q.shiftOneBitRight();
r.setPositive();
}
}
} while (r >= b);


if (r != null) {
r = a - (q * b );
if (r.isNegative) {
Expand Down
4 changes: 2 additions & 2 deletions src/IntUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class IntUtil

public inline static var MAX_BITSIZE:Int =
#if bigint64
32;
64;
#else
16;
32;
#end

#if macro_optimize_bitsize // --------------- macro optimization -------------------
Expand Down

0 comments on commit 4ec4940

Please sign in to comment.