c newbie question

M

mre

for a big integer(e.g. 100000000000) the INT can't handle this,
so I have to use FLOAT, but here comes the problem,
for INT, 5/2 gives me 2, but how can I do so for FLOAT? For example, I
want 500000000000/200000000000 equals to 2 (500000000000 and
200000000000 are both FLOAT), how can I do that?

Thanks!
 
P

Peter Nilsson

mre said:
for a big integer(e.g. 100000000000) the INT can't handle this,
so I have to use FLOAT, but here comes the problem,

What are INT and FLOAT? The C language uses int and float.
for INT, 5/2 gives me 2, but how can I do so for FLOAT? For example, I
want 500000000000/200000000000 equals to 2 (500000000000 and
200000000000 are both FLOAT), how can I do that?

Either use long long if available, or try using doubles and look at the functions
available in <math.h>: floor, ceil, modf, etc...

On many implementations float and int are the same size. Since float has to store an
exponent, this means it may have _less_ precision than an int.
 
L

Leor Zolman

for a big integer(e.g. 100000000000) the INT can't handle this,
so I have to use FLOAT, but here comes the problem,
for INT, 5/2 gives me 2, but how can I do so for FLOAT? For example, I
want 500000000000/200000000000 equals to 2 (500000000000 and
200000000000 are both FLOAT), how can I do that?

Thanks!

Just take the "floor" of your double result:

#include <stdio.h>
#include <math.h>

int main(void)
{
double num = 500000000000, denom = 200000000000;

double d = floor (num / denom);
printf("result = %f\n", d);

return 0;
}

Output:

result = 2.000000

-leor
 
M

Martin Ambuhl

mre said:
for a big integer(e.g. 100000000000) the INT can't handle this,
so I have to use FLOAT, but here comes the problem,
for INT, 5/2 gives me 2, but how can I do so for FLOAT? For example, I
want 500000000000/200000000000 equals to 2 (500000000000 and
200000000000 are both FLOAT), how can I do that?


#include <stdio.h>
#include <math.h>
#include <float.h>

int main(void)
{
float xf = 500000000000.f, yf = 200000000000.f;
double x = 500000000000., y = 200000000000.;
double ipart;
modf(xf / yf, &ipart); /* ignoring returned fractional part */
printf("%.*gf/%.*gf = %.*g\n", FLT_DIG, xf, FLT_DIG, yf, DBL_DIG,
ipart);
modf(xf / yf, &ipart); /* ignoring returned fractional part */
printf("%.*g/%.*g = %.*g\n", DBL_DIG, x, DBL_DIG, y, DBL_DIG,
ipart);
return 0;
}

5e+11f/2e+11f = 2
500000000000/200000000000 = 2
 
E

E. Robert Tisdale

Leor said:
Just take the "floor" of your double result:

> cat main.c
#include <stdio.h>
#include <math.h>

int main(int argc, char* argv[]) {
double dnum = -5, ddenom = +2;
int inum = -5, idenom = +2;

int i = inum/idenom;
double d = floor(dnum/ddenom);
printf("result = %d = -5/+2\n", i);
printf("result = %f = floor(-5/+2)\n", d);

return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c -lm
> ./main
result = -2 = -5/+2
result = -3.000000 = floor(-5/+2)
 
E

E. Robert Tisdale

Martin said:
#include <stdio.h>
#include <math.h>
#include <float.h>

int main(void)
{
float xf = 500000000000.f, yf = 200000000000.f;
double x = 500000000000., y = 200000000000.;
double ipart;
modf(xf / yf, &ipart); /* ignoring returned fractional part */
printf("%.*gf/%.*gf = %.*g\n", FLT_DIG, xf, FLT_DIG, yf, DBL_DIG,
ipart);
modf(xf / yf, &ipart); /* ignoring returned fractional part */
printf("%.*g/%.*g = %.*g\n", DBL_DIG, x, DBL_DIG, y, DBL_DIG,
ipart);
return 0;
}

5e+11f/2e+11f = 2
500000000000/200000000000 = 2

> cat main.c
#include <stdio.h>
#include <float.h>
#include <math.h>

int main(int argc, char* argv[]) {
float xf = 500000000000.f, yf = 200000000000.f;
double x = 500000000000., y = 200000000000.;
printf("%.*gf/%.*gf = %.*g\n",
FLT_DIG, xf, FLT_DIG, yf, DBL_DIG, truncf(xf/yf));
printf("%.*g/%.*g = %.*g\n",
DBL_DIG, x, DBL_DIG, y, DBL_DIG, trunc(x/y));
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c -lm
> ./main
5e+11f/2e+11f = 2
500000000000/200000000000 = 2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top