<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 2 May 2017, at 07:44, Pavel Afonine &lt;<a href="mailto:pafonine@lbl.gov" class="">pafonine@lbl.gov</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">If memory serves norm of a zero vector is zero, isn't it? So naively I'd expect that function to return zero, not fail in obscure way..<br class=""><br class="">Broadly speaking, norm is length. Length of a zero vector is zero.<br class=""><br class="">So my proposal would be to treat zero vectors as special case and do the right thing for it.<br class=""></div></div></blockquote></div><br class=""></div><div class="">If the current implementation</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class=""><span style="font-size: 12px;" class="">&nbsp; &nbsp; &nbsp; vec3 normalize() const</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 12px;" class="">&nbsp; &nbsp; &nbsp; {</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 12px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; return (*this) / length();</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 12px;" class="">&nbsp; &nbsp; &nbsp; }</span></font></div></div><div class=""><br class=""></div><div class="">was to be replaced by the following, as Pavel proposed if I understand correctly,</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><span style="font-size: 12px;" class="">if(length() == 0) raise scitbx::error(“a zero vector cannot be normalised”);</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 12px;" class="">return (*this)/length();</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 12px;" class=""><br class=""></span></font></div><div class="">this may not result in any performance hit in fact. Indeed the compiler would still be allowed to inline <font face="Menlo" class=""><span style="font-size: 12px;" class="">normalize</span></font>&nbsp;and then the processor branch prediction unit may realise the branch throwing the exception is hardly ever taken, and take advantage of that to reorder calculations to, in effect, make the code equivalent to the current implementation. But this is the point: the current implementation is guaranteed to be optimal whether the rewrite may be optimal. So if this function appears in a critical path, you would need to check there is no performance hit.</div><div class=""><br class=""></div><div class="">Best wishes,</div><div class=""><br class=""></div><div class="">Luc</div><div class=""><br class=""></div></body></html>