<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 20, 2014 at 9:45 PM, 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br>
Anna Petrášová wrote:<br>
<br>
> I was wondering, if r.profile shouldn't use atan2<br>
> instead of atan? It seems that the division in atan could possibly lead to<br>
> overflow? But nobody complained about it yet... Tests work with atan2.<br>
<br>
</span>Using atan2() shouldn't be necessary, as each quadrant is handled<br>
separately. However, the due south case is problematic:<br>
<br>
            if (rows > 0 && cols >= 0) {<br>
                /* SW Quad or due south */<br>
                AZI = atan((rows / cols));<br>
<br>
as "cols" could equal zero.<br>
<br>
Floating-point division by zero won't result in overflow, it will<br>
result in infinity (which is representable). The problem is that it's<br>
not clear whether the argument to atan() will be positive infinity<br>
(resulting in AZI=pi/2) or negative infinity (resulting in AZI=-pi/2).<br>
<br>
If cols is -0.0, the argument will (typically) be negative infinity<br>
(IIRC, C implementations aren't required to preserve the sign of a<br>
zero).<br>
<br>
Given that AZI is only used for calculating X and Y, I would suggest<br>
using neither atan() nor atan2() but simply normalising the vector,<br>
e.g.<br>
<br>
        double k = res / hypot(rows, cols);<br>
        double X = k * rows;<br>
        double Y = k * cols;<br></blockquote><div><br></div><div>Thanks for the suggestion, I committed it in 62327. It passed my tests.</div><div><br></div><div>Anna</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Note that this doesn't need a separate case for each quadrant.<br>
<br>
Technically, we shouldn't be using hypot() as it isn't part of C89 (it<br>
was added in C99). But it's already being used in:<br>
<br>
        lib/vector/diglib/prune.c<br>
        lib/vector/Vlib/line.c<br>
        lib/vector/Vlib/poly.c<br>
        lib/gmath/la.c<br>
        lib/gis/distance.c<br>
<span class=""><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></div>