Help,how does division truncate?

Discussion in 'C Programming' started by jackie, Aug 18, 2008.

  1. jackie

    jackie Guest

    I know in c that 3/2 gets 1 and 3%2 is also 1,and i think this is
    guaranteed by C99(is that true?),but on the other hand,-3/2 may be -1
    or -2,and -3%2 may be -1 and 1 respectively,it is implement-
    dependent,so,is my understanding of this true(positive division and
    mod is guaranteed while negative is not)? thx for your help in
    advance..
     
    jackie, Aug 18, 2008
    #1
    1. Advertising

  2. jackie

    James Kuyper Guest

    jackie wrote:
    > I know in c that 3/2 gets 1 and 3%2 is also 1,and i think this is
    > guaranteed by C99(is that true?),but on the other hand,-3/2 may be -1
    > or -2,and -3%2 may be -1 and 1 respectively,it is implement-
    > dependent,so,is my understanding of this true(positive division and
    > mod is guaranteed while negative is not)? thx for your help in
    > advance..


    What you say was true for only in C90; in C99 section 6.5.5p6 says "When
    integers are divided, the result of the / operator is the algebraic
    quotient with any fractional part discarded.90)". The "90)" refers to
    footnote 90, which says: 'This is often called ‘‘truncation toward zero"'.

    What that means is that if the algebraic quotient is -1.5, the result -1
    (the .5 is discarded). Since 1.5 gets rounded to 1, and -1.5 gets
    rounded to -1, both rounded results are closer to 0 than the un-rounded
    results, which is why this is referred to as "toward zero".

    This causes problems for me. When I use negative numbers in such
    calculations, what I want, far more often than not, is what they call
    "rounding to negative infinity" - 1.5 gets rounded to 1, and -1.5 gets
    rounded to -2 - both results are closer to negative infinity than the
    number before rounding. However, standardizing it to round toward zero
    still makes things a lot easier for me than not standardizing it at all.
     
    James Kuyper, Aug 18, 2008
    #2
    1. Advertising

  3. jackie

    James Kuyper Guest

    pete wrote:
    > jackie wrote:
    >> I know in c that 3/2 gets 1 and 3%2 is also 1,and i think this is
    >> guaranteed by C99(is that true?),but on the other hand,-3/2 may be -1
    >> or -2,and -3%2 may be -1 and 1 respectively,it is implement-
    >> dependent,so,is my understanding of this true(positive division and
    >> mod is guaranteed while negative is not)? thx for your help in
    >> advance..

    >
    > It depends on whether your implementation
    > conforms to C89 rules or C99 rules.


    He specified C99.
     
    James Kuyper, Aug 18, 2008
    #3
  4. jackie

    jackie Guest

    Thanks pete and James Kuyper,ur replies really make life easier for
    me,actually,i am a student from China,and i don't have the opportunity
    to have a copy of C99,so when i try to make the program more
    portable,sometimes i really don't know what the standards are.So
    really thank you for ur help here.
     
    jackie, Aug 18, 2008
    #4
  5. jackie wrote:
    > Thanks pete and James Kuyper,ur replies really make life easier for
    > me,actually,i am a student from China,and i don't have the opportunity
    > to have a copy of C99,so when i try to make the program more
    > portable,sometimes i really don't know what the standards are.So
    > really thank you for ur help here.


    Google for n1256.pdf, in case Google censored that for you, try
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

    This the current C99 with TC1, TC2 and TC3 incorporated.

    Bye, Jojo
     
    Joachim Schmitz, Aug 18, 2008
    #5
  6. jackie

    jackie Guest


    > Google for n1256.pdf, in case Google censored that for you, tryhttp://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
    >
    > This the current C99 with TC1, TC2 and TC3 incorporated.
    >
    > Bye, Jojo


    thank u Joachim Schmitz,but what's the meaning of TC1,2,3? Is it still
    a draft or it has already been approved? thx
     
    jackie, Aug 19, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yannick Turgeon

    Regexp to truncate

    Yannick Turgeon, Oct 11, 2003, in forum: Perl
    Replies:
    1
    Views:
    449
    Yannick Turgeon
    Oct 13, 2003
  2. Eddie
    Replies:
    1
    Views:
    9,756
    Arthur Yousif
    Dec 23, 2003
  3. Replies:
    4
    Views:
    531
    Jack Klein
    Jun 5, 2004
  4. Natevw

    truncate end of file

    Natevw, Nov 3, 2004, in forum: C++
    Replies:
    1
    Views:
    3,204
    Victor Bazarov
    Nov 3, 2004
  5. Replies:
    94
    Views:
    4,689
    ¬a\\/b
    Feb 9, 2007
Loading...

Share This Page