It’s been a while since I’ve posted because I really wanted to have something to show for myself. I finally have a completely working += function (for non-programmer types, a+=b is equivalent to a = a+b). It took longer than I expected because I kept rewriting it – I started from scratch 3 times. I kept seeing better ways to structure it to get better performance, better readability, and be able to handle all cases. In the end, it’s not perfect, I still have some optimizing to do, but that comes last. The important thing is that it’s finished and does exactly what it’s supposed to.

Also, I compiled this not only with wxDev-C++, which is the shmancy graphical IDE that I’m using for this project, but also with gnu g++ running on Cygwin. If you’ve never heard of Cygwin, you’re missing out – it’s a complete bash shell that will run in windows. I’ve successfully compiled my program in linux! (Technically unix, but it doesn’t matter in this case). I did have to write an itoa() function, which is built in on my other compiler but undefined with g++, but it was worth it.

I’m tacking the code onto the end of my post so you can look at it if you care, but I think the output is what’s important right now. To test it, I added the first 1000 digits of pi to the first 1000 digits of e. What better to test a program? Here’s the output (the beginning bits are the compilation, if you’re not familiar):


Evan@EvanLaptop ~
$ cd ipfloat
Evan@EvanLaptop ~/ipfloat
$ g++ -o iptest -O ipfloat.cpp main.cpp
Evan@EvanLaptop ~/ipfloat
$ ./iptest
ipfloat addition test program
Enter the first floating point decimal:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208 99862803482534211706798214808651328230664709384460955058223172535940812848111745 02841027019385211055596446229489549303819644288109756659334461284756482337867831 65271201909145648566923460348610454326648213393607260249141273724587006606315588 17488152092096282925409171536436789259036001133053054882046652138414695194151160 94330572703657595919530921861173819326117931051185480744623799627495673518857527 24891227938183011949129833673362440656643086021394946395224737190702179860943702 77053921717629317675238467481846766940513200056812714526356082778577134275778960 91736371787214684409012249534301465495853710507922796892589235420199561121290219 60864034418159813629774771309960518707211349999998372978049951059731732816096318 59502445945534690830264252230825334468503526193118817101000313783875288658753320 83814206171776691473035982534904287554687311595628638823537875937519577818577805 321712268066130019278766111959092164201989
Enter the second floating point decimal: 2.718281828459045235360287471352662497757247093699959574966967627724076630353547 59457138217852516642742746639193200305992181741359662904357290033429526059563073 81323286279434907632338298807531952510190115738341879307021540891499348841675092 44761460668082264800168477411853742345442437107539077744992069551702761838606261 33138458300075204493382656029760673711320070932870912744374704723069697720931014 16928368190255151086574637721112523897844250569536967707854499699679468644549059 87931636889230098793127736178215424999229576351482208269895193668033182528869398 49646510582093923982948879332036250944311730123819706841614039701983767932068328 23764648042953118023287825098194558153017567173613320698112509961818815930416903 51598888519345807273866738589422879228499892086805825749279610484198444363463244 96848756023362482704197862320900216099023530436994184914631409343173814364054625 31520961836908887070167683964243781405927145635490613031072085103837505101157477 041718986106873969655212671546889570350354
The resulting number is: 5.859874482048838473822930854632165381954416493075065395941912220031893036639756 59319941700386728349540961447844528536656891125820617962580462569370338907674818 84164313298820118687934745037021501814009760026451635966356002176255831179542924 10032662577227913367091937760464196672090650501146337994133343276289768444921849 50626610392171487418791827566197462970356072065923967626421356861484392915082175 11258940893912747006105559582286343223962181620722448452478299327175142163406587 12822864827413110742257569851577865655872662372877154665119930858735362389813101 26700432299723241658187346813883017884824930180632421367970122480560902207847289 15501019830167802432300074632496023648871277681536117590701745382018377051707123 12462922937505620903641509899383397935711242086804198727329561543930177179559563 56351201968897173534462114551725550567527056630113002015631723127049103022807946 15335168008685578543203666499148068960614457231119251854609961041357082919735282 363431254173003988933978783505981734552343

Nifty, eh? I’m proud. I’ll be working on multiplication next. I think I’ll start with a schoolboy approach, O(N^2) and then try my hand at Fast Fouriers to try and get O(Nlog(N)). Actually, I’ve read that optimized approaches can get O(Nlog(log(N))), but of course that doesn’t necessarily mean a performance improvement.

In other news, I’m working out every morning now. I’ve only been to the gym three times and I’m already seeing some good strength improvements. I’ll be in amazing shape by the end of summer (and I have an exercise buddy to make sure I’m up at 6 every morning!)

Here’s the code for interested parties. I’m sorry it’s not formatted well, sooner or later I’ll get some css to work here to try and make it more readable.


ipfloat &ipfloat::operator +=(const ipfloat &rhs){
int c = 0, carry = 0, i;
char* thisMan = new char[strlen(getMan())+1];
char* rhsMan = new char[strlen(rhs.getMan()) + 1];
strcpy(thisMan, getMan());
strcpy(rhsMan, rhs.getMan());
//following code "aligns" decimals for addition.
int thisBefore, thisAfter, rhsBefore, rhsAfter;
thisBefore = (exp>=0) ? exp + 1 : 1;
thisAfter = strlen(man) - exp - 1;
rhsBefore = (rhs.getExp()>=0) ? rhs.getExp() + 1 : 1;
rhsAfter = strlen(rhs.getMan()) - rhs.getExp() - 1;
if(getExp()<0)
{
free(thisMan);
thisMan = (char*)malloc(sizeof(char)*(strlen(getMan()) - getExp() +1));
for(c = 0; c<-getExp(); c++)
thisMan[c] = '0';
thisMan[c] = '';
strcat(thisMan, getMan());
}
if(rhs.getExp()<0)
{
free(rhsMan);
rhsMan = (char*)malloc(sizeof(char)*(strlen(rhs.getMan()) - rhs.getExp()+1));
for(c = 0; c<-rhs.getExp(); c++)
rhsMan[c] = '0';
rhsMan[c] = '';
strcat(rhsMan, rhs.getMan());
}
if(thisBefore>rhsBefore)
{
char* tmp = new char[strlen(rhsMan) + 1];
strcpy(tmp, rhsMan);
free(rhsMan);
rhsMan = (char*)malloc(sizeof(char)*(strlen(rhs.getMan()) + (thisBefore - rhsBefore) + 1));
for(c = 0; c<(thisBefore-rhsBefore); c++)
rhsMan[c] = '0';
rhsMan[c] = '';
strcat(rhsMan, tmp);
}
else if(rhsBefore>thisBefore)
{
char* tmp = new char[strlen(thisMan) + 1];
strcpy(tmp, thisMan);
free(thisMan);
thisMan = (char*)malloc(sizeof(char)*(strlen(getMan()) + (rhsBefore - thisBefore) + 1));
for(c = 0; c<(rhsBefore-thisBefore); c++)
thisMan[c] = '0';
thisMan[c] = '';
strcat(thisMan, tmp);
}
if(thisAfter>rhsAfter)
{
char* tmp = new char[strlen(rhsMan) + 1];
strcpy(tmp, rhsMan);
free(rhsMan);
rhsMan = (char*)malloc(sizeof(char)*(strlen(tmp) + (thisAfter-rhsAfter) + 1));
strcpy(rhsMan, tmp);
for(c=strlen(tmp); c<strlen(tmp) + (thisAfter-rhsAfter); c++)
rhsMan[c] = '0';
rhsMan[c] = '';
}
else if(rhsAfter>thisAfter)
{
char* tmp = new char[strlen(thisMan) + 1];
strcpy(tmp, thisMan);
free(thisMan);
thisMan = (char*)malloc(sizeof(char)*(strlen(tmp) + (rhsAfter-thisAfter) + 1));
strcpy(thisMan, tmp);
for(c=strlen(tmp); c<strlen(tmp) + (rhsAfter-thisAfter); c++)
thisMan[c] = '0';
thisMan[c] = '';
}
//actually add
free(man);
man = (char*)malloc(sizeof(char)*(strlen(thisMan) + 1));
for(i = strlen(thisMan); i>=0; i--)
{
man[i] = (char)((((int)(thisMan[i] + rhsMan[i] - 2*'0')) + carry)%10) + '0';
carry = (thisMan[i] + rhsMan[i] - 2*'0' + carry >9)?1:0;
}
man[strlen(thisMan)] = '';
exp = (exp>rhs.getExp())? exp : rhs.getExp();
if(carry==1)
{
char* tmp = new char[strlen(man) + 1];
strcpy(tmp, man);
man = (char*)malloc(sizeof(char)*(strlen(tmp)+2));
man[0] = '1';
man[1] = '';
strcat(man, tmp);
exp++;
}
return *this;
}

Ich bin sehr begeistert diesen Abend!
I am very excited this evening! (Google: I am very enthusiastic about this evening!)

4 Comments

  1. To the untrained eye, I’d say you have just written a program that accomplishes something that has been written into programming languages for years.

    And I am guessing I have an untrained eye.

    But, I’m sure “+= mathematics” != “ipfloat += mathematics”.

    Also, I’m hoping the plus and equal sign do not need escape sequences.

    return me==nerd;
    OUTPUT:true

  2. To the untrained eye, yes it seems like a standard feature.

    But write a quickie c++ program:
    double a,b;
    cin>>a;
    cin>>b;
    a+=b;
    cout<<a;

    And try the input I used, 1000 digits of pi and 1000 digits of e. It won’t work; doubles can usually only hold a precision of 32 bits, which comes to about e16 (which is x10^16). So you could only add the first 16 digits of pi to the first 16 digits of e (this will vary some from system to system, you might get more/fewer digits).

    Anyway, my ipfloat class allows arbitrary precision, which means it will just expand the data type to hold more digits as necessary. I could add a trillion digits of pi to a trillion digits of e, and theoretically with enough ram and processing power, I would get the result with a trillion digits of accuracy.

    :P No escaping for me…this is an operator overload. http://en.wikipedia.org/wiki/C%2B%2B#Operators_and_operator_overloading

  3. And this is exactly why I’m not a programmer.

    Or, just not a good one anyway.

    But, rationally, shouldn’t there be some way to store an infinitely large number? I find it hard to believe that something so crucial isn’t a part of modern programming languages.

  4. Heehee. Well, rationally, it’s impossible to store an infinitely large number…

    But I know what you mean. Java actually does have BigFloat and BigInt classes, I believe, but at the speed costs of an interpreted language. So although programming my project in Java would be easier both mathematically and graphically (Java graphics are easy breezy), I’m not willing to compromise that much on the issue of speed. C++ all the way.


Post a Comment

*
*