Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Big numbers

Posted on 2003-12-02
Medium Priority
482 Views
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+x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x10*x10+x11*x11*x11*x11+x12*x12*x12*x12+x13*x13*x13*x13,
x1*x1*x1*y1+x2*x2*x2*y2+x3*x3*x3*y3+x4*x4*x4*y4+x5*x5*x5*y5+x6*x6*x6*y6+x7*x7*x7*y7+x8*x8*x8*y8+x9*x9*x9*y9+x10*x10*x10*y10+x11*x11*x11*y11+x12*x12*x12*y12+x13*x13*x13*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*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,
xr1*x1*x1+xr2*x2*x2+xr3*x3*x3+xr4*x4*x4+xr5*x5*x5+xr6*x6*x6+xr7*x7*x7+xr8*x8*x8+xr9*x9*x9+xr10*x10*x10+xr11*x11*x11+xr12*x12*x12+xr13*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*x1*x1*y1+x2*x2*x2*y2+x3*x3*x3*y3+x4*x4*x4*y4+x5*x5*x5*y5+x6*x6*x6*y6+x7*x7*x7*y7+x8*x8*x8*y8+x9*x9*x9*y9+x10*x10*x10*y10+x11*x11*x11*y11+x12*x12*x12*y12+x13*x13*x13*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,
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;
}
0
Question by:Besbous
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 45

Accepted Solution

sunnycoder earned 150 total points
ID: 9864216
0

LVL 84

Expert Comment

ID: 9864424
Couldn'''t you just declare double x1,x2... ?
0

Author Comment

ID: 9864443
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

LVL 46

Assisted Solution

Kent Olsen earned 450 total points
ID: 9866665
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

LVL 22

Assisted Solution

grg99 earned 150 total points
ID: 9866692
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

LVL 46

Expert Comment

ID: 9866754

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

Author Comment

ID: 9867253
Thank you guys ! I ll try this now !
0

Author Comment

ID: 9867391
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

Author Comment

ID: 9867476
Hey guys !

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

0

LVL 46

Expert Comment

ID: 9867484

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

Author Comment

ID: 9871758

thx a lot Kent, that was helpful !

I got now the result i wanted !

have a good day
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month11 days, 19 hours left to enroll