Page 2 of 2

Posted: Mon Jan 07, 2008 11:50 am
by Candy
Zacariaz wrote:What i want is this:
mydatatype hugenumber = 123456789887764311234567876543... (and so on)

Code: Select all

class mydatatype {
    mydatatype(int c, ...) {
        va_list arg_list;
        va_start (arg_list, c);
        while (c--)
            *this = (*this * 1000) + va_arg (arg_list, uint16_t));
    }
}

mydatatype huge_num(10,    321,234,567,898,765,432,123,456,789,876);
cout << huge_num;
Never stop to be inventive ;). You might need to add a few operators though...

Posted: Mon Jan 07, 2008 11:55 am
by lukem95
you could write a program that used strings to split the number up, outputting or dumping to file the c value, or using decimal places or w/e then have your program read from the file?

Posted: Mon Jan 07, 2008 4:53 pm
by Candy
And a tested one in c++0x:

Code: Select all

class mydatatype;

template <typename... T>
void getvalue(mydatatype val, T... values);

class mydatatype {
public:
    mydatatype(int value) {}
    template <typename... T>
    mydatatype(T... value) {
        getvalue(*this, value...);
    }
    const mydatatype &operator*=(const mydatatype &val) {}
    const mydatatype &operator+=(const mydatatype &val) {}
};

template <typename T2, typename... T>
void getvalue(mydatatype val, T2 value, T... values) {
    val *= 1000;
    val += value;
    getvalue(val, values...);
}

void getvalue(mydatatype dty) {
    return;
}

int main() {
        mydatatype huge_num(321,234,567,898,765,432,123,456,789,876);
        return 0;
}
Well.... tested.... it compiles at least? :)

Posted: Mon Jan 07, 2008 5:15 pm
by Zacariaz
looks very interesting, now i just need to understand it.
Ill be back soon ;)

Posted: Mon Jan 07, 2008 8:05 pm
by Zacariaz
i must admit that im somewhat confused Candy. The code you posted, how did it ever compile on your computer? Am i missing some compiler specific features or headers here?

Posted: Mon Jan 07, 2008 8:32 pm
by Alboin
Zacariaz wrote:i must admit that im somewhat confused Candy. The code you posted, how did it ever compile on your computer? Am i missing some compiler specific features or headers here?
It's c++0x C++, the next version of C++. It won't compile on a normal g++ installation.

You'll need the version of GCC that supports a version of it. I haven't installed it myself, but its homepage seems to be here.

Posted: Mon Jan 07, 2008 9:01 pm
by Zacariaz
ok, didnt know that, thanks.

Posted: Tue Jan 08, 2008 1:25 pm
by Candy
Zacariaz wrote:ok, didnt know that, thanks.
To get you started, a working example using the ellipsis method. The c++0x method is slightly better (you don't have to specify how many arguments there are beforehand and because of that you can't do that wrong, that's what the 10 is) but as of this moment only possible on experimental compilers (as correctly noted above).

The specific c++0x construct I used was variadic templates. It should also support parameter packs but I haven't been able to convince my GCC 4.3 preproduction model to accept any int... constructs.

Code: Select all

#include <stdarg.h>
#include <stdint.h>
#include <iostream>
using namespace std;

class mydatatype {
private:
    int *data;
    int count;
public:
    mydatatype(int c, ...) {
        count = c;
        data = new int[c];
        va_list arg_list;
        va_start (arg_list, c);
        while (c--)
            data[c] = va_arg (arg_list, int);
    }
    friend ostream &operator<<(ostream &, const mydatatype &);
};

ostream &operator<<(ostream &os, const mydatatype &ob)
{
        for (int i=ob.count-1; i>=0; i--) {
                os << ob.data[i];
                if (i) os << ",";
        }
        return os;
}

int main() {
        mydatatype huge_num(10,    321,234,567,898,765,432,123,456,789,876);
        cout << huge_num << endl;
}

Posted: Tue Jan 08, 2008 2:35 pm
by Zacariaz
thanks once again, just one thing, the stdint.h? Is it nesacery?
allso i supose there is no real difference between stdarg.h and cstdarg?

Posted: Tue Jan 08, 2008 4:03 pm
by Candy
Zacariaz wrote:thanks once again, just one thing, the stdint.h? Is it nesacery?
allso i supose there is no real difference between stdarg.h and cstdarg?
cstdarg is equal to stdarg, except that everything is in the namespace std. Since I only use macros, it's essentially equal.

stdint.h isn't needed anymore. I had a reference to uint16_t in there, but it got removed, so it's become unnecessary. There's of course also a cstdint, but that's not needed either ;).

Posted: Tue Jan 08, 2008 6:08 pm
by Zacariaz
actually i found that i didnt have cstdint, maybe its time to upgrade...

Posted: Thu Jan 10, 2008 9:12 am
by binutils
googling is no hard at all wrote:

Code: Select all

#include <stdio.h>
#include <gmp.h>

int main (int argc,  char **argv)
{
  MP_INT tt, a1, b1, a2, b2, a, b, tmp1, tmp2, tmp3, num, den, lscl, res;
  MP_RAT x, xtmp, xxtmp;
  int happy;
  FILE *fp;
  fp = fopen("data1", "w");
  mpz_init_set_str(&tt, "1", 10);
  mpz_init_set_str(&a1, "0", 10);
  mpz_init_set_str(&res, "0", 10);
  mpz_init_set_str(&b2, "0", 10);
  mpz_init_set_str(&a2, "1", 10);
  mpz_init_set_str(&b1, "1", 10);
  mpz_init_set_str(&a, "0", 10);
  mpz_init_set_str(&b, "0", 10);
  mpz_init_set_str(&tmp1, "0", 10);
  mpz_init_set_str(&tmp2, "0", 10);
  mpz_init_set_str(&tmp3, "0", 10);
#if 0
  mpz_init_set_str(&num, "6931471805599453094172321", 10);
  mpz_init_set_str(&den, "4054651081081643819780131", 10);
#endif

  mpz_init_set_str(&num,
		   
"69314718055994530941723212145817656807550013436025525412068000949339362196969
471560586332699641868754200148102057068573368552023575813055703267075163507596
193072757082837143519030703862389167347112335011536449795523912047517268157493
206515552473413952588295045300709532636664265410423915781495204374043038550080
194417064167151864471283996817178454695702627163106454615025720740248163777338
963855069526066834113727387372292895649354702576265209885969320196505855476470
330679365443254763274495125040606943814710468994650622016772042452452961268794
654619316517468139267250410380254625965686914419287160829380317271436778265487
756648508567407764845146443994046142260319309673540257444607030809608504748663
852313818167675143866747664789088143714198549423151997354880375165861275352916
610007105355824987941472950929311389715599820565439287170007218085761025236889
213244971389320378439353088774825970171559107088236836275898425891853530243634
214367061189236789192372314672321720534016492568727477823445353476481149418642
386776774406069562657379600867076257199184734022651462837904883062033061144630
073719489002743643965002580936519443041191150608094879306786515887090060520346
842973619384128965255653968602219412292420757432175748909770675268711581705113
700915894266547859596489065305846025866838294002283300538207400567705304678700
184162404418833232798386349001563121889560650553151272199398332030751408426091
479001265168243443893572472788205486271552741877243002489794540196187233980860
831664811490930667519339312890431641370681397776498176974868903887789991296503
619270710889264105230924783917373501229842420499568935992206602204654941510613
918788574424557751020683703086661948089641218680779020818158858000168811597305
618667619918739520076671921459223672060253959543654165531129517598994005600036
651356756905124592682574394648316833262490180382424082423145230614096380570070
255138770268178516306902551370323405380214501901537402950994226299577964742713
815736380172987394070424217997226696297993931270694", 10);

  mpz_init_set_str(&den, 
"10986122886681096913952452369225257046474905578227494517346943336374942932186
089668736157548137320887879700290659578657423680042259305198210528018707672774
106031627691833813671793736988443609599037425703167959115211455919177506713470
549401667755802222031702529468975606901065215056428681380363173732985777823669
916547921318181490200301038236301222486527481982259910974524908964580534670088
459650857484441190188570876474948670796130858294116021661211840014098255143919
487688936798494302255731535329685345295251459213876494685932562794416556941578
272310355168866102118469890439943063138255285736466882824988136822800634143910
786893251456437510204451627561934973982116941585740535361758900975122233797736
969687754354795135712982177017581242122351405810163272465588937249564919185242
960796684234647069377237252655082032078333928055892853146873095132606458309184
397496822230325765467533311823019649275257599132217851353390237482964339502546
074245824934666866121881436526565429542767610505477795422933973323401173743193
974579847018559548494059478353943841010602930762292228131207489306344534025277
732685627148001681871547243978207187803444678021617815841904282007672124325573
801436417887682616104101681872424068790890992987420815218323752894275273253407
100283575069506240396546275224430846258845085978625308322477453888506800348832
434049008399005808094356528212237038870203680454860077621424408869725941358436
599922621173967080495095279271436315464044462308915818536711960837030485352090
967262958241504035599512135545033224174847410033198148783245256933470494993730
165633666099190395712282284488167431215062856999387403881901274483956479103477
288597211985064942279698579166995641855126504150219155471966585692972660652357
329373683002783092177660538703046200766158494670022601175679751800393479176327
784493514263496836003755785716070049818151918437343829093474666045775065927367
012111537058249647984793040420582396475385785096062609338991470612013024310826
0518262958640076003059494321166880446106134684533980", 10);

  mpz_sub(&den, &den, &num);
  mpz_init_set_str(&lscl, "17312340490667563", 10); /* (/ 1.0 (log (expt 2.0 (/ 1.0 1200)))) */
  mpq_init(&x);
  mpq_init(&xtmp);
  mpq_init(&xxtmp);
  mpq_set_num(&x, &num);
  mpq_set_den(&x, &den);
  happy = 1;
  while (happy)
    {
      mpz_mul(&tmp1, &a1, &tt);
      mpz_add(&a, &tmp1, &a2);
      mpz_mul(&tmp1, &b1, &tt);
      mpz_add(&b, &tmp1, &b2);
      mpz_mul(&tmp2, &den, &b);
      mpz_mul(&tmp1, &num, &a);
      mpz_sub(&tmp3, &tmp1, &tmp2);
      mpz_abs(&tmp1, &tmp3);
#if 0
      mpz_mul(&res, &tmp1, &lscl);
      /* printf("%s %s %s\n", mpz_get_str(NULL, 10, &res), mpz_get_str(NULL, 10, &b), mpz_get_str
(NULL, 10, &a)); */
      fprintf(fp, "%s %s %s\n", mpz_get_str(NULL, 10, &res), mpz_get_str(NULL, 10, &b), mpz_get_str
(NULL, 10, &a)); 
#endif
      fprintf(fp, "%s %s\n", mpz_get_str(NULL, 10, &b), mpz_get_str(NULL, 10, &a)); 

      fflush(fp);
      mpq_set_num(&xtmp, &tt);
      mpq_sub(&xxtmp, &x, &xtmp);
      mpq_inv(&x, &xxtmp);
      mpq_get_num(&tmp1, &x);
      mpq_get_den(&tmp2, &x);
      mpz_div(&tt, &tmp1, &tmp2);
      mpz_set(&a2, &a1);
      mpz_set(&b2, &b1);
      mpz_set(&a1, &a);
      mpz_set(&b1, &b);
      happy = mpz_cmp(&num, &b);
    }
  fclose(fp);
}

Posted: Thu Jan 10, 2008 12:02 pm
by Candy
OF course, you can wrap that GMP logic in a class to get rid of mpz_mul etc.