Re: Rounding a floating point number




"CBFalconer" <cbfalconer@xxxxxxxxx> wrote in message
news:47C58B78.4F05267@xxxxxxxxxxxx
user923005 wrote:
CBFalconer <cbfalco...@xxxxxxxxx> 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.

There was a long discussion about this stuff last November in c.l.c with
various examples of why such rounding can be useful. One example was
cleaning noisy data.

--
Bart



.



Relevant Pages

  • RE: Rounding of floating point numbers
    ... what I am trying to do, I don't think "banker's rounding" will work. ... The total number of patterns I need will vary each time the ... The rule I must follow for odd numbers is to divide by two, round up ... nearest integer, numbers with .51 are always rounded up ...
    (perl.beginners)
  • Re: Round to Nearest Integer in Access
    ... always round to the nearest integer. ... For gimmicky supermarket 'rounding ...
    (microsoft.public.access.queries)
  • Re: Rounding a floating point number
    ... snip float rounding discussion ... ... functions round to nearest integer. ... but I am claiming that using the C99 functions to round to N ...
    (comp.lang.c)
  • Re: Rounding a floating point number
    ... round to nearest integer. ... but I am claiming that using the C99 functions to round to N ... the floor function are all that is necessary in either case. ...
    (comp.lang.c)
  • Re: round number to nearest integer.
    ... > I received some help here a week ago about rounding up numbers using ... Unfortunately it turns out that I need to round up/down numbers ... > to their nearest integer. ... an integer by adding 1/2, taking the floor of the result, and casting ...
    (comp.lang.java.programmer)