in which you'll see a 1 and 0 outputted. why on earth is that happening?
And also, for something like:
code:
pair<BigInteger,int> div(BigInteger &v1, int v2){
.......
cout<<ret.first.arr[0]<<endl;
return ret;
}
....
p=div(v,1e9); //although 1e9 is a double rather than an int, it is not causing the problmes
cout<<p.first.arr[0]<<endl;
and this produce totally different outputs!!!
I am completely lost as to whats going on...
Sponsor Sponsor
rizzix
Posted: Sat Mar 26, 2005 1:34 pm Post subject: (No subject)
try:
c++:
class BigInteger { protected:
int sz,start,maxsize, *arr;
Posted: Sat Mar 26, 2005 1:46 pm Post subject: (No subject)
hmmm is that syntax C friendly?
bugzpodder
Posted: Sat Mar 26, 2005 1:47 pm Post subject: (No subject)
i do have a destructor, but didnt bother to post it. my full code is 200 lines long. Okay, so a class probably works, but what the heck is wrong with struct?
wtd
Posted: Sat Mar 26, 2005 1:48 pm Post subject: (No subject)
None of this is valid C. It is, however, perfectly valid C++ code.
bugzpodder
Posted: Sat Mar 26, 2005 1:49 pm Post subject: (No subject)
hmm interesting, never seen code like that before wtd.
and the second problem is... well here is the full code (first problem was fixed): funny things are happening in toString (outputs dont match), which calls div
no clue why it is happening
code:
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
#define pb push_back //pushes an element in the back
#define pf push_front //pushes an element in the front
#define fe first //first element of a pair container
#define se second //second element of a pair container
#define len length() //length of a string
using namespace std;
typedef unsigned int uint; //32-bit unsigned int
typedef long long ll; //64-bit signed long long
typedef unsigned long long ull; //64-bit unsigned long long
int compareTo(BigInteger &v1, BigInteger &v2){
if (v1.sz!=v2.sz) return v1.sz<v2.sz?1:-1;
for (int i=v1.sz-1;i>=0;i--)
if (v1[i]!=v2[i]) return v1[i]<v2[i]?1:-1;
return 0;
}
string toString(BigInteger v){
if (v.sz==1 && v[0]==0) return "0";
divtype p; string ret;
BigInteger zero(0);
int i;
//while(compareTo(v,zero)!=0){
p=div(v,1e9);
v=p.fe;
cout<<v.start<<' '<<v[0]<<' '<<v.sz<<' '<<p.se<<endl;
for (i=0;i<9;i++){ret+=p.se%10+'0';p.se/=10;}
//}
reverse(ret.begin(),ret.end());
for (i=0;i<ret.len-1 && ret[i]=='0';i++);
return ret.substr(i);
}
Posted: Sat Mar 26, 2005 1:56 pm Post subject: (No subject)
and i still dont get what the heck is wrong with what I did (in my first post) the second constructor calls the first, and it IS getting called, so it seems perfectly fine
Sponsor Sponsor
wtd
Posted: Sat Mar 26, 2005 2:04 pm Post subject: (No subject)
Do NOT do I/O in a function like that.
bugzpodder
Posted: Sat Mar 26, 2005 2:04 pm Post subject: (No subject)
??? err?? why not? its just debugging output? my code is getting SIGFPE (division/mod by 0) and SIGSEV's... these outputs pinpointed where the error is... I was wondering why the hell my code is getting SIGFPE until i found out that the variable sz mysteriously changed its value to 0 when the second constructor is called (so i made everything into one constructor... still not knowing why I cant keep two constructors like what I did before) and then I am getting an infinite loop in toString, which it turns out that the return value from div is different than the value I am getting in toString?!?! I am completely lost as to why this is happening...
anyhow i think the problem is with structs since the code has being tested if you remove the struct and use typedef deque<uint> BigInteger;
unfortunately deque is much slower... and I dont get why structs are giving me these resuts.
wtd
Posted: Sat Mar 26, 2005 2:27 pm Post subject: (No subject)
Because I/O is not meat to go into functions designed purely to output a new value. It's just bad. First make your changes, then check to see if things are as you expected. If you feel the need to do this inside a function, it's a good sign that function is doing too much.
Posted: Sat Mar 26, 2005 2:37 pm Post subject: (No subject)
bugzpodder wrote:
i do have a destructor, but didnt bother to post it. my full code is 200 lines long. Okay, so a class probably works, but what the heck is wrong with struct?
its because u have to pay close attention to the Consturctor() : initialization {} statements
although it appears to be two ways to intialize members of a class/struct in c++. there really is only one recognized official way. i suggest u create get/set accessor/mutators or simply define variables protected, for this to work out well.
also note Constructor(some_args) : Consturct() {} is the only working way to go about with calling previously defined constructs including the parent class' constuctor in c++. that is you cannot call the constuctor within the {} block of another constrcutor. although this should work, its does not always work well with all compilers.
although in D, Java, and a hell a lot of other languages, you dont have such annoying complications.
bugzpodder
Posted: Sat Mar 26, 2005 2:39 pm Post subject: (No subject)
wtd, One of my original plans is to push performance, since there is just too much overhead in vectors.
rizzix, ic, well it seemed to work :S heh
rizzix
Posted: Sat Mar 26, 2005 2:46 pm Post subject: (No subject)
the fist most common way in most languages:
code:
class BigInteger {
protected:
int sz,start,maxsize, *arr;
Note that if you want to initialzise members of class using a previously defined consructor always use the second method:
code:
BigInteger(int val) : BigInteger() {arr[0]=val;}
you may if you wish, initialize member of the class directly using the second way, although the first way will work just as well.
wtd
Posted: Sat Mar 26, 2005 2:48 pm Post subject: (No subject)
bugzpodder wrote:
wtd, One of my original plans is to push performance, since there is just too much overhead in vectors.
I can guarantee std::vector will be faster than what you can cook up. That's not to insult your skills. but a lot of people have invested a lot of time in making the STL fast.
Also, your use of macros, and lack of spacing in your code will not make it any faster.