<div dir="ltr"><div>Hi Glynn<br><br></div>Thanks for the explanation. Good to know that the problem lies with the exceeding the integer range.. something I have to be careful about in the future.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 25, 2014 at 1:04 AM, Glynn Clements <span dir="ltr"><<a href="mailto:glynn@gclements.plus.com" target="_blank">glynn@gclements.plus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
Paulo van Breugel wrote:<br>
<br>
> I have the following function (to calculate cell area)<br>
><br>
> PI=<a href="tel:3.1415926536" value="+31415926536">3.1415926536</a><br>
> RES=0.0008333<br>
> R=6371007<br>
> r.mapcalc "test3 = (sin(y()+$RES/2) - sin(y()-$RES/2)) * ($RES * $PI/180) *<br>
> $R^2"<br>
><br>
> This will give me a wrong results, apparently because R is an integer<br>
> value. If I adapt the formula:<br>
><br>
> r.mapcalc "test3 = (sin(y()+$RES/2) - sin(y()-$RES/2)) * ($RES * $PI/180) *<br>
> float($R)^2"<br>
><br>
> I get the correct result. From the help file I understand that if any of<br>
> the arguments is a floating number the result should be a floating number.<br>
> But that does not seem the case above so apparently I am missing something<br>
> here. Can anybody explain the logic behind the above?<br>
<br>
</div></div>In the first case, 6371007^2 is calculated using integer arithmetic,<br>
and the result (40589730194049) will exceed the range of an "int"<br>
(2^31-1 = 2147483647), typically resulting in overflow (R^2 will<br>
probably equal -2005720447). The value will be converted to "double"<br>
for the multiplication, but by that point the error has already<br>
occurred.<br>
<br>
In the second case, R is converted to "float" (single-precision<br>
floating-point), and the result of squaring it is well within the<br>
range of a "float", although it will be rounded to 40589731037184.0<br>
(using double() instead of float() will use double-precision and avoid<br>
the rounding error).<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Glynn Clements <<a href="mailto:glynn@gclements.plus.com">glynn@gclements.plus.com</a>><br>
</font></span></blockquote></div><br></div>