Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Rounding

 
 
Vincenzo Mercuri
Guest
Posts: n/a
 
      07-12-2010
Richard Heathfield ha scritto:
> Russell Shaw wrote:
>> Hi,
>> 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?

>
> Multiplication and division associate left to right, so the compiler
> can't optimise out the division - it must give the result 20 for the
> above calculation.
>
> Unfortunately, if you mean precisely what you say, y = y / h * h breaks
> when y is an exact multiple of h. Say y = 27 and h = 9 - your calc gives
> 27, which isn't less than y's original value. It is quite possible, even
> likely, that that's what you really meant. But, if you really meant that
> you want to have the largest multiple of h that is *less than* y (not
> less than or equal to y), you can do this instead:
>
> y = ((y + h - 1) / h - 1) * h;



or simply:

y = (y - 1) / h * h;

(you had a great idea!)






--
Vincenzo Mercuri
 
Reply With Quote
 
 
 
 
Billy Mays
Guest
Posts: n/a
 
      07-12-2010
On 7/12/2010 1:54 PM, Vincenzo Mercuri wrote:
>
> or simply:
>
> y = (y - 1) / h * h;
>
> (you had a great idea!)
>



I believe this would give the same result:

(y - 1) - (( y - 1) % h)

but without the multiply and divide.

--
Billy Mays
http://www.jpgdump.com <- My attempt at humor.
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      07-12-2010
Billy Mays wrote:

> On 7/12/2010 1:54 PM, Vincenzo Mercuri wrote:
>>
>> or simply:
>>
>> y = (y - 1) / h * h;
>>
>> (you had a great idea!)
>>

>
>
> I believe this would give the same result:
>
> (y - 1) - (( y - 1) % h)
>
> but without the multiply and divide.


You think % is done without division?


 
Reply With Quote
 
Billy Mays
Guest
Posts: n/a
 
      07-12-2010
On 7/12/2010 3:15 PM, osmium wrote:
> Billy Mays wrote:
>
>> On 7/12/2010 1:54 PM, Vincenzo Mercuri wrote:
>>>
>>> or simply:
>>>
>>> y = (y - 1) / h * h;
>>>
>>> (you had a great idea!)
>>>

>>
>>
>> I believe this would give the same result:
>>
>> (y - 1) - (( y - 1) % h)
>>
>> but without the multiply and divide.

>
> You think % is done without division?
>
>


It can be

/************************************************** *****/
#include <stdio.h>
#include <stdint.h>

int main(int argc, char ** argv)
{
int y;
y = 31;
y = (((int64_t)(y-1) * 0x66666667LL) >> 34) ;
y = (y<<3) + (y<<1);
printf("%d\n", y);
return 0;
}
/************************************************** *****/




--
Billy Mays
http://www.jpgdump.com <- My attempt at humor.
 
Reply With Quote
 
Vincenzo Mercuri
Guest
Posts: n/a
 
      07-13-2010
Billy Mays ha scritto:
> On 7/12/2010 3:15 PM, osmium wrote:
>> Billy Mays wrote:
>>
>>> On 7/12/2010 1:54 PM, Vincenzo Mercuri wrote:
>>>>
>>>> or simply:
>>>>
>>>> y = (y - 1) / h * h;
>>>>
>>>> (you had a great idea!)
>>>>
>>>
>>>
>>> I believe this would give the same result:
>>>
>>> (y - 1) - (( y - 1) % h)
>>>
>>> but without the multiply and divide.

>>
>> You think % is done without division?
>>
>>

>
> It can be
>
> /************************************************** *****/
> #include <stdio.h>
> #include <stdint.h>
>
> int main(int argc, char ** argv)
> {
> int y;
> y = 31;
> y = (((int64_t)(y-1) * 0x66666667LL) >> 34) ;
> y = (y<<3) + (y<<1);
> printf("%d\n", y);
> return 0;
> }
> /************************************************** *****/
>


Well, actually I don't know if the assembly implementation
of the '/' operator is so different from that of a set of
expressions with shift operators. But I think it is likely
the case.



--
Vincenzo Mercuri
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      07-14-2010
On 13 July, 18:58, Vincenzo Mercuri <vincenzo.merc...@gmail.com>
wrote:
> Billy Mays ha scritto:
>
>
>
>
>
> > On 7/12/2010 3:15 PM, osmium wrote:
> >> Billy Mays wrote:

>
> >>> On 7/12/2010 1:54 PM, Vincenzo Mercuri wrote:

>
> >>>> or simply:

>
> >>>> y = (y - 1) / h * h;

>
> >>>> (you had a great idea!)

>
> >>> I believe this would give the same result:

>
> >>> (y - 1) - (( y - 1) % h)

>
> >>> but without the multiply and divide.

>
> >> You think % is done without division?

>
> > It can be

>
> > /************************************************** *****/
> > #include <stdio.h>
> > #include <stdint.h>

>
> > int main(int argc, char ** argv)
> > {
> > int y;
> > y = 31;
> > y = (((int64_t)(y-1) * 0x66666667LL) >> 34) ;
> > y = (y<<3) + (y<<1);
> > printf("%d\n", y);
> > return 0;
> > }
> > /************************************************** *****/

>
> Well, actually I don't know if the assembly implementation
> of the '/' operator is so different from that of a set of
> expressions with shift operators. But I think it is likely
> the case. *


its quite common for compilers to substitute shifts for divides by
powers of 2


 
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