Re: Rounding

Discussion in 'C Programming' started by Francois Grieu, Jul 12, 2010.

  1. On 12/07/2010 10:58, Russell Shaw wrote:
    > I want to round a number, y, to the largest multiple of h that is
    > less than y. Can i do:
    >
    > int y = 27;
    > int h = 10;
    >
    > y = y/h*h;
    >
    > should give 20, or will compiler optimization cancel out h?


    It will do what you want. The compiler is not free to make
    optimizations that change the result of integer expressions.

    Notice that for y = -27, you could get either -30 or -20.
    That's another reason to use unsigned.

    Other strictly equivalent options:
    y = y-y%h;
    y -= y%h;

    If y is non-negative and h a power of two, there is also
    y &= ~(h-1);

    Francois Grieu
     
    Francois Grieu, Jul 12, 2010
    #1
    1. Advertising

  2. Francois Grieu ha scritto:
    > On 12/07/2010 10:58, Russell Shaw wrote:
    >> I want to round a number, y, to the largest multiple of h that is
    >> less than y. Can i do:
    >>
    >> int y = 27;
    >> int h = 10;
    >>
    >> y = y/h*h;
    >>
    >> should give 20, or will compiler optimization cancel out h?

    >
    > It will do what you want. The compiler is not free to make
    > optimizations that change the result of integer expressions.
    >
    > Notice that for y = -27, you could get either -30 or -20.


    Yes, this is right for C90. Nevertheless compiling under C99
    mode, you will always get -20




    --
    Vincenzo Mercuri
     
    Vincenzo Mercuri, Jul 12, 2010
    #2
    1. Advertising

  3. Francois Grieu

    Guest

    What's C99?

    Dean

    On 07/12/2010 03:30:01:896 Vincenzo Mercuri <> wrote:


    >
    > Francois Grieu ha scritto:
    > > On 12/07/2010 10:58, Russell Shaw wrote:
    > >> I want to round a number, y, to the largest multiple of h that is
    > >> less than y. Can i do:
    > >>
    > >> int y = 27;
    > >> int h = 10;
    > >>
    > >> y = y/h*h;
    > >>
    > >> should give 20, or will compiler optimization cancel out h?

    > >
    > > It will do what you want. The compiler is not free to make
    > > optimizations that change the result of integer expressions.
    > >
    > > Notice that for y = -27, you could get either -30 or -20.

    >
    > Yes, this is right for C90. Nevertheless compiling under C99
    > mode, you will always get -20
    >
    >
    >
    >
     
    , Jul 13, 2010
    #3
  4. On Jul 13, 2:55 pm, wrote:

    Please don't top-post.

    > What's C99?


    <http://www.lmgtfy.com/?q=c99&l=1>

    > <snip>


    --
    Peter
     
    Peter Nilsson, Jul 13, 2010
    #4
  5. don't top-post. Put your reply after or interspersed with the material
    you are replying to. Trim anything you are not replying to taht does
    not helpful context. I have corrected your post.

    On 13 July, 05:55, wrote:>
    > On 07/12/2010 03:30:01:896 Vincenzo Mercuri <> wrote:


    <snip>

    > > Yes, this is right for C90. Nevertheless compiling under C99
    > > mode, you will always get -20

    >
    > What's C99?


    The C programming language has been standardised originally by the
    American standards organisation, ANSI. This standard was adopted,
    essentially unchanged, by the International standards organistaion,
    ISO. As the ANSI standard came out in 1989 it (and its ISO equivalent)
    are often referred to as C89 (occaisionally as C90 (the date of ISO
    adoption)). In 1999 ISO issued a new standard for C and this is often
    referred to as C99. C99 tightened up some definitions including the
    meaning of division for negative numbers.
     
    Nick Keighley, Jul 13, 2010
    #5
    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. walala
    Replies:
    12
    Views:
    1,876
  2. valentin tihomirov

    rounding to integer

    valentin tihomirov, Feb 15, 2004, in forum: VHDL
    Replies:
    2
    Views:
    9,644
    Jonathan Bromley
    Feb 16, 2004
  3. C

    Rounding Numbers

    C, Aug 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    499
  4. Thor W Hammer

    Formatting a number without rounding

    Thor W Hammer, Nov 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    509
    Karl Seguin
    Nov 22, 2005
  5. iksrazal
    Replies:
    1
    Views:
    734
    Chris Smith
    Jul 3, 2003
Loading...

Share This Page