[GRASS-dev] [GRASS GIS] #1976: r.mapcalc: Allow rounding of floating numbers
GRASS GIS
trac at osgeo.org
Tue May 21 08:00:31 PDT 2013
#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.
>
> > 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>
More information about the grass-dev
mailing list