Last active
April 29, 2016 17:40
-
-
Save GilesBathgate/cd03c177dc316397492127da35152331 to your computer and use it in GitHub Desktop.
This file contains 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
extern(C) | |
{ | |
alias uint mp_limb_t; | |
struct __mpz_struct | |
{ | |
int _mp_alloc; | |
int _mp_size; | |
mp_limb_t *_mp_d; | |
} | |
struct __mpq_struct | |
{ | |
__mpz_struct _mp_num; | |
__mpz_struct _mp_den; | |
} | |
alias __mpq_struct *mpq_ptr; | |
alias const __mpq_struct *mpq_srcptr; | |
void __gmpq_init (mpq_ptr); | |
alias __gmpq_init mpq_init; | |
int __gmpq_set_str (mpq_ptr, const char *, int); | |
alias __gmpq_set_str mpq_set_str; | |
char *__gmpq_get_str (char *, int, mpq_srcptr); | |
alias __gmpq_get_str mpq_get_str; | |
void __gmpq_add (mpq_ptr, mpq_srcptr, mpq_srcptr); | |
alias __gmpq_add mpq_add; | |
} | |
import std.conv; | |
import core.stdc.stdlib; | |
class mpq | |
{ | |
this() | |
{ | |
m=cast(mpq_ptr)malloc(__mpq_struct.sizeof); | |
mpq_init(m); | |
} | |
this(in string value) | |
{ | |
this(); | |
mpq_set_str(m,value.ptr,10); | |
} | |
~this() | |
{ | |
free(m); | |
} | |
mpq opBinary(string op)(mpq rhs) | |
{ | |
static if (op == "+") { | |
auto r=new mpq; | |
mpq_add(r.m,m,rhs.m); | |
return r; | |
} | |
else static assert(0, "Operator "~op~" not implemented"); | |
} | |
override string toString() | |
{ | |
return to!string(mpq_get_str(null,10,m)); | |
} | |
private: | |
mpq_ptr m; | |
} | |
import std.stdio; | |
void main() { | |
auto a=new mpq("11/12"); | |
auto b=new mpq("13/12"); | |
auto c=a+b; | |
writeln(c.toString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment