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

Glynn Clements glynn at gclements.plus.com
Mon Jan 9 05:27:48 EST 2006


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 at gclements.plus.com>




More information about the grass-dev mailing list