Velocity Reviews > Re: Rounding

# Re: Rounding

Francois Grieu
Guest
Posts: n/a

 07-12-2010
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

Vincenzo Mercuri
Guest
Posts: n/a

 07-12-2010
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

nothing@geocities.com
Guest
Posts: n/a

 07-13-2010
What's C99?

Dean

On 07/12/2010 03:30:01:896 Vincenzo Mercuri <(E-Mail Removed)> 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
>
>
>
>

Peter Nilsson
Guest
Posts: n/a

 07-13-2010
On Jul 13, 2:55*pm, (E-Mail Removed) wrote:

> What's C99?

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

> <snip>

--
Peter

Nick Keighley
Guest
Posts: n/a

 07-13-2010
you are replying to. Trim anything you are not replying to taht does

On 13 July, 05:55, (E-Mail Removed) wrote:>
> On 07/12/2010 03:30:01:896 Vincenzo Mercuri <(E-Mail Removed)> 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.