Skip to content

Commit

Permalink
r.stream.*: Avoid comparing floating point numbers using not equal (#567
Browse files Browse the repository at this point in the history
)

* r.stream.*: Avoid comparing floating point numbers using not equal

* Use GRASS_EPSILON instead of an arbitrary threshold
  • Loading branch information
HuidaeCho committed Jun 28, 2021
1 parent e557981 commit 4d72dba
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
8 changes: 4 additions & 4 deletions grass7/raster/r.stream.basins/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ int ram_read_map(MAP * map, char *input_map_name, int check_res,
G_get_window(&this_window);
Rast_get_cellhd(input_map_name, mapset, &cellhd);
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down Expand Up @@ -410,8 +410,8 @@ int seg_read_map(SEG * seg, char *input_map_name, int check_res,

/* check resolution equal any integer check; equal 0 no check */
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down
8 changes: 4 additions & 4 deletions grass7/raster/r.stream.channel/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ int ram_read_map(MAP * map, char *input_map_name, int check_res,
G_get_window(&this_window);
Rast_get_cellhd(input_map_name, mapset, &cellhd);
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down Expand Up @@ -410,8 +410,8 @@ int seg_read_map(SEG * seg, char *input_map_name, int check_res,

/* check resolution equal any integer check; equal 0 no check */
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down
8 changes: 4 additions & 4 deletions grass7/raster/r.stream.distance/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ int ram_read_map(MAP * map, char *input_map_name, int check_res,
G_get_window(&this_window);
Rast_get_cellhd(input_map_name, mapset, &cellhd);
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down Expand Up @@ -410,8 +410,8 @@ int seg_read_map(SEG * seg, char *input_map_name, int check_res,

/* check resolution equal any integer check; equal 0 no check */
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down
8 changes: 4 additions & 4 deletions grass7/raster/r.stream.order/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ int ram_read_map(MAP * map, char *input_map_name, int check_res,
G_get_window(&this_window);
Rast_get_cellhd(input_map_name, mapset, &cellhd);
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down Expand Up @@ -410,8 +410,8 @@ int seg_read_map(SEG * seg, char *input_map_name, int check_res,

/* check resolution equal any integer check; equal 0 no check */
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down
8 changes: 4 additions & 4 deletions grass7/raster/r.stream.segment/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ int ram_read_map(MAP * map, char *input_map_name, int check_res,
G_get_window(&this_window);
Rast_get_cellhd(input_map_name, mapset, &cellhd);
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down Expand Up @@ -410,8 +410,8 @@ int seg_read_map(SEG * seg, char *input_map_name, int check_res,

/* check resolution equal any integer check; equal 0 no check */
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down
3 changes: 2 additions & 1 deletion grass7/raster/r.stream.slope/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ int main(int argc, char *argv[])

G_get_window(&window);
Rast_get_cellhd(in_dir_opt->answer, mapset, &cellhd);
if (window.ew_res != cellhd.ew_res || window.ns_res != cellhd.ns_res)
if (fabs(window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
in_dir_opt->answer, in_dir_opt->answer);
Expand Down
8 changes: 4 additions & 4 deletions grass7/raster/r.stream.snap/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ int ram_read_map(MAP * map, char *input_map_name, int check_res,
G_get_window(&this_window);
Rast_get_cellhd(input_map_name, mapset, &cellhd);
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down Expand Up @@ -410,8 +410,8 @@ int seg_read_map(SEG * seg, char *input_map_name, int check_res,

/* check resolution equal any integer check; equal 0 no check */
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down
8 changes: 4 additions & 4 deletions grass7/raster/r.stream.stats/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ int ram_read_map(MAP * map, char *input_map_name, int check_res,
G_get_window(&this_window);
Rast_get_cellhd(input_map_name, mapset, &cellhd);
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down Expand Up @@ -410,8 +410,8 @@ int seg_read_map(SEG * seg, char *input_map_name, int check_res,

/* check resolution equal any integer check; equal 0 no check */
if (check_res)
if (this_window.ew_res != cellhd.ew_res ||
this_window.ns_res != cellhd.ns_res)
if (fabs(this_window.ew_res - cellhd.ew_res) >= GRASS_EPSILON ||
fabs(this_window.ns_res - cellhd.ns_res) >= GRASS_EPSILON)
G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
"Run 'g.region raster=%s' to set proper region resolution."),
input_map_name, input_map_name);
Expand Down

0 comments on commit 4d72dba

Please sign in to comment.