# Re: Rounding

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

1. ### Francois GrieuGuest

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

2. ### Vincenzo MercuriGuest

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

3. ### 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
4. ### Peter NilssonGuest

On Jul 13, 2:55 pm, wrote:

> What's C99?

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

> <snip>

--
Peter

Peter Nilsson, Jul 13, 2010
5. ### Nick KeighleyGuest

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