[GRASS5] [bug #3967] (grass) Undocumented: max() returns integer values if at least one argument is an int

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

Subject: Undocumented: max() returns integer values if at least one argument is an int

Platform: GNU/Linux/i386
grass obtained from: CVS
grass binary for platform: Compiled from Sources
GRASS Version: GRASS 6.1.cvs checked out 20050106

According to the r.mapcalc documentation page, max() returns a float if any of its arguments are floats. In fact, map() returns an integer result if any of its arguments are integers!

example:

r.mapcalc "temp1 = 0" # CELL
r.mapcalc "temp2 = 0.1" # FCELL
r.mapcalc "temp3 = max(temp1, temp2)" # Returns 0!
r.mapcalc "temp4 = max(float(temp1), temp2)" # Returns 0.1!

This is confusing. I suppose this is a C library thing. In the scripting languages I use, the cast to float is implicit in these situations.

Minor fix: update the docs to make clear the behavior
Major fix: implicit promotion to float if either (any) raster is of type float or double

David

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

Request Tracker wrote:

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

Subject: Undocumented: max() returns integer values if at least one argument is an int

Platform: GNU/Linux/i386
grass obtained from: CVS
grass binary for platform: Compiled from Sources
GRASS Version: GRASS 6.1.cvs checked out 20050106

According to the r.mapcalc documentation page, max() returns a float
if any of its arguments are floats. In fact, map() returns an
integer result if any of its arguments are integers!

Actually, it return type is the same is its first argument.

example:

r.mapcalc "temp1 = 0" # CELL
r.mapcalc "temp2 = 0.1" # FCELL

Actually, this is DCELL; you would need to use "0.1f" or "float(0.1)"
to get FCELL.

r.mapcalc "temp3 = max(temp1, temp2)" # Returns 0!
r.mapcalc "temp4 = max(float(temp1), temp2)" # Returns 0.1!

r.mapcalc "temp5 = max(temp2, temp1)" # Returns 0.1!

This is confusing. I suppose this is a C library thing.

No, it's a bug:

--- raster/r.mapcalc/check.c~ 2004-11-09 13:45:12.000000000 +0000
+++ raster/r.mapcalc/check.c 2006-01-09 10:19:38.000000000 +0000
@@ -99,11 +99,11 @@
   argt[0] = CELL_TYPE;

   for (i = 1; i <= argc; i++)
- if (argt[1] == FCELL_TYPE)
+ if (argt[i] == FCELL_TYPE)
       argt[0] = FCELL_TYPE;

   for (i = 1; i <= argc; i++)
- if (argt[1] == DCELL_TYPE)
+ if (argt[i] == DCELL_TYPE)
       argt[0] = DCELL_TYPE;

   for (i = 1; i <= argc; i++)

FWIW, this applies to max, min, median and mode (all of the functions
which take a variable number of arguments where the result type should
be the most general of the argument types).

In the scripting languages I use, the cast to float is implicit in these
situations.

Minor fix: update the docs to make clear the behavior

Major fix: implicit promotion to float if either (any) raster is of
type float or double

That's how it was /supposed/ to behave.

Fixed in CVS.

--
Glynn Clements <glynn@gclements.plus.com>

On Mon, Jan 09, 2006 at 10:27:48AM +0000, Glynn Clements wrote:

> this bug's URL: http://intevation.de/rt/webrt?serial_num=3967
> -------------------------------------------------------------------------
>
> Subject: Undocumented: max() returns integer values if at least one argument is an int

...

> Major fix: implicit promotion to float if either (any) raster is of
> type float or double

That's how it was /supposed/ to behave.

Fixed in CVS.

Fixed also in 6.0.2-CVS.

Markus