Velocity Reviews > Rounding a floating point number

# Rounding a floating point number

user923005
Guest
Posts: n/a

 02-28-2008
On Feb 27, 8:10*am, CBFalconer <(E-Mail Removed)> wrote:
> user923005 wrote:
> > CBFalconer <(E-Mail Removed)> wrote:
> >> user923005 wrote:

>
> >> ... snip float rounding discussion ...

>
> >>> You will still have to multiply and divide by powers of 10 and
> >>> use floor() to achieve the same thing because the C99 round()
> >>> functions round to nearest integer. *They do not round to
> >>> nearest k decimal places.

>
> >> Are you claiming that such multiplication and division by 10 is
> >> too complex for the average reader of c.l.c? *

>
> > No, but I am claiming that using the C99 functions to round to N
> > digits will take twice as much work as a function that does it
> > directly, since the multiplication and division by a power of
> > ten and the floor function are all that is necessary in either
> > case. *And so while you can round to N digits using the C99
> > rounding functions, it really does not make a lot of sense to do
> > it that way.

>
> Let me also point out that such rounding is rarely needed. *Most of
> the time maintaining the original alleged precision and rounding
> only the output, via printf, is needed.

I would go one step further:
It's better than rounding the original data.
But sometimes people want to do it anyway.

Bartc
Guest
Posts: n/a

 02-28-2008

"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> user923005 wrote:
>> CBFalconer <(E-Mail Removed)> wrote:
>>> user923005 wrote:
>>>
>>> ... snip float rounding discussion ...
>>>
>>>> You will still have to multiply and divide by powers of 10 and
>>>> use floor() to achieve the same thing because the C99 round()
>>>> functions round to nearest integer. They do not round to
>>>> nearest k decimal places.
>>>
>>> Are you claiming that such multiplication and division by 10 is
>>> too complex for the average reader of c.l.c?

>>
>> No, but I am claiming that using the C99 functions to round to N
>> digits will take twice as much work as a function that does it
>> directly, since the multiplication and division by a power of
>> ten and the floor function are all that is necessary in either
>> case. And so while you can round to N digits using the C99
>> rounding functions, it really does not make a lot of sense to do
>> it that way.

>
> Let me also point out that such rounding is rarely needed. Most of
> the time maintaining the original alleged precision and rounding
> only the output, via printf, is needed.

various examples of why such rounding can be useful. One example was
cleaning noisy data.

--
Bart