Bill Allombert on Wed, 08 Jun 2005 13:38:32 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: dumb question about rationals |
On Wed, Jun 08, 2005 at 07:20:23AM +0200, Vincent Torri wrote: > > Hello, > > i've just started to use PARI (that is, the library), and I want to use > the rational objects. But I have some problems. > My program is quite simple : > > GEN r1; > GEN r2; > > pari_init (1000000, 2); > > r1 = cgetg (3, t_FRAC); > r2 = cgetg (3, t_FRAC); > > r1[1] = (long)2; > r1[2] = (long)2; > > r2[1] = (long)2; > r2[2] = (long)2; > > r2 = gadd (r2, r1); > > but, when i launch the program, i get: > > *** segmentation fault: bug in PARI or calling program. > *** Error in the PARI system. End of program. > > Could you please tell me why there is this problem, and where my error(s) > is (are) 'r1[1] = (long)2;' is not correct, you must convert the C long integer 2 to a PARI GEN integer using stoi(). 'r1[1] = (long)stoi(2);' should work. Secondly, the numerator and the denominator must be coprime else you build a incorrect PARI object, so 2/2 is not allowed. Usually it is simpler to use gdiv() to build rational numbers. I suggest you try out GP2C. This program convert GP code to C code, so you can use it to generate example code you can reuse. For example, what you are doing is equivalent to the GP code r1=2/2; r2=2/2; r2=r1+r2; GP2C will translate it to the following C code: r1 = gdivgs(gdeux, 2); r2 = gdivgs(gdeux, 2); r2 = gadd(r1, r2); Cheers, Bill.