Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Changelog for dutils v0.2.0 as of August 17, 2022
# Changelog for dutils v0.2.0 as of August 18, 2022

## NEW

Expand All @@ -9,7 +9,7 @@
- dutils.math.def contains the definitions for the template mathematical type: Mtype, and for the wrappers for the function and operator lists.

- dutils.math.number contains the definition of the Number type, which is currently incomplete and undocumented.
The Number type currently supports four operations: +, -, *, and /, and the function toDstring.
The Number type currently supports four operations: +, -, *, and /, and the functions toDstring and fromDstring.

- dutils.math.core currently only supports registering and validating a function.

Expand Down
110 changes: 107 additions & 3 deletions source/dutils/math/number.d
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class Number : Mtype!NumberContainer
*/
override void fromDstring(dstring from) pure @safe
{
dstring val;
dstring ival;
dchar[] val;
dchar[] ival;
size_t i;
do
{
Expand All @@ -131,6 +131,72 @@ class Number : Mtype!NumberContainer
++i;
}
while(i < from.length);

i = 0;
long dec = -1;
long z = 0;
size_t firstz = 0;
size_t[] zi;
import std.conv : to;
bool neg = false;
if(val[0] == d('-'))
{
neg = true;
val[0 .. $-1] = val[1 .. $].dup;
--val.length;
}
pragma(inline, true) void inFunc(in dchar[] val, ref BigInt rval)
{
rval = BigInt(0);
do
{
if(val[i] == d('.'))
{
dec = i;
++i;
continue;
}

else if(val[i] == d('0'))
{
if(firstz == 0)
firstz = i;
zi ~= i;
++i;
continue;
}
BigInt temp = BigInt(10);
temp ^^= (val.length-1-i);
temp *= BigInt(to!ubyte([val[i]]));
rval = temp;
++i;
}
while(i < val.length);
i = 0;

if(neg)
rval *= -1;
neg = false;
}

inFunc(val, this.contained.val);
if(dec == -1)
this.contained.pow10 = zi.length;
else if(firstz == 0)
this.contained.pow10 = -zi.length;
else
this.contained.pow10 = -(val.length-1-dec);
firstz = 0;
zi = [];
z = 0;
i = 0;
dec = 0;
--ival.length; //The last character of ival is d('i'), which is a non integer.
if(ival[0] == d('-'))
neg = true;
ival[0 .. $-1] = ival[1 .. $].dup;
--ival.length;
inFunc(ival, this.contained.ival);
}

/*************************************************
Expand Down Expand Up @@ -182,6 +248,35 @@ class Number : Mtype!NumberContainer
temp.applyOp!W(op, rhs);
return new Number(temp.val);
}

bool opEquals(in Number rhs) pure const @safe nothrow @nogc
{
return (this.contained == rhs.contained);
}
}

///
pure @safe unittest {
BigInt a = 1;
immutable BigInt b = -1;
immutable long c = 0;
Number e = new Number(NumberContainer(a,b,c));
a = 2;
Number f = new Number(NumberContainer(a,b,c));
e.applyOp("/", f);
assert(e.val == NumberContainer(BigInt(6), BigInt(-2), -1L));
assert(e.toDstring == ".6-.2i"d);
f = new Number(NumberContainer(BigInt(6), BigInt(0), 1L));
assert(f.toDstring == "60+00i"d);
f = new Number(NumberContainer(BigInt(6), BigInt(0), -2L));
assert(f.toDstring == ".06+.00i"d, cast(char[])f.toDstring.dup);
auto g = f.toDstring;
f.fromDstring(g);
assert(g == f.toDstring);
f = new Number(NumberContainer(BigInt(3), BigInt(6), -23L));
g = f.toDstring;
f.fromDstring(g);
assert(g == f.toDstring);
}

///
Expand Down Expand Up @@ -331,7 +426,7 @@ struct NumberContainer
}
else static if(op == "^^")
{
//Oy Vey: I ain't implementing this until function execution and exponential functions exist..
//Oy Vey: I ain't implementing this until function execution and exponential functions exist.
}
else static if(op == "*")
{
Expand Down Expand Up @@ -376,6 +471,15 @@ struct NumberContainer
return ret;
}

/*************************************
* Determine if two Numbers are equal.
*
* Params:
* rhs =
* The Number to compare with.
* Returns:
* Whether this is equal to rhs.
*/
bool opEquals(in NumberContainer rhs) pure @safe nothrow const @nogc
{
return ((this.val == rhs.val) && (this.ival == rhs.ival))
Expand Down