[GRASS-git] [OSGeo/grass] 866c7f: r.surf.random: double min max for DCELL and r.mapc...

Vaclav Petras noreply at github.com
Wed Dec 1 18:01:43 PST 2021


  Branch: refs/heads/main
  Home:   https://github.com/OSGeo/grass
  Commit: 866c7f057a238e17c5200ad4c2136fcd4f8202e0
      https://github.com/OSGeo/grass/commit/866c7f057a238e17c5200ad4c2136fcd4f8202e0
  Author: Vaclav Petras <wenzeslaus at gmail.com>
  Date:   2021-12-01 (Wed, 01 Dec 2021)

  Changed paths:
    M raster/r.surf.random/Makefile
    M raster/r.surf.random/local_proto.h
    M raster/r.surf.random/main.c
    M raster/r.surf.random/randsurf.c
    A raster/r.surf.random/testsuite/test_min_max.py

  Log Message:
  -----------
  r.surf.random: double min max for DCELL and r.mapcalc-style random numbers (#322)

The original code did not allow for floating point numbers
as min and max parameters. The decimal part, if provided, was ignored without
any warning. Now the code accepts doubles for DCELL output and ints for
CELL output. Numbers not fully parseable as ints are now rejected by an
explicit check. (The min and max values travel through the code as doubles
in both cases.)

With the original implementation, the max was could have been exceeded for CELL
maps due to the equation used. On the other hand, the min might not have been reached
with the similar probability as max could have been exceeded for ranges
which contained zero.

For example, in 7.6.1 using 10,000,0000 cells and
-i (CELL output), parameters min=2 max=15 produce histogram (r.report u=p,c)
with bins 2-15 approximatelly 7.14-15% and bin 16 with 2-4 cells.
Same setup with min=-2 max=13 produces similar bin 14 with 0-5 cells,
bin -2 is 0 or 1 cell, bins -1,1-15 6.25% and bin 0 12.50%.

Now using the method r.mapcalc is using for rand()
implemented in f_rand() in xrand.c which does not suffer from the same issues.
For consistency, using the same implementation also for DCELL,
so now both CELL and DCELL options are now using functions from gis.h:
G_mrand48() and G_drand48(). Consequently, the gmath.h dependency
was removed (header file includes and in the Makefile) together with unused math.h.

Additionally to enforcing the int, a parameter check also enforces that min <= max
since min > max seems like a input error. (This is different from r.mapcalc which
flips the values, but there we assume less control over the input and it is too
late to report a message, so it is appropriate and more practical to be more
permissive.) min == max is allowed and not treated in a special way.

Raster history is now being written as well as a map title which contains
the range with interval written differently for CELL and DCELL as
the CELL contains max while DCELL does not (based on [0,1) from G_drand48()).

Test is provided for the min-max comparison and int-double check.
A larger test provided for the generated values as well, although
even with a larger number of cells the desired result might not achieved.
A fixed GRASS_RANDOM_SEED is provided for stability (but it may not be the
one making a future problem to show, although it does for 7.6.2).
Also the exceeded max is much easier to catch than the not reached min,
but both range and closeness are checked for both CELL and DCELL.




More information about the grass-commit mailing list