Thread: What is wrong with division?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    What is wrong with division?

    Hi.

    I study C. I have a real variable "r" and I cannot assign a value of 1/7 to it directly. Here it goes:

    Code:
    main()
    {
    float r;
    r=1/7;
    printf("1/7=%.6f\n",r);
    getch();
    }
    This program returns 1/7=0.000000
    While the second program with a source code

    Code:
    main()
    {
    float r,r1;
    r1=7;
    r=1/r1;
    printf("1/7=%.7f\n",r);
    getch();
    }
    returns 1/7=0.142857

    So, in C language I cannot assign a value of 1/7 directly? Or am I doing something wrong?

    Thank you for help.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try r = 1.0/7.0 and see what happens.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Whaw, it actually works now. Mathematicly there is no reason for it, but probably there is some reason in C. Like you cannot divide two integers but two reals you can I guess. Thank you for help.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nerio View Post
    Whaw, it actually works now. Mathematicly there is no reason for it, but probably there is some reason in C. Like you cannot divide two integers but two reals you can I guess. Thank you for help.
    The numeric literals you were using to divide are assumed to be integers. C differentiates by the presence of a decimal point.

    And yes you can divide two integers... try the simple 21/7 .... you will get three. BUT when the answer involves decimal points as in 22/7 you will still get 3... whole numbers only. What do you think happens if the answer you get is less than 1 but greater than 0? That's right, you get 0.
    Last edited by CommonTater; 07-14-2011 at 06:26 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    When dividing two integers you get a quotient plus a remainder. In your case the answer comes out at zero, remainder one.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nerio View Post
    Whaw, it actually works now. Mathematicly there is no reason for it, but probably there is some reason in C. Like you cannot divide two integers but two reals you can I guess.
    Mathematically there is a reason. If computing the ratio of two integers, and the result is also required to be an integer, then some form of truncation or rounding must occur.

    In C, when dividing two values of type int, the result will be of type int. That means, mathematically, that truncation or rounding must occur.

    Similarly, in C, when dividing two values of type float, the result will be of type float.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. true division in C ?
    By acidblue in forum C Programming
    Replies: 3
    Last Post: 12-03-2009, 11:22 PM
  2. Division Result
    By anirban in forum Tech Board
    Replies: 19
    Last Post: 08-13-2008, 10:21 PM
  3. Division By Subtraction
    By blacksnake in forum C++ Programming
    Replies: 19
    Last Post: 01-13-2007, 10:17 AM
  4. Division by 0
    By ajaxthegreater in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-08-2005, 04:21 PM
  5. Help with simple division
    By k_rock923 in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2005, 11:46 AM