<?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[Sin(x) ohne float]]></title><description><![CDATA[<p>Hi, ich bin gerade auf der suche nach einer sinusfunktion die ohne floats auskommt.</p>
<p>z.B.</p>
<pre><code class="language-cpp">// input: [0, 6283] ?
// output: [-1000, 1000]
long long mysine(long long val)
{
    return ...;
}
</code></pre>
<p>gibts sowas?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/243708/sin-x-ohne-float</link><generator>RSS for Node</generator><lastBuildDate>Sat, 19 Sep 2026 21:11:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/243708.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 20 Jun 2009 09:16:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 09:16:33 GMT]]></title><description><![CDATA[<p>Hi, ich bin gerade auf der suche nach einer sinusfunktion die ohne floats auskommt.</p>
<p>z.B.</p>
<pre><code class="language-cpp">// input: [0, 6283] ?
// output: [-1000, 1000]
long long mysine(long long val)
{
    return ...;
}
</code></pre>
<p>gibts sowas?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1729943</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1729943</guid><dc:creator><![CDATA[foofel]]></dc:creator><pubDate>Sat, 20 Jun 2009 09:16:33 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 09:50:08 GMT]]></title><description><![CDATA[<p>nimm die normale sinusfunktion und multiplizier den wert der rauskommt mit 1000 und speicher den in deinem long long ab.. ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1729956</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1729956</guid><dc:creator><![CDATA[blubdeldiblubblub]]></dc:creator><pubDate>Sat, 20 Jun 2009 09:50:08 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 09:52:20 GMT]]></title><description><![CDATA[<p>hab keine normale sin funktion</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1729958</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1729958</guid><dc:creator><![CDATA[foofel]]></dc:creator><pubDate>Sat, 20 Jun 2009 09:52:20 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 10:09:59 GMT]]></title><description><![CDATA[<p>foofel schrieb:</p>
<blockquote>
<p>hab keine normale sin funktion</p>
</blockquote>
<p>Du wirst ja wohl einen Taschenrechner besitzen, mit dem du diese Tabelle anlegen kannst. Oder eine Tabellenkalkulation.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1729966</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1729966</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sat, 20 Jun 2009 10:09:59 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 10:19:49 GMT]]></title><description><![CDATA[<p>foofel schrieb:</p>
<blockquote>
<p>Hi, ich bin gerade auf der suche nach einer sinusfunktion die ohne floats auskommt.</p>
<p>z.B.</p>
<pre><code class="language-cpp">// input: [0, 6283] ?
// output: [-1000, 1000]
long long mysine(long long val)
{
    return ...;
}
</code></pre>
<p>gibts sowas?</p>
</blockquote>
<p>Bestimmt. Im Notfall selber bauen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Welchem Winkel entspricht denn die Zahl 6283? Außerdem würde ich nicht so'ne krumme Zahl wie 1000 zum Skalieren benutzen. Eher 1024 oder so. Wenn Du dann nämlich a*sin(b1) berechnen willst, schreibst Du einfach (a*mysine(b2)+512)&gt;&gt;10, wobei b2 der zu b1 gehörige Winkel in Deiner Einheit ist. (Das funktioniert natürlich nur, wenn vorzeichenbehaftete Ganzzahlen im Zweierkomplement gespeichert werden.)</p>
<p>Wieviel Speicher (für 'nen Lookup-Table) darfst Du denn verbraten? Wenn Du Platz für 6284 Ganzzahlen im Speicher hast, dann ist das die einfachste und beste Lösung.</p>
<p>Wenn Du genug RAM hast, aber kaum ROM, kannst Du so eine Tabelle relativ leicht zur Laufzeit vorberechnen. Das geht zur Not auch ohne Fließkommazahlen.</p>
<p>Ansonnsten müsstest Du den Definitionsberich &quot;verkleinern&quot; (Du brauchst nur den Bereich 0...pi/2 und kannst den Rest spiegeln), in Abschnitte aufteilen und per Abschnitt zB eine lineare Approximation nutzen.</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1729971</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1729971</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sat, 20 Jun 2009 10:19:49 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 11:49:21 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;vector&gt;
#include &lt;cmath&gt;

std::vector&lt;long&gt; dreck() 
{
	std::vector&lt;long&gt; ig(6283+1);
	for(long l = 0;l&lt;=6283; l++) 
	{
		ig[l]=static_cast&lt;long&gt;((sin(0.001f*l)*1000) + 0.5f);
	}
	return ig;
}

long mysine(long val)
{
	static const std::vector&lt;long&gt; harry = dreck();
    return harry[val];
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1730015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730015</guid><dc:creator><![CDATA[harryDreckig]]></dc:creator><pubDate>Sat, 20 Jun 2009 11:49:21 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 12:23:31 GMT]]></title><description><![CDATA[<p>harryDreckig schrieb:</p>
<blockquote>
<pre><code class="language-cpp">#include &lt;vector&gt;
#include &lt;cmath&gt;

std::vector&lt;long&gt; dreck() 
{
	std::vector&lt;long&gt; ig(6283+1);
	for(long l = 0;l&lt;=6283; l++) 
	{
		ig[l]=static_cast&lt;long&gt;((sin(0.001f*l)*1000) + 0.5f);
	}
	return ig;
}

long mysine(long val)
{
	static const std::vector&lt;long&gt; harry = dreck();
    return harry[val];
}
</code></pre>
</blockquote>
<p>Nix sin da! (Ausser du wolltest auch drauf hinweisen es vorher zu berechnen.)</p>
<p>Sebastian Pizer schrieb:</p>
<blockquote>
<p>foofel schrieb:</p>
<blockquote>
<p>Hi, ich bin gerade auf der suche nach einer sinusfunktion die ohne floats auskommt.</p>
<p>z.B.</p>
<pre><code class="language-cpp">// input: [0, 6283] ?
// output: [-1000, 1000]
long long mysine(long long val)
{
    return ...;
}
</code></pre>
<p>gibts sowas?</p>
</blockquote>
<p>Bestimmt. Im Notfall selber bauen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Welchem Winkel entspricht denn die Zahl 6283? Außerdem würde ich nicht so'ne krumme Zahl wie 1000 zum Skalieren benutzen. Eher 1024 oder so. Wenn Du dann nämlich a*sin(b1) berechnen willst, schreibst Du einfach (a*mysine(b2)+512)&gt;&gt;10, wobei b2 der zu b1 gehörige Winkel in Deiner Einheit ist. (Das funktioniert natürlich nur, wenn vorzeichenbehaftete Ganzzahlen im Zweierkomplement gespeichert werden.)</p>
<p>Wieviel Speicher (für 'nen Lookup-Table) darfst Du denn verbraten? Wenn Du Platz für 6284 Ganzzahlen im Speicher hast, dann ist das die einfachste und beste Lösung.</p>
<p>Wenn Du genug RAM hast, aber kaum ROM, kannst Du so eine Tabelle relativ leicht zur Laufzeit vorberechnen. Das geht zur Not auch ohne Fließkommazahlen.</p>
<p>Ansonnsten müsstest Du den Definitionsberich &quot;verkleinern&quot; (Du brauchst nur den Bereich 0...pi/2 und kannst den Rest spiegeln), in Abschnitte aufteilen und per Abschnitt zB eine lineare Approximation nutzen.</p>
<p>Gruß,<br />
SP</p>
</blockquote>
<p>6283 ist 2PI * 1000, also einfach einmal rum. Die Werte vorher zu berechnen wäre vielleicht ne möglichkeit aber ich hab nur 20kb sram + 128kb flash, das würde schon einiges verbraten. Es muss auch garnicht soo schnell sein. *g*</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730031</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730031</guid><dc:creator><![CDATA[foofel]]></dc:creator><pubDate>Sat, 20 Jun 2009 12:23:31 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 13:09:56 GMT]]></title><description><![CDATA[<p>foofel schrieb:</p>
<blockquote>
<p>6283 ist 2PI * 1000, also einfach einmal rum. Die Werte vorher zu berechnen wäre vielleicht ne möglichkeit aber ich hab nur 20kb sram + 128kb flash, das würde schon einiges verbraten. Es muss auch garnicht soo schnell sein.</p>
</blockquote>
<p>Wie gesagt. 6283 für 2pi und 1000 für 1.0 ist eventuell nicht die geschickteste Lösung. Ansonnsten bist Du schon startklar: Kleine Tabelle vorberechnen und linear interpolieren</p>
<pre><code class="language-cpp">// sin_table[x] = round(4096*sin(x*pi/32))
// für x=0...16
static const int sin_table[17] = {
  0,401,799,1189,1567,1931,2276,2598,2896
  3166,3406,3612,3784,3920,4017,4076,4096
};

// Gibt Approximation für round(4096*sin(angle*pi/4096))
// zurueck. (&quot;8192 = 360°&quot;)
int my_sine(int angle)
{
  // Reduktion von (-oo,+oo) auf [0°,360°)
  angle &amp;= 8192-1;

  // Reduktion von [0°,360°) auf [0°,180°)
  int sign = 1 - 2*((angle &gt;&gt; 12) &amp; 1);
  angle &amp;= 4096-1;

  // Reduktion von [0°,180°) auf [0°,90°]
  if (angle&gt;2048) angle=4096-angle;

  return sign * interpolate(sin_table,angle/128.0);
}
</code></pre>
<p>wobei Du für <code>interpolate(sin_table,angle/128.0)</code> jetzt noch selbst dein Hirn etwas anstrengen musst. Ich denke, es ist klar, was gemeint ist.</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730056</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730056</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sat, 20 Jun 2009 13:09:56 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 13:21:13 GMT]]></title><description><![CDATA[<p>foofel schrieb:</p>
<blockquote>
<p>6283 ist 2PI * 1000, also einfach einmal rum. Die Werte vorher zu berechnen wäre vielleicht ne möglichkeit aber ich hab nur 20kb sram + 128kb flash, das würde schon einiges verbraten. Es muss auch garnicht soo schnell sein. *g*</p>
</blockquote>
<p>dann würde ich erstmal falten.<br />
sin(x)=<br />
if(x&gt;3142) return -sin(x-3142);<br />
else if(x&gt;3142/2) return sin(3142-x);<br />
else return kleinsin(x);</p>
<p>kleinsin(x) kannste dann mit der tabelle machen, und die muß nur von 0 bis 1571 gehen.<br />
oder noch besser, jetzt machst du für kleinsin(x) einfach eine näherungsformel, tum beispiel tailor-reihe.</p>
<p>kleinsin(x)=x-x*x/2000*x/3000; //bis zu 7% abweichung<br />
oder<br />
kleinsin(x)=x-x*x/2000*x/3000+x*x/2000*x/3000*x/4000*x/5000; //bis zu 0.4% abweichung</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730063</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730063</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 20 Jun 2009 13:21:13 GMT</pubDate></item><item><title><![CDATA[Reply to Sin(x) ohne float on Sat, 20 Jun 2009 13:52:00 GMT]]></title><description><![CDATA[<p>1. warum nehmt ihr alle long (long) oder so, wenn ihr eh nur nen wertebereich braucht, den so gar short schon abdeckt?! Oo</p>
<p>2. sollten 20kb (ich nehm mal an byte - bit wäre ne komische größe ^^) ja wohl reichen 6283/2 shorts zu speichern... sollten ziemlich genau 6kB sein...</p>
<p>3. warum c++ forum, wenn du offensichtlich keine standard-lib zur verfügung hast? oder hast du nur den math header nicht oder was?</p>
<p>also so in etwa:</p>
<pre><code class="language-cpp">struct _sinus_detail
{
private:
 short *cache;
 const static short max_index = 6283;

 short get_index(unsigned short val)
 {
   short size = max_index+1; //gesamtlänge
   short limit = size&lt;&lt;1; //die hälfte

   if(val &gt; limit)
      val = size-val;

   return val;
 }
public:
 _sinus_detail()
 {
   cache = new short[max_index];
   //entweder die werte hier fest reinschreiben oder nen algorithmus suchen - siehe dazu unten
 }

 short sin(unsigned short val)
 {
   short index = get_index(val);
   if(index == val)
     return cache[index];
   else
     return -cache[index];
 }

 ~_sinus_detail()
 {
   delete cache;
 }
};
static _sinus_detail math;

int main()
{
 short a = math.sin(1234);
 short b = math.sin(4321);
}
</code></pre>
<p>kann sein, du musst das alginment noch deaktivieren, weil er sonst pro short trotzdem 4B verbrät - musst du selbst ma gucken...</p>
<p>Algorithmen für Sinus-Berechnungen:<br />
<a href="http://de.wikipedia.org/wiki/Sinus_und_Kosinus#Definition_als_Taylorreihe" rel="nofollow">http://de.wikipedia.org/wiki/Sinus_und_Kosinus#Definition_als_Taylorreihe</a></p>
<p>bb</p>
<p>edit:<br />
1. da es dir nicht auf die geschwindigkeit ankommt, wie mir gerade aufgefallen ist (lesen ist nich so meins^^), brauchst du eigtl gar nichts vorberechnen<br />
2. kann man das ganze natürlich noch mal zusammenklappen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730065</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730065</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sat, 20 Jun 2009 13:52:00 GMT</pubDate></item></channel></rss>