tuple2<double, double> *hsv_to_rgb(double h, double s, double v) { double f, p, q, t; __ss_int i; if ((s==0.0)) { return (new tuple2<double, double>(3,v,v,v)); } i = __int((h*6.0)); f = ((h*6.0)-i); p = (v*(1.0-s)); q = (v*(1.0-(s*f))); t = (v*(1.0-(s*(1.0-f)))); i = __mods(i, (__ss_int)6); if ((i==0)) { return (new tuple2<double, double>(3,v,t,p)); } if ((i==1)) { return (new tuple2<double, double>(3,q,v,p)); } if ((i==2)) { return (new tuple2<double, double>(3,p,v,t)); } if ((i==3)) { return (new tuple2<double, double>(3,p,q,v)); } if ((i==4)) { return (new tuple2<double, double>(3,t,p,v)); } if ((i==5)) { return (new tuple2<double, double>(3,v,p,q)); } return 0; }
__ss_int atol(str *s, __ss_int base) { char c = s->unit[len(s)-1]; if(c=='l' || c=='L') s = s->__slice__(7, 0, -1, 1); return __int(s, base); }
str *DynamInt::__mul__(DynamInt *num) { /** Overloaded add function Adds an existing DynamInt to this one @param num reference to an existing DynamInt @return added DynamInt */ str *bottom, *product, *top; int __2, __3, carry, i, j, k, n, resultSize, tempProduct; if (__gt(this, num)) { top = this->data; bottom = num->data; } else { top = num->data; bottom = this->data; } tempProduct = 0; product = const_0; resultSize = (this->size+num->size); FAST_FOR(n,0,resultSize,1,2,3) product = (const_1)->__add__(product); END_FOR i = (len(bottom)-1); while ((i>=0)) { carry = 0; j = (len(top)-1); while ((j>=0)) { k = (i+j); tempProduct = __int((((__int(top->__getitem__(j))*__int(bottom->__getitem__(i)))+__int(product->__getitem__(k)))+carry)); product = __add_strs(3, product->__slice__(2, 0, (k+1), 0), __str(__mods(tempProduct, 10)), product->__slice__(1, (k+2), 0, 0)); carry = __int(__divs(tempProduct, 10)); j = (j-1); } if ((carry!=0)) { product = __add_strs(3, product->__slice__(2, 0, i, 0), __str(carry), product->__slice__(1, (i+1), 0, 0)); } i = (i-1); } return product; return 0; }
__ss_int atoi(str *s, __ss_int base) { return __int(s, base); }
#include <tlp/test/Test.hpp> #include <tlp/int/IntType.h> #include <tlp/int/algo/Inc.h> #include <tlp/int/algo/Dec.h> #include <tlp/int/algo/Add.h> #include <tlp/int/algo/Sub.h> #include <tlp/int/algo/Mul.h> #include <tlp/int/algo/Div.h> #include <tlp/int/algo/Mod.h> #include <tlp/int/algo/Sum.h> FIXTURE(TestInt) { TEST("compare int type") { ASSERT_EQ(__int(0), __int(0)); ASSERT_NE(__int(0), __int(1)); }; TEST("operator inc on int type") { ASSERT_EQ(__inc(__int(0)), __int(1)); }; TEST("operator dec on int type") { ASSERT_EQ(__dec(__int(0)), __int(-1)); }; TEST("operator add on int type") {
__ss_int str::__int__() { return __int(this); }
str *DynamInt::__sub__(DynamInt *num) { /** Overloaded Subtraction function subtracts an existing DynamInt from this one @param num reference to an existing DynamInt @return added DynamInt */ str *bottom, *sub, *sum, *top; int borrow, i, j, tempSub; /** Uses two's compliment on each element of both numbers */ if (__eq(this, num)) { return const_1; } if (__gt(this, num)) { top = this->data; bottom = num->data; } else if (__gt(num, this)) { top = num->data; bottom = this->data; } /** Temporary Variables used in addition process tempSub ----- Stores partial subtraction j ----- Stores length of bottom number i ----- Stores length of top number sub ----- Stores final sub string borrow ----- A flag that determines if it would need to borrow from next digit */ tempSub = 0; j = (len(bottom)-1); i = (len(top)-1); borrow = 0; sub = const_0; while ((i>=0)) { /** Have we run out of bottom digits ? */ if ((j>=0)) { tempSub = ((__int(top->__getitem__(i))-__int(bottom->__getitem__(j)))-borrow); if ((tempSub<0)) { borrow = 1; tempSub = (tempSub+10); } else { borrow = 0; } j = (j-1); } else { /** No more bottom digits, subtract any leftover borrow out */ tempSub = (__int(top->__getitem__(i))-borrow); if ((tempSub<0)) { borrow = 1; tempSub = (tempSub+10); } else { borrow = 0; } } /** lets concatenate the main sum outputed */ sub = (__str(tempSub))->__add__(sub); i = (i-1); } if ((borrow==1)) { sum = (const_2)->__add__(sub); } return sub; }
str *DynamInt::__add__(DynamInt *num) { /** Overloaded add function Adds an existing DynamInt to this one @param num reference to an existing DynamInt @return added DynamInt */ str *bottom, *sum, *top; int carryover, i, j, tempSum; /** Basically, this selects what should be added to what it is based on primary school addition techniques i.e to add 1234 to 12345, the code makes a configuration similar to 1 2 3 4 5 + 1 2 3 4 ------------- 1 3 5 7 9 ------------- The larger number goes on top while the smaller goes below */ if (__gt(this, num)) { top = this->data; bottom = num->data; } else { top = num->data; bottom = this->data; } /** Temporary Variables used in addition process tempSum ----- Stores partial subtraction j ----- Stores length of bottom number i ----- Stores length of top number sum ----- Stores final sum string carryover ----- A flag that determines if there is a carryover to the next digit */ tempSum = 0; j = (len(bottom)-1); i = (len(top)-1); sum = const_0; carryover = 0; while ((i>=0)) { /** Have we run out of bottom digits ? */ if ((j>=0)) { tempSum = ((__int(top->__getitem__(i))+__int(bottom->__getitem__(j)))+carryover); j = (j-1); } else { /** No more bottom digits, add any leftover carryover */ tempSum = (__int(top->__getitem__(i))+carryover); } if ((tempSum>=10)) { /** do we have a remainder to carryover ? */ tempSum = __mods(tempSum, 10); carryover = 1; } else { carryover = 0; } /** lets concatenate the main sum outputed */ sum = (__str(tempSum))->__add__(sum); i = (i-1); } if ((carryover==1)) { sum = (__str(carryover))->__add__(sum); } return sum; }