tag:blogger.com,1999:blog-6066499303222625653.post5970292783381206464..comments2018-11-23T11:36:21.256+04:00Comments on John Khvatov's Blog: C++: compile time square root (sqrt) using templatesAnonymoushttp://www.blogger.com/profile/10987267584048000341noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-6066499303222625653.post-16764401071304813282013-05-29T17:49:21.883+04:002013-05-29T17:49:21.883+04:00And another thing for the record since I'm her...And another thing for the record since I'm here! Rather than asking "is mid * mid >= res?", I use "is mid >= res / mid?" Why? To avoid integer overflow from mid * mid.<br />Jeremyhttps://www.blogger.com/profile/01910912497083882333noreply@blogger.comtag:blogger.com,1999:blog-6066499303222625653.post-23249538891863630012013-05-20T18:02:50.418+04:002013-05-20T18:02:50.418+04:00Interesting. I guess your method instantiates les...Interesting. I guess your method instantiates less templates than the one in C++ Templates [1] because it is not using the IfThenElse template. Have you compared the performance? However, I'm sure you could improve it by storing the results rather than recalculating them? I.e.:<br /><br />template <br />class SQRT {<br />public:<br /> static const int mid = MID(r, l);<br /> static const bool gr = POW(mid) >= res;<br /> static const int value = SQRT::value;<br />};<br /><br />Cheers.<br /><br />[1] https://www.informit.com/articles/article.aspx?p=30667&seqNum=3<br />Jeremyhttps://www.blogger.com/profile/01910912497083882333noreply@blogger.com