Created
May 26, 2017 14:10
-
-
Save zaydek-old/2738ab5dc89c0aff6fb93c1b2c713a37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Ratio | |
{ | |
import std.experimental.checkedint; | |
Checked!long[2] data; | |
this(long x, long y) | |
{ | |
data[0] = x; | |
data[1] = y; | |
} | |
} | |
void main() | |
{ | |
Ratio ratio = Ratio(3, 2); | |
} | |
/* | |
struct Number | |
{ | |
import std.experimental.checkedint; | |
import std.exception: enforce; | |
import std.regex: StaticRegex, ctRegex, matchFirst; | |
import std.array: array; | |
import std.conv: to; | |
import std.math: abs; | |
import std.format: format; | |
/+ | |
initializers | |
+/ | |
private Checked!long data; | |
this(long ones, long hths = 0, long tths = 0) | |
in | |
{ | |
string enforces(string component) { | |
return `enforce(`~component~` >= 0 && `~component~` < 100, __FUNCTION__);`; | |
} | |
mixin(enforces(`hths`)); | |
mixin(enforces(`tths`)); | |
} | |
body | |
{ | |
string initializes(string op) { | |
return `data = ones.checked * 10_000 `~op~` hths * 100 `~op~` tths;`; | |
} | |
if (ones < 0) mixin(initializes(`-`)); | |
else mixin(initializes(`+`)); | |
} | |
/+ | |
constructors | |
+/ | |
static StaticRegex!char | |
componentsRegex = ctRegex!`^([-+]?\d+)(?:\.(\d{2})(?:(\d{2})\d*)?)?$`; | |
static Number fromString(string str) | |
in { | |
enforce(str.matchFirst(componentsRegex), __FUNCTION__); | |
} | |
body | |
{ | |
string componentToLong(int index) | |
{ | |
string i = index.to!string; | |
return `components[`~i~`].length ? components[`~i~`].to!long : 0`; | |
} | |
string[3] components = str.matchFirst(componentsRegex).array[1..$]; | |
return Number( | |
mixin(componentToLong(0)), | |
mixin(componentToLong(1)), | |
mixin(componentToLong(2)), | |
); | |
} | |
/+ | |
properties | |
+/ | |
long integer() { return data.get / 10_000; } | |
long decimal() { return data.get.abs % 10_000; } | |
long round(long x) | |
{ | |
if (x < 100) | |
return x; | |
else | |
return round((x % 10 < 5 ? x : x + 10) / 10); | |
} | |
/+ | |
statements | |
+/ | |
Number opBinary(string op)(Number that) | |
{ | |
static if (op == `-` || op == `+`) | |
mixin(`this.data `~op~`= that.data.get; return this;`); | |
} | |
Number opBinary(string op)(Number that) | |
{ | |
static if (op == `/` || op == `*`) | |
mixin(`this.data `~op~`= that; return this;`); | |
} | |
Number opBinary(string op)(long that) | |
{ | |
static if (op == `/` || op == `*`) | |
mixin(`this.data `~op~`= that; return this;`); | |
} | |
/+ | |
formatters | |
+/ | |
string toString() { return `%d.%04d`.format(integer, decimal ); } | |
string toPrettyString() { return `%d.%02d`.format(integer, round(decimal)); } | |
} | |
void main() | |
{ | |
import std.stdio: writeln; | |
writeln((Number(-53,24,32) * 2.5).toPrettyString); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment