473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dividing 1 by a number k

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[i] = 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);
(...)

Nov 15 '05 #1
4 1349
"ferbar" <fb******@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
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
Nov 15 '05 #2
Right! thanks.

FBM

Nov 15 '05 #3
"Alexei A. Frounze" <al*****@chat.ru> wrote:
# "ferbar" <fb******@gmail.com> wrote in message
# news:11*********************@g44g2000cwa.googlegro ups.com...
# > 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Title does not dictate behaviour.
Nov 15 '05 #4
ferbar wrote:
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
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jack Higgs | last post by:
I'm building a maths program for year 6 children. Problem with division - I generate a random number between say 1 and 1000. I want to generate another random number which when divided by the...
5
by: M. Akkerman | last post by:
Hi, I've been working on this problem for a while now but I just can't seem to find a good solution for it. Here is the situation. I have the following class class TBigNumber {
65
by: Pmb | last post by:
I'm confused as to what the compiler error message I'm getting is refering to. Can someone take a gander and let me know what I did wrong? The program is below. When I compile it I get the...
5
by: Hasanain F. Esmail | last post by:
I have spent many hours to solve this problem but no result yet. Your help will highly be appriciated. Problem: Let us say I have two field named as number1 and number2 There are 12 pcs of an...
1
by: Tom Keane | last post by:
I pretty much want to know how to get the number of records returned from a query, so I can divide that number into another number and place it as another field in the query result. Is this...
3
by: gh | last post by:
I am dividing var1 by var2 and I want to check and see if there is a remainder. How do I do this in C#? TIA
8
by: eyoung | last post by:
I placed some code below...took out most of the unneeded html crap for this. this gives me a listing of phone charges...every other line with a different color for easy reading. What I want is...
4
by: Harish Ramadurgam | last post by:
hai, Can anyone help me how to divide my program into different projects? I am using VC++ compiler. Regards, Harish.
2
by: meggahertz | last post by:
Hello Everyone! I need help with dividing a list of numbers in to groups of numbers that have constant difference between them. This constant number does not change it remains the same . e.g. 1000...
20
by: chazzy69 | last post by:
Hi all, i have a php script which retrieves listings from a database and then displays them on a page, the problem is that sometimes the number of listings can be in excess of 100 or more. So what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.