Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Rounding

Reply
Thread Tools

Rounding

 
 
deanfamily11
Guest
Posts: n/a
 
      09-13-2005
Say I have the number 15.7, how can I round it up to 16?


 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
 
 
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      09-14-2005
floor(x+0.5)

 
Reply With Quote
 
=?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

 
Reply With Quote
 
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?


 
Reply With Quote
 
=?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


 
Reply With Quote
 
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


 
Reply With Quote
 
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)
 
Reply With Quote
 
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;


 
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