# [GRASS-dev] Overflow warning in r.mapcalc calculation

Rainer M. Krug Rainer at krugs.de
Fri May 17 10:22:36 PDT 2013

```Glynn Clements <glynn at gclements.plus.com> writes:

> Rainer M. Krug wrote:
>
>> > One option is to modify round() to take a second argument (defaulting
>> > to 1), and have it return the first argument rounded to the nearest
>> > multiple of the second. The return type would be that of the second
>> > argument, i.e. round(x,1) rounds to the nearest integer and returns an
>> > integer, round(x,1.0) rounds to the nearest integer and returns a
>> > float, round(x,1e-3) would round to 3 decimal places (i.e. the nearest
>> > multiple of 0.001), etc.
>>
>> Sounds like a sensible approach without adding to many new
>> functions. But I would actually split the two, i.e. have two more
>> arguments, where one specifies the type,
>> and the other one the number of decimals to round to, i.e.
>>
>> round(x, 0, "I") would be the default, rounding to whole number and
>> return an integer
>
> That interface isn't possible. r.mapcalc doesn't have a string type,
> and there's no way that a function's return type can depend upon the
> value of a parameter.

In this case I would suggest different functions, e.g.

round(x) - as it is now

roundI(x, dec) - round to integer, dec>0 is ignored, dec<0 as below

roundD(x, dec) - round to double, where dec is the number of decimals it
should be rounded to

roundF(x, dec) - round to float, where dec is the number of decimals it
should be rounded to

if dec is positive, it is the number of decimals, if negative the
... (no idea what it is called, but if it is -1, 111 is rounded to 110,
-2 111 is rounded to 100)

default from dec would be 0.

These different functions would also have the advantage of probably
being faster then one single function.

Cheers,

Rainer

--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer at krugs.de

Skype:      RMkrug
```