• C

Big numbers

hello ! i ve got a problem , I m using the following program, but becoz of big numbers I don t get the exact result. I don t know how to use GMP, I downloaded it, couldn't run.would u pls help out !


#include <stdio.h>


#define IMAX 6

#define JMAX 7



main(){

int x1=172,x2=172,x3=555,x4=514,x5=190,x6=535,x7=473,x8=319,x9=362,x10=244,x11=336,x12=436,x13=467;
int y1=402,y2=154,y3=144,y4=414,y5=96,y6=99,y7=214,y8=248,y9=314, y10=370,y11=96,y12=144,y13=262;
int xr1=360, xr2=210, xr3=200, xr4=360, xr5=180, xr6=180, xr7=240, xr8=260, xr9=300,  xr10=340, xr11=180, xr12=200, xr13=270;



double array[IMAX][JMAX]={
{x1*x1+x2*x2+x3*x3+x4*x4+x5*x5+x6*x6+x7*x7+x8*x8+x9*x9+x10*x10+x11*x11+x12*x12+x13*x13,
  x1*y1+x2*y2+x3*y3+x4*y4+x5*y5+x6*y6+x7*y7+x8*y8+x9*y9+x10*y10+x11*y11+x12*y12+x13*y13,
  x1*x1*x1+x2*x2*x2+x3*x3*x3+x4*x4*x4+x5*x5*x5+x6*x6*x6+x7*x7*x7+x8*x8*x8+x9*x9*x9+x10*x10*x10+x11*x11*x11+x12*x12*x12+x13*x13*x13,
  x1*x1*y1+x2*x2*y2+x3*x3*y3+x4*x4*y4+x5*x5*y5+x6*x6*y6+x7*x7*y7+x8*x8*y8+x9*x9*y9+x10*x10*y10+x11*x11*y11+x12*x12*y12+x13*x13*y13,
  x1*y1*y1+x2*y2*y2+x3*y3*y3+x4*y4*y4+x5*y5*y5+x6*y6*y6+x7*y7*y7+x8*y8*y8+x9*y9*y9+x10*y10*y10+x11*y11*y11+x12*y12*y12+x13*y13*y13,
  x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13,
  xr1*x1+xr2*x2+xr3*x3+xr4*x4+xr5*x5+xr6*x6+xr7*x7+xr8*x8+xr9*x9+xr10*x10+xr11*x11+xr12*x12+xr13*x13},
{x1*y1+x2*y2+x3*y3+x4*y4+x5*y5+x6*y6+x7*y7+x8*y8+x9*y9+x10*y10+x11*y11+x12*y12+x13*y13,
  y1*y1+y2*y2+y3*y3+y4*y4+y5*y5+y6*y6+y7*y7+y8*y8+y9*y9+y10*y10+y11*y11+y12*y12+y13*y13,
  x1*x1*y1+x2*x2*y2+x3*x3*y3+x4*x4*y4+x5*x5*y5+x6*x6*y6+x7*x7*y7+x8*x8*y8+x9*x9*y9+x10*x10*y10+x11*x11*y11+x12*x12*y12+x13*x13*y13,
  x1*y1*y1+x2*y2*y2+x3*y3*y3+x4*y4*y4+x5*y5*y5+x6*y6*y6+x7*y7*y7+x8*y8*y8+x9*y9*y9+x10*y10*y10+x11*y11*y11+x12*y12*y12+x13*y13*y13,
  y1*y1*y1+y2*y2*y2+y3*y3*y3+y4*y4*y4+y5*y5*y5+y6*y6*y6+y7*y7*y7+y8*y8*y8+y9*y9*y9+y10*y10*y10+y11*y11*y11+y12*y12*y12+y13*y13*y13,
  y1+y2+y3+y4+y5+y6+y7+y8+y9+y10+y11+y12+y13,
  xr1*y1+xr2*y2+xr3*y3+xr4*y4+xr5*y5+xr6*y6+xr7*y7+xr8*y8+xr9*y9+xr10*y10+xr11*y11+xr12*y12+xr13*y13},
{x1*x1*x1+x2*x2*x2+x3*x3*x3+x4*x4*x4+x5*x5*x5+x6*x6*x6+x7*x7*x7+x8*x8*x8+x9*x9*x9+x10*x10*x10+x11*x11*x11+x12*x12*x12+x13*x13*x13,
  x1*x1*y1+x2*x2*y2+x3*x3*y3+x4*x4*y4+x5*x5*y5+x6*x6*y6+x7*x7*y7+x8*x8*y8+x9*x9*y9+x10*x10*y10+x11*x11*y11+x12*x12*y12+x13*x13*y13,
  x1*x1*x1*x1+x2*x2*x2*x2+x3*x3*x3*x3+x4*x4*x4*x4+x5*x5*x5*x5+x6*x6*x6*x6+x7*x7*x7*x7+x8*x8*x8*x8+x9*x9*x9*x9+x10*x10*x10*x10+x11*x11*x11*x11+x12*x12*x12*x12+x13*x13*x13*x13,
  x1*x1*x1*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10*x10*y10*y10+x11*x11*y11*y11+x12*x12*y12*y12+x13*x13*y13*y13,
  x1*y1*y1*y1+x2*y2*y2*y2+x3*y3*y3*y3+x4*y4*y4*y4+x5*y5*y5*y5+x6*y6*y6*y6+x7*y7*y7*y7+x8*y8*y8*y8+x9*y9*y9*y9+x10*y10*y10*y10+x11*y11*y11*y11+x12*y12*y12*y12+x13*y13*y13*y13,
  x1*y1+x2*y2+x3*y3+x4*y4+x5*y5+x6*y6+x7*y7+x8*y8+x9*y9+x10*y10+x11*y11+x12*y12+x13*y13,
  xr1*x1*y1+xr2*x2*y2+xr3*x3*y3+xr4*x4*y4+xr5*x5*y5+xr6*x6*y6+xr7*x7*y7+xr8*x8*y8+xr9*x9*y9+xr10*x10*y10+xr11*x11*y11+xr12*x12*y12+xr13*x13*y13},
{x1*y1*y1+x2*y2*y2+x3*y3*y3+x4*y4*y4+x5*y5*y5+x6*y6*y6+x7*y7*y7+x8*y8*y8+x9*y9*y9+x10*y10*y10+x11*y11*y11+x12*y12*y12+x13*y13*y13,
  y1*y1*y1+y2*y2*y2+y3*y3*y3+y4*y4*y4+y5*y5*y5+y6*y6*y6+y7*y7*y7+y8*y8*y8+y9*y9*y9+y10*y10*y10+y11*y11*y11+y12*y12*y12+y13*y13*y13,
  x1*x1*y1*y1+x2*x2*y2*y2+x3*x3*y3*y3+x4*x4*y4*y4+x5*x5*y5*y5+x6*x6*y6*y6+x7*x7*y7*y7+x8*x8*y8*y8+x9*x9*y9*y9+x10*x10*y10*y10+x11*x11*y11*y11+x12*x12*y12*y12+x13*x13*y13*y13,
  x1*y1*y1*y1+x2*y2*y2*y2+x3*y3*y3*y3+x4*y4*y4*y4+x5*y5*y5*y5+x6*y6*y6*y6+x7*y7*y7*y7+x8*y8*y8*y8+x9*y9*y9*y9+x10*y10*y10*y10+x11*y11*y11*y11+x12*y12*y12*y12+x13*y13*y13*y13,
  y1*y1*y1*y1+y2*y2*y2*y2+y3*y3*y3*y3+y4*y4*y4*y4+y5*y5*y5*y5+y6*y6*y6*y6+y7*y7*y7*y7+y8*y8*y8*y8+y9*y9*y9*y9+y10*y10*y10*y10+y11*y11*y11*y11+y12*y12*y12*y12+y13*y13*y13*y13,
  y1*y1+y2*y2+y3*y3+y4*y4+y5*y5+y6*y6+y7*y7+y8*y8+y9*y9+y10*y10+y11*y11+y12*y12+y13*y13,
  xr1*y1*y1+xr2*y2*y2+xr3*y3*y3+xr4*y4*y4+xr5*y5*y5+xr6*y6*y6+xr7*y7*y7+xr8*y8*y8+xr9*y9*y9+xr10*y10*y10+xr11*y11*y11+xr12*y12*y12+xr13*y13*y13},
{x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13,
  y1+y2+y3+y4+y5+y6+y7+y8+y9+y10+y11+y12+y13,
  x1*x1+x2*x2+x3*x3+x4*x4+x5*x5+x6*x6+x7*x7+x8*x8+x9*x9+x10*x10+x11*x11+x12*x12+x13*x13,
  x1*y1+x2*y2+x3*y3+x4*y4+x5*y5+x6*y6+x7*y7+x8*y8+x9*y9+x10*y10+x11*y11+x12*y12+x13*y13,
  y1*y1+y2*y2+y3*y3+y4*y4+y5*y5+y6*y6+y7*y7+y8*y8+y9*y9+y10*y10+y11*y11+y12*y12+y13*y13,
  13,xr1+xr2+xr3+xr4+xr5+xr6+xr7+xr8+xr9+xr10+xr11+xr12+xr13},
};

int i, j, axis;
double pivot, aik;
for(i=0;i<IMAX;i++){
  for(j=0;j<JMAX;j++){
    printf("%10.1f", array[i][j]);
  }
     putchar('\n');
}

   for(axis=0;axis<IMAX;axis++){
     pivot=array[axis][axis];
     for(j=axis;j<JMAX;j++){
    array[axis][j]/=pivot;
     }
     for(i=0;i<IMAX;i++){
       if(i!=axis){
         aik=array[i][axis];
          for(j=0;j<JMAX;j++){
             array[i][j]-=array[axis][j]*aik;
  }
}
}
   }

for(i=0;i<IMAX;i++){
  for(j=0;j<JMAX;j++){
    printf("%5.6f", array[i][j]);
  }
    putchar('\n');
}
   putchar('\n');
return;
}
BesbousAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
Couldn'''t you just declare double x1,x2... ?
0
BesbousAuthor Commented:
i did ! didn t work

 when i compile it it gives negative value, in the matrix!

Im using Cygwin, should i add  these libraries to the cygwin one ?( http://spanky.triumf.ca/www/fractint/arb-prec.html
http://www.tc.umn.edu/~ringx004/mapm-main.html)
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Kent OlsenData Warehouse Architect / DBACommented:
Hi Besbous,

I compiled and ran your program with the x* variables being defined as both int and double and got the same result both times.  The results actually look good (accurate).  The only negative number that I'm seeing is a negative fraction at array[2][6] and array[2][7], but these appear after your calculating loop where you subtract so a negative value is "plausible".

Floating point arithmetic on Intel chips is inherently "iffy".  The bottom bits of any calculation are suspect so you often get answer slightly different that what you expect.  That's the reason you often see floating point value checks that resemble:

#define DELTA (0.01)

if ((Value - Delta) < ExpectedValue && (Value + Delta) > ExpectedValue)
  /*  Value is "close enough  */


However, in your case you don't need floating point arithmetic. The largest value that I see is 1.9E+12.  Slightly larger than 32 bits but a value that will definitely fit into 64 bits.

Most C compilers these days support 64-bit math.  You should be able to define your variables as "_int64" or "long long", depending on your compiler.

By the way, that was quite clever putting array[][] on the stack and letting the compiler generate the values at the time the function is called.  When I first saw the program I didn't realize that you'd taken that approach and was sure that the program wouldn't compile.  :)  


Kent
0
grg99Commented:
You're going  to have overflow problems if you try using integers to compute such large numbers.

I would declare the variables to be of type float or double.

It would also help to move the initialization into separate statements, then you can step thru the statements with a debugger and see what's happening.

It wouldalso help to #define sx(e) (x##e*x##e)    and #define sy(e) (y##e*y##e)  so you can replace all those x11*x11 by sx(11),
that will cut down the visual clutter and chance of typos by a considerable factor.

Even better, if there's some pattern to the array, write some for loops to do the initialization, you may be able to reduce all that comlex babble to
a line or two of code.

0
Kent OlsenData Warehouse Architect / DBACommented:

grg99's on the right track.


#include <stdio.h>

#define IMAX 6
#define JMAX 7

enum {x1=172,x2=172,x3=555,x4=514,x5=190,x6=535,x7=473,x8=319,x9=362,x10=244,x11=336,x12=436,x13=467};
enum {int y1=402,y2=154,y3=144,y4=414,y5=96,y6=99,y7=214,y8=248,y9=314, y10=370,y11=96,y12=144,y13=262};
enum {int xr1=360, xr2=210, xr3=200, xr4=360, xr5=180, xr6=180, xr7=240, xr8=260, xr9=300,  xr10=340, xr11=180, xr12=200, xr13=270};


long array[IMAX][JMAX]={
{x1*x1+x2*x2+x3*x3+x4*x4+x5*x5+x6*x6+x7*x7+x8*x8+x9*x9+x10*x10+x11*x11+x12*x12+x13*x13,
  x1*y1+x2*y2+x3*y3+x4*y4+x5*y5+x6*y6+x7*y7+x8*y8+x9*y9+x10*y10+x11*y11+x12*y12+x13*y13,
  x1*x1*x1+x2*x2*x2+x3*x3*x3+x4*x4*x4+x5*x5*x5+x6*x6*x6+x7*x7*x7+x8*x8*x8+x9*x9*x9+x10*x10*x10+x11*x11*x11+x12*x12*x12+x13*x13*x13,
  x1*x1*y1+x2*x2*y2+x3*x3*y3+x4*x4*y4+x5*x5*y5+x6*x6*y6+x7*x7*y7+x8*x8*y8+x9*x9*y9+x10*x10*y10+x11*x11*y11+x12*x12*y12+x13*x13*y13,
  x1*y1*y1+x2*y2*y2+x3*y3*y3+x4*y4*y4+x5*y5*y5+x6*y6*y6+x7*y7*y7+x8*y8*y8+x9*y9*y9+x10*y10*y10+x11*y11*y11+x12*y12*y12+x13*y13*y13,
  x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13,
...etc

main ()

Now the compiler will generate the initial values for you.  A byproduct is that your executable will be smaller and faster, but the real benefit is that any value that is too large to fit into a long will generate a compilation warning.  You can then experiment with data types.  float should work fine, as should 64-bit integers.

It's generally poor practice to enumerate (or define) variables with these names as they are names commonly used in programming, but in this case we should be fine.


Kent
0
BesbousAuthor Commented:
Thank you guys ! I ll try this now !
0
BesbousAuthor Commented:
Hi kent,

 Thx a lot ! well actually I tried ur suggestion, but it always gave integer overflow in expression. Im using cygwin ! i tried to use some other libraries but
didn t really work !

0
BesbousAuthor Commented:
Hey guys !

 I think it worked ! just with float ! ooof
thank you guys

 
0
Kent OlsenData Warehouse Architect / DBACommented:

One complication will be type casting.  The values generated by enum{} will be treated as ints so the mathematics being done to generate the initial values is integer math.  The compiler is very nicely telling us that we've overflowed the int, so we need to force the compiler to do "long" arithmetic.

This can be accomplished by changing the enum{} to #define, or by incorporating a macro that forces the recast.  grg99 suggested a macro to compute the square of the value.  It's a good idea and also simplifies the code

#define square(e) ((long)e*(long)e)
#define cube(e)  ((long)e*(long)e*(long)e)

Then modify the initialization to resemble:

long array[IMAX][JMAX]={
{square(x1)+square(x2)+square(x3)+square(x4)+square(x5)+square(x6)+square(x7)+square(x8)+sqaure(x9)+square(x10)+square(x11)+square(x12)+square(x13),
  square(x1)+square(x2)+square(x3)+square(x4)+square(x5)+square(x6)+square(x7)+square(x8)+sqaure(x9)+square(x10)+square(x11)+square(x12)+square(x13),
  cube(x1)+cube(x2)+cube(x3)+cube(x4)+cube(x5)+cube(x6)+cube(x7)+cube(x8)+cube(x9)+cube(x10)+cube(x11)+cube(x12)+cube(x13), ,
  cube(x1)+cube(x2)+cube(x3)+cube(x4)+cube(x5)+cube(x6)+cube(x7)+cube(x8)+cube(x9)+cube(x10)+cube(x11)+cube(x12)+cube(x13), ,
  cube(x1)+cube(x2)+cube(x3)+cube(x4)+cube(x5)+cube(x6)+cube(x7)+cube(x8)+cube(x9)+cube(x10)+cube(x11)+cube(x12)+cube(x13), ,
  (long)x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13,
...etc


Kent
0
BesbousAuthor Commented:

thx a lot Kent, that was helpful !

I got now the result i wanted !

have a good day
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.