Velocity Reviews > C++ > Rounding

# Rounding

deanfamily11
Guest
Posts: n/a

 09-13-2005
Say I have the number 15.7, how can I round it up to 16?

Victor Bazarov
Guest
Posts: n/a

 09-14-2005
deanfamily11 wrote:
> Say I have the number 15.7, how can I round it up to 16?

Add 0.5 and assign it to an integer.

V

kevin.hall@motioneng.com
Guest
Posts: n/a

 09-14-2005
floor(x+0.5)

=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a

 09-14-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> floor(x+0.5)

ceil(x);

Ali

Victor Bazarov
Guest
Posts: n/a

 09-14-2005
Ali Çehreli wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ps.com...
>> floor(x+0.5)

>
> ceil(x);

That would be ill-advised. ceil(0.1) gives 1, which actually
should be 0 AFA rounding is concerned, no?

=?utf-8?Q?Ali_=C3=87ehreli?=
Guest
Posts: n/a

 09-14-2005
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ali ehreli wrote:
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) ps.com...
>>> floor(x+0.5)

>>
>> ceil(x);

>
> That would be ill-advised. ceil(0.1) gives 1, which actually
> should be 0 AFA rounding is concerned, no?

I realized it as soon as I posted my response; still, I will use the
incomplete requirement as an excuse:

"Say I have the number 15.7, how can I round it up to 16?"

Ali

Note: I've found at least one reference on the web, which mentions the use
of a ceil-like functionality under the title "Rounding up."

http://support.microsoft.com/default...b;en-us;196652

Victor Bazarov
Guest
Posts: n/a

 09-14-2005
Ali Cehreli wrote:
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Ali ehreli wrote:
>>> <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed) ps.com...
>>>> floor(x+0.5)
>>>
>>> ceil(x);

>>
>> That would be ill-advised. ceil(0.1) gives 1, which actually
>> should be 0 AFA rounding is concerned, no?

>
> I realized it as soon as I posted my response; still, I will use the
> incomplete requirement as an excuse:
>
> "Say I have the number 15.7, how can I round it up to 16?"
>
>
>
> Ali
>
> Note: I've found at least one reference on the web, which mentions
> the use of a ceil-like functionality under the title "Rounding up."
>
> http://support.microsoft.com/default...b;en-us;196652

Yes, we both used what is allowed on Usenet and sometimes leads to
unexpected results -- assumptions I assumed the requirement was
to round in the general sense, you used the precise requirements
stated in the posting. Either can be incorrect.

V

Pete Becker
Guest
Posts: n/a

 09-14-2005
Victor Bazarov wrote:
>
> That would be ill-advised. ceil(0.1) gives 1, which actually
> should be 0 AFA rounding is concerned, no?
>

Usually, but if the rounding mode is round upward or round away from
zero then 1 is right (but not right for negative numbers in the latter
case). <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

M
Guest
Posts: n/a

 09-14-2005
On Tue, 13 Sep 2005 18:05:23 -0700, Ali Çehreli <(E-Mail Removed)>
wrote:

>I realized it as soon as I posted my response; still, I will use the
>incomplete requirement as an excuse:
>
>"Say I have the number 15.7, how can I round it up to 16?"
>
>
>
>Ali
>

He didn't say up or nearest so it's a pretty good excuse.

He also didn't say what he wanted to do with the rounded value.

Usually, when I'm rounding, it's for printing output. If that's the
case here, then one can use formatting:

printf("The number is: %.0f\n", num");
or
cout.setf(cout.fixed, cout.floatfield);
cout.precision(0);
cout << "The number is: " << num << endl;