Re: [GRASS-user] compare a DCELL and FCELL question

ming han <[email protected]> Sun, 24 Jan 2021 11:05:52 -0500
Newsgroups gmane.comp.gis.grass.devel,gmane.comp.gis.grass.user
Message-ID <CAD+uAWgGZn8Mwe5-Lm1JbiytjKPQ4ij-oJS+0nxJbgQTdCXasA@mail.gmail.com>
--===============3945467212368857231==
Content-Type: multipart/alternative; boundary="00000000000027ac8105b9a799e8"

--00000000000027ac8105b9a799e8
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi Everyone

   Many thanks for your help. Is if(fabs(map_A - map_B) <=3D 1.0e-15, ... )
approach may increase the runtime compare to '=3D=3D' way?

Thanks
Ming

Markus Metz <[email protected]> =E4=BA=8E2021=E5=B9=B41=E6=9C=
=8824=E6=97=A5=E5=91=A8=E6=97=A5 =E4=B8=8A=E5=8D=8810:57=E5=86=99=E9=81=93=
=EF=BC=9A

> Trying to answer the original question: with a DCELL map "cat1_acc_riv"
> and a FCELL map "cat1_minacc", why is "float(cat1_acc_riv) =3D=3D
> float(cat1_minacc)" not equal to "int(cat1_acc_riv) =3D=3D int(cat1_minac=
c)" ?
>
> int truncates to integer while float converts to single precision floatin=
g
> point. E.g. with cat1_acc_riv =3D 1.1 and cat1_minacc =3D 1.9,
> "float(cat1_acc_riv) =3D=3D float(cat1_minacc)" becomes "1.1 =3D=3D 1.9" =
whereas
> "int(cat1_acc_riv) =3D=3D int(cat1_minacc)" becomes "1 =3D=3D 1", thus th=
e results
> are different.
>
> Another reason for possible differences is that float can only represent
> max 7 decimal digits. E.g. float(194320567) becomes 194320560 but
> int(194320567) preserves the value 194320567.
>
> Thus the safest is to cast everything to the type with the highest
> precision. In this case with FCELL and DCELL, use "double(cat1_acc_riv) =
=3D=3D
> double(cat1_minacc)" or even better the suggestion of Markus N.
>
> Markus M
>
>
> On Sun, Jan 24, 2021 at 3:51 PM ming han <[email protected]> wrote:
> >
> > Hi Markus and Micha
> >
> >      I am just trying to find grids have the same values in these two
> rasters, I will try the threshold approach.
> >
> > Thanks
> > Ming
> >
> > Markus Neteler <[email protected]> =E4=BA=8E2021=E5=B9=B41=E6=9C=8824=
=E6=97=A5=E5=91=A8=E6=97=A5 =E4=B8=8A=E5=8D=886:58=E5=86=99=E9=81=93=EF=BC=
=9A
> >>
> >> Hi Ming,
> >>
> >> On Sun, Jan 24, 2021 at 10:49 AM ming han <[email protected]> wrote:
> >> >
> >> > Hi Micha
> >> >
> >> >      Many thanks for your reply.
> >> >      Here is the command I am using:
> >> >
> >> >      if(float(cat1_acc_riv) =3D=3D float(cat1_minacc), str_r, null()=
)
> >> >
> >> >       The str_r is a CELL raster. the result is different when I
> change it to:
> >> >        if(int(cat1_acc_riv) =3D=3D int(cat1_minacc), str_r, null())
> >>
> >> Note that numerical "equality" is better tested with a threshold test
> >> against the map pixel difference.
> >> As the threshold, we use GRASS_EPSILON which is defined as 1.0e-15.
> >>
> >> Hence the test needs to be implemented in a different way, i.e. by
> >> using an epsilon.
> >> Essentially something like this:
> >>
> >> if(fabs(map_A - map_B) <=3D 1.0e-15, ... )
> >>
> >> In your case (untested):
> >> r.mapcalc diffepsilon =3D if( abs( map_A - map_B) <=3D 1.0e-15, str_r =
,
> null())
> >>
> >> See related discussions here: [1], [2] and elsewhere.
> >>
> >> [1] Comment by Glynn:
> https://trac.osgeo.org/grass/ticket/2854#comment:9
> >> [2] Comment by Glynn:
> >> https://lists.osgeo.org/pipermail/grass-user/2015-October/073200.html
> >>
> >> Best,
> >> Markus
> >
> > _______________________________________________
> > grass-dev mailing list
> > [email protected]
> > https://lists.osgeo.org/mailman/listinfo/grass-dev
>

--00000000000027ac8105b9a799e8
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi Everyone=C2=A0<div><br></div><div>=C2=A0 =C2=A0Many tha=
nks for your help. Is if(fabs(map_A - map_B) &lt;=3D 1.0e-15, ... ) approac=
h=C2=A0may increase the runtime compare to &#39;=3D=3D&#39; way?=C2=A0</div=
><div><br></div><div>Thanks</div><div>Ming=C2=A0</div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">Markus Metz &lt;<a h=
ref=3D"mailto:[email protected]">[email protected]<=
/a>&gt; =E4=BA=8E2021=E5=B9=B41=E6=9C=8824=E6=97=A5=E5=91=A8=E6=97=A5 =E4=
=B8=8A=E5=8D=8810:57=E5=86=99=E9=81=93=EF=BC=9A<br></div><blockquote class=
=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rg=
b(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div>Trying to answer the=
 original question: with a DCELL map &quot;cat1_acc_riv&quot; and a FCELL m=
ap &quot;cat1_minacc&quot;, why is &quot;float(cat1_acc_riv) =3D=3D float(c=
at1_minacc)&quot; not equal to &quot;int(cat1_acc_riv) =3D=3D int(cat1_mina=
cc)&quot; ?<br></div><div><br></div><div>int truncates to integer while flo=
at converts to single precision floating point. E.g. with cat1_acc_riv =3D =
1.1 and cat1_minacc =3D 1.9, &quot;float(cat1_acc_riv) =3D=3D float(cat1_mi=
nacc)&quot; becomes &quot;1.1 =3D=3D 1.9&quot; whereas &quot;int(cat1_acc_r=
iv) =3D=3D int(cat1_minacc)&quot; becomes &quot;1 =3D=3D 1&quot;, thus the =
results are different.</div><div><br></div><div>Another reason for possible=
 differences is that float can only represent max 7 decimal digits. E.g. fl=
oat(194320567) becomes 194320560 but int(194320567) preserves the value 194=
320567.</div><div><br></div><div>Thus the safest is to cast everything to t=
he type with the highest precision. In this case with FCELL and DCELL, use =
&quot;double(cat1_acc_riv) =3D=3D double(cat1_minacc)&quot; or even better =
the suggestion of Markus N.</div><div><br></div><div>Markus M<br></div><div=
><br></div><div><br>On Sun, Jan 24, 2021 at 3:51 PM ming han &lt;<a href=3D=
"mailto:[email protected]" target=3D"_blank">[email protected]</a>&gt; wr=
ote:<br>&gt;<br>&gt; Hi Markus and Micha<br>&gt;<br>&gt; =C2=A0 =C2=A0 =C2=
=A0I am just trying to find grids have the same values in these two rasters=
, I will try the threshold approach.<br>&gt;<br>&gt; Thanks<br>&gt; Ming <b=
r>&gt;<br>&gt; Markus Neteler &lt;<a href=3D"mailto:[email protected]" targ=
et=3D"_blank">[email protected]</a>&gt; =E4=BA=8E2021=E5=B9=B41=E6=9C=8824=
=E6=97=A5=E5=91=A8=E6=97=A5 =E4=B8=8A=E5=8D=886:58=E5=86=99=E9=81=93=EF=BC=
=9A<br>&gt;&gt;<br>&gt;&gt; Hi Ming,<br>&gt;&gt;<br>&gt;&gt; On Sun, Jan 24=
, 2021 at 10:49 AM ming han &lt;<a href=3D"mailto:[email protected]" targe=
t=3D"_blank">[email protected]</a>&gt; wrote:<br>&gt;&gt; &gt;<br>&gt;&gt;=
 &gt; Hi Micha<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; =C2=A0 =C2=A0 =C2=A0Many t=
hanks for your reply.<br>&gt;&gt; &gt; =C2=A0 =C2=A0 =C2=A0Here is the comm=
and I am using:<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; =C2=A0 =C2=A0 =C2=A0if(fl=
oat(cat1_acc_riv) =3D=3D float(cat1_minacc), str_r, null())<br>&gt;&gt; &gt=
;<br>&gt;&gt; &gt; =C2=A0 =C2=A0 =C2=A0 The str_r is a CELL raster. the res=
ult is different when I change it to:<br>&gt;&gt; &gt; =C2=A0 =C2=A0 =C2=A0=
 =C2=A0if(int(cat1_acc_riv) =3D=3D int(cat1_minacc), str_r, null())<br>&gt;=
&gt;<br>&gt;&gt; Note that numerical &quot;equality&quot; is better tested =
with a threshold test<br>&gt;&gt; against the map pixel difference.<br>&gt;=
&gt; As the threshold, we use GRASS_EPSILON which is defined as 1.0e-15.<br=
>&gt;&gt;<br>&gt;&gt; Hence the test needs to be implemented in a different=
 way, i.e. by<br>&gt;&gt; using an epsilon.<br>&gt;&gt; Essentially somethi=
ng like this:<br>&gt;&gt;<br>&gt;&gt; if(fabs(map_A - map_B) &lt;=3D 1.0e-1=
5, ... )<br>&gt;&gt;<br>&gt;&gt; In your case (untested):<br>&gt;&gt; r.map=
calc diffepsilon =3D if( abs( map_A - map_B) &lt;=3D 1.0e-15, str_r , null(=
))<br>&gt;&gt;<br>&gt;&gt; See related discussions here: [1], [2] and elsew=
here.<br>&gt;&gt;<br>&gt;&gt; [1] Comment by Glynn: <a href=3D"https://trac=
.osgeo.org/grass/ticket/2854#comment:9" target=3D"_blank">https://trac.osge=
o.org/grass/ticket/2854#comment:9</a><br>&gt;&gt; [2] Comment by Glynn:<br>=
&gt;&gt; <a href=3D"https://lists.osgeo.org/pipermail/grass-user/2015-Octob=
er/073200.html" target=3D"_blank">https://lists.osgeo.org/pipermail/grass-u=
ser/2015-October/073200.html</a><br>&gt;&gt;<br>&gt;&gt; Best,<br>&gt;&gt; =
Markus<br>&gt;<br>&gt; _______________________________________________<br>&=
gt; grass-dev mailing list<br>&gt; <a href=3D"mailto:[email protected].=
org" target=3D"_blank">[email protected]</a><br>&gt; <a href=3D"htt=
ps://lists.osgeo.org/mailman/listinfo/grass-dev" target=3D"_blank">https://=
lists.osgeo.org/mailman/listinfo/grass-dev</a></div></div>
</blockquote></div>

--00000000000027ac8105b9a799e8--

--===============3945467212368857231==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
grass-dev mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/grass-dev

--===============3945467212368857231==--