Dividing 1 by a number k

F

ferbar

Hi,

Below is the code for obtaining k intervals, each of size 1/k. But when
I do the division, the result is always 0. I believe is about the
format, but i'm not sure what to change to obtain:

0.0099009900990099009900990099009901

Thanks.

FBM

void divide_interval_01(double * interval) {
int i, k = K;
double interval_i = 1;

interval_i = (1 / k);
for (i=0; i < k; i++)
interval = interval_i * (i+1);

}
int main(int argc, char** argv) {
double * sample, * intervals;
(...)
intervals = (double*) malloc(K*sizeof(double));
/* Chi-square test */

divide_interval_01(intervals);
(...)
 
A

Alexei A. Frounze

ferbar said:
Below is the code for obtaining k intervals, each of size 1/k. But when
I do the division, the result is always 0. ....
interval_i = (1 / k);

Both 1 and k are integer, hence you get the integer quotient (0). If you
make one of those floating point, you'll get a nonzero quotient.

Alex
 
S

SM Ryan

# # > Below is the code for obtaining k intervals, each of size 1/k. But when
# > I do the division, the result is always 0.
# ...
# > interval_i = (1 / k);
#
# Both 1 and k are integer, hence you get the integer quotient (0). If you
# make one of those floating point, you'll get a nonzero quotient.

Also due to precision issues, you get more accurate, and slower,
results if you divide inside the loop instead of multiply by the
reciprocal.
 
M

Martin Ambuhl

ferbar said:
Hi,

Below is the code for obtaining k intervals, each of size 1/k. But when
I do the division, the result is always 0. [...]
int i, k = K;
double interval_i = 1;

interval_i = (1 / k);

1 is an integer; k is an integer.
1 / k yields an integer value, which is 0 for k > 1.
On the other hand 1.0 (the zero is for readability) is a floating point
value and
1.0 / k yields a floating point value
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top