Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Rounding

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
nothing@geocities.com
Guest
Posts: n/a
 
      07-13-2010
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
>
>
>
>

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      07-13-2010
On Jul 13, 2:55*pm, noth...@geocities.com wrote:

Please don't top-post.

> What's C99?


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

> <snip>


--
Peter
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      07-13-2010
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, noth...@geocities.com wrote:>
> On 07/12/2010 03:30:01:896 Vincenzo Mercuri <vincenzo.merc...@gmail.com> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting a number without rounding Thor W Hammer ASP .Net 2 11-22-2005 06:51 PM
rounding to integer valentin tihomirov VHDL 2 02-16-2004 10:07 AM
will Synposys Design Compiler support division by two's power and integer rounding? walala VHDL 12 09-14-2003 03:49 PM
Rounding Numbers C ASP .Net 2 08-25-2003 03:24 PM
prevent rounding with Number.floatValue() ? iksrazal Java 1 07-03-2003 07:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57