[GRASS5] [bug #1372] (grass) r.mapcalc: strange multiplication bug

this bug's URL: http://intevation.de/rt/webrt?serial_num=1372
-------------------------------------------------------------------------

Hi,

while working with r.mapcalc (in fact r.mapcalc new version)
we came accross a strange multiplication error:

The idea was to multiply all values in
plancS11.30m@Curvature
with 100000. The result seems to be nonsense.

On Thu, Oct 24, 2002 at 05:43:25PM +0200, Steno Fontanari wrote:
baez:insubri 171%r.info plancS11.30m@Curvature
+----------------------------------------------------------------------------+
| Layer: plancS11.30m Date: Wed Jan 30 16:58:19 2002 |
| Type of Map: raster Number of Categories: 255 |
[...]
| Data Type: DCELL |
| Rows: 3251 |
| Columns: 3871 |
| Total Cells: 12584621 |
| Projection: Transverse Mercator (zone 0) |
| N: 5157109 S: 5059581 Res: 29.9993848 |
| E: 1728648 W: 1612512 Res: 30.00154999 |
| Range of data: min = -466153.648586 max = 466153.648586 |
| |
| Data Source: |
| |
| Data Description: |
| generated by r.param.scale |
+----------------------------------------------------------------------------+

baez:insubri 172%r.mapcalc 'pippo2=int(plancS11.30m@Curvature*100000)'
baez:insubri 172%r.support -r pippo2

  Updating the stats for [pippo2]

baez:insubri 173%r.info pippo2
+----------------------------------------------------------------------------+
| Layer: pippo2 Date: Thu Oct 24 17:26:50 2002 |
| Mapset: erdemolo Login of Creator: fontana |
| Location: pat |
| DataBase: /mpa_gdata/ssi/BIO/GRASS_DATA/data |
| Title: ( pippo2 ) |
|----------------------------------------------------------------------------|
| |
| Type of Map: raster Number of Categories: 1082618906 |
| Data Type: CELL |
| Rows: 3251 |
| Columns: 3871 |
| Total Cells: 12584621 |
| Projection: Transverse Mercator (zone 0) |
| N: 5157109 S: 5059581 Res: 29.9993848 |
| E: 1728648 W: 1612512 Res: 30.00154999 |
| Range of data: min = -1082618906 max = 1082618906 |
| |
| Data Source: |
| Data Description: |
| generated by r.mapcalc |
| |
+----------------------------------------------------------------------------+

Did we hit an upper/lower limit of CELL?
If so, r.mapcalc should refuse to write an output map.

Markus

--- Headers Follow ---

From neteler@itc.it Thu Oct 24 17:49:19 2002

Return-Path: <neteler@itc.it>
Delivered-To: grass-bugs@lists.intevation.de
Received: from mail.intevation.de (aktaia [212.95.126.10])
  by lists.intevation.de (Postfix) with ESMTP id D97971394F
  for <grass-bugs@lists.intevation.de>; Thu, 24 Oct 2002 17:49:18 +0200 (CEST)
Received: from camelot.itc.it (camelot.itc.it [195.223.171.5])
  by mail.intevation.de (Postfix) with ESMTP id 1B4861B6BA
  for <grass-bugs@intevation.de>; Thu, 24 Oct 2002 17:49:17 +0200 (CEST)
Received: from orchestra.itc.it (orchestra [10.0.10.11])
  by camelot.itc.it (8.11.3/8.11.3) with ESMTP id g9OFnFX28107
  for <grass-bugs@intevation.de>; Thu, 24 Oct 2002 17:49:16 +0200 (MET DST)
Received: from thuille.itc.it. (thuille [10.40.0.110])
  by orchestra.itc.it (8.11.6/8.11.6) with ESMTP id g9OFnFt12041;
  Thu, 24 Oct 2002 17:49:15 +0200
Received: (from neteler@localhost)
  by thuille.itc.it. (8.11.6/8.11.2) id g9OFnFn24934;
  Thu, 24 Oct 2002 17:49:15 +0200
Date: Thu, 24 Oct 2002 17:49:15 +0200
From: Markus Neteler <neteler@itc.it>
To: grass-bugs@intevation.de, Steno Fontanari <fontana@itc.it>
Subject: r.mapcalc: strange multiplication bug
Message-ID: <20021024174915.A24931@itc.it>
References: <20021024174325.A6071@baez.itc.it>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: Mutt/1.2.5.1i
In-Reply-To: <20021024174325.A6071@baez.itc.it>; from fontana@itc.it on Thu, Oct 24, 2002 at 05:43:25PM +0200
X-Spam-Status: No, hits=-9.9 required=5.0
  tests=IN_REP_TO,REFERENCES,SPAM_PHRASE_00_01,USER_AGENT,
        USER_AGENT_MUTT
  version=2.41
X-Spam-Level:

-------------------------------------------- Managed by Request Tracker

Request Tracker wrote:

while working with r.mapcalc (in fact r.mapcalc new version)
we came accross a strange multiplication error:

The idea was to multiply all values in
plancS11.30m@Curvature
with 100000. The result seems to be nonsense.

baez:insubri 171%r.info plancS11.30m@Curvature

| Range of data: min = -466153.648586 max = 466153.648586 |

466153.648586 * 100000 = 46615364858.60001, which is 20 times the
range of a 32-bit integer (-2147483648 ... 2147483647).

baez:insubri 172%r.mapcalc 'pippo2=int(plancS11.30m@Curvature*100000)'

This will wrap.

Did we hit an upper/lower limit of CELL?

Yes.

If so, r.mapcalc should refuse to write an output map.

So you want int() to detect overflow errors? That's easy enough.
However, if you had performed the coercion to integer first, then
multiplied, i.e.:

  r.mapcalc 'pippo2=int(plancS11.30m@Curvature)*100000'

then the integer multiplication operator would have to trap the error.
And that is pretty hard to do correctly and portably without a
substantial performance hit.

Note that the old r.mapcalc behaved in exactly the same way. As do the
integer arithmetic operators in most programming languages.

--
Glynn Clements <glynn.clements@virgin.net>