[GRASS-dev] [GRASS GIS] #1976: r.mapcalc: Allow rounding of floating numbers
Paulo van Breugel
p.vanbreugel at gmail.com
Tue May 21 08:27:41 PDT 2013
On Tue, May 21, 2013 at 5:00 PM, GRASS GIS <trac at osgeo.org> wrote:
> #1976: r.mapcalc: Allow rounding of floating numbers
>
> -------------------------+--------------------------------------------------
> Reporter: pvanbosgeo | Owner: grass-dev@…
> Type: enhancement | Status: new
> Priority: normal | Milestone: 7.0.0
> Component: Raster | Version: unspecified
> Keywords: r.mapcalc | Platform: Unspecified
> Cpu: Unspecified |
>
> -------------------------+--------------------------------------------------
>
> Comment(by mmetz):
>
> Replying to [comment:4 glynn]:
> > Replying to [comment:2 mmetz]:
> >
> > > The output type of round() is now the same like the input type.
> >
> > This changes long-standing behaviour in a way which could break scripts.
>
> OK. Restoring the original behaviour for round(x) is easy. But it would be
> nice to have a round(x, y) function that preserves the data type of x in
> order to have a possibility to avoid integer overflow.
>
I am not sure I understand the solution proposed by Glyn, but does it mean
we are back at the results of round always be an integer? That would be
disappointing imho. Preserving the data type does make so much more sense.
>
> >
> > > Rounding to a given number of decimal places is supported with
> round(x, y)
> >
> > From the mailing list discussion ...
> >
> > Rounding to a given number of decimals is unnecessarily limiting. The
> algorithm for generalised rounding is essentially:
> >
> {{{
> roundTo(x, k) = round(x / k) * k.
> }}}
> > Rounding to N decimal places is just a case of using k=1/10^N^. If you
> > allow k to be specified directly, then you can round to arbitrary
> > steps (e.g. k=5 would round to the nearest multiple of 5, etc).
>
> I was just looking at the function round() in R which rounds to decimal
> places. Generalised rounding makes more sense.
>
> >
> > However: there's a slight problem with doing it that way: 0.1 isn't
> > exactly representable in binary, so e.g. x/0.1 isn't equal to x*10; it
> > would be more accurate to use:
> {{{
> roundTo(x, k) = round(x * k) / k
> }}}
> > where k is the reciprocal of the step, so k=10^N^ to round to N decimal
> > places (or k=2 to round to 1/2).
>
> >
> > Unless someone has a better idea, I plan to change the round() function
> so that the second argument is the step value, and add an roundi() (round-
> inverse) function where the second argument is the reciprocal of the step
> value (to avoid the rounding error when using a step of 10^-N^).
>
> Sounds good to me.
>
> --
> Ticket URL: <http://trac.osgeo.org/grass/ticket/1976#comment:5>
> GRASS GIS <http://grass.osgeo.org>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20130521/1a5f59f3/attachment.html>
More information about the grass-dev
mailing list