<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[template-sinus]]></title><description><![CDATA[<p>Hallo.</p>
<p>In nem Buch bin ich grade drüber gestolpert, wie man sin/cos etc. zur Compilezeit ausrechnen kann. Das Beispiel anhand des sinus in dem Buch funktioniert natürlich nicht, da non-type Parameter int oder enums sein müssen.</p>
<pre><code class="language-cpp">// Serienbildung für sin( R ).
// Bei standardgemäßen Compilern bitte double R durch double&amp; R ersetzen
template&lt; double R &gt; struct Sinus
{
	enum {  MaxTerms = 10 } ; // Erhöhen für mehr Genauigkeit
	static inline double sin()
	{ 
		return R * Term&lt; R, 0, MaxTerms &gt;::val();
	} 
} ;

template&lt; double R, int I, int MaxTerms &gt;
struct Term
{
	enum
	{ 
		// Continue ist true, bis wir M Terme ausgewertet haben 
		Continue = I + 1 != MaxTerms,
		NxtI = ( I + 1 ) * Continue,
		NxtMaxTerms = MaxTerms * Continue
	} ;
	// Rekursive Definition, einmal pro Term aufzurufen
	static inline double val()
	{ 
		return 1 - R * R / (2.0 * I + 2.0) / 
			(2.0 * I + 3.0) * Term&lt; R * Continue, NxtI, 
			NxtMaxTerms &gt;::val();
	} 
} ;
// Spezialisierung, um die Schleife zu terminieren 
template &lt;&gt; struct Term&lt; 0.0, 0, 0 &gt;
{
	static inline double val() {  return 1.0; } 
} ;
</code></pre>
<p>Warum hat der Autor das im Buch? Ging das vor C++03 noch? (Das Buch ist aus dem Jahr 2000).</p>
<p>Das kann man doch bestimmt irgendwie umschreiben, dass es trotzdem noch hinhaut, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/291178/template-sinus</link><generator>RSS for Node</generator><lastBuildDate>Mon, 17 Aug 2026 21:25:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/291178.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 12 Aug 2011 09:53:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to template-sinus on Fri, 12 Aug 2011 09:53:25 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>In nem Buch bin ich grade drüber gestolpert, wie man sin/cos etc. zur Compilezeit ausrechnen kann. Das Beispiel anhand des sinus in dem Buch funktioniert natürlich nicht, da non-type Parameter int oder enums sein müssen.</p>
<pre><code class="language-cpp">// Serienbildung für sin( R ).
// Bei standardgemäßen Compilern bitte double R durch double&amp; R ersetzen
template&lt; double R &gt; struct Sinus
{
	enum {  MaxTerms = 10 } ; // Erhöhen für mehr Genauigkeit
	static inline double sin()
	{ 
		return R * Term&lt; R, 0, MaxTerms &gt;::val();
	} 
} ;

template&lt; double R, int I, int MaxTerms &gt;
struct Term
{
	enum
	{ 
		// Continue ist true, bis wir M Terme ausgewertet haben 
		Continue = I + 1 != MaxTerms,
		NxtI = ( I + 1 ) * Continue,
		NxtMaxTerms = MaxTerms * Continue
	} ;
	// Rekursive Definition, einmal pro Term aufzurufen
	static inline double val()
	{ 
		return 1 - R * R / (2.0 * I + 2.0) / 
			(2.0 * I + 3.0) * Term&lt; R * Continue, NxtI, 
			NxtMaxTerms &gt;::val();
	} 
} ;
// Spezialisierung, um die Schleife zu terminieren 
template &lt;&gt; struct Term&lt; 0.0, 0, 0 &gt;
{
	static inline double val() {  return 1.0; } 
} ;
</code></pre>
<p>Warum hat der Autor das im Buch? Ging das vor C++03 noch? (Das Buch ist aus dem Jahr 2000).</p>
<p>Das kann man doch bestimmt irgendwie umschreiben, dass es trotzdem noch hinhaut, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2105261</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2105261</guid><dc:creator><![CDATA[templatesinus]]></dc:creator><pubDate>Fri, 12 Aug 2011 09:53:25 GMT</pubDate></item><item><title><![CDATA[Reply to template-sinus on Fri, 12 Aug 2011 09:57:35 GMT]]></title><description><![CDATA[<p>Die Ersetung von double zu double&amp; macht was ganz anderes, als man sich erhofft.<br />
Keine Ahnung, weshalb der Autor das geschrieben hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2105265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2105265</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 12 Aug 2011 09:57:35 GMT</pubDate></item><item><title><![CDATA[Reply to template-sinus on Fri, 12 Aug 2011 10:29:03 GMT]]></title><description><![CDATA[<p>Falls dies keine rein akademische Übung ist: So etwas zur Compilezeit auszurechnen, ist eine grundlegende Compileroptimierung von der man immer erwarten darf, dass sie gemacht wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2105283</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2105283</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 12 Aug 2011 10:29:03 GMT</pubDate></item></channel></rss>