<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Function returning double parameter</title>
		<link>http://www.allegro.cc/forums/view/588213</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 27 Oct 2006 22:43:44 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello, I got th following problem: a function is returning a value from type &#39;double&#39;, but the caller receives the wrong value. In the function&#39;s body all is correct, but the return value does not match. The prototype is as follows:<br /><span class="source-code"><span class="k1">double</span> grad2rad<span class="k2">(</span> <span class="k1">int</span> grad_angle <span class="k2">)</span><span class="k2">;</span></span><br />and the body is:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> grad2rad<span class="k2">(</span> <span class="k1">int</span> grad_angle <span class="k2">)</span><span class="k2">{</span>
  <span class="k1">double</span> new_angle<span class="k2">;</span>
  new_angle <span class="k3">=</span> <span class="k2">(</span>grad_angle<span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">)</span><span class="k3">*</span>PI<span class="k2">;</span>
  <span class="k1">return</span> new_angle<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
PI is a constant: (22.0/7.0). Will be thankful for any help.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thats  me)</author>
		<pubDate>Thu, 26 Oct 2006 00:39:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t see anything wrong. What you expect and what you get? Remember there may be rounding errors.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Thu, 26 Oct 2006 00:51:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
PI is a constant: (22.0/7.0)
</p></div></div><p>
What&#39;s wrong with M_PI? Your code should look like this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> grad2rad<span class="k2">(</span> <span class="k1">int</span> grad_angle <span class="k2">)</span><span class="k2">{</span>
   <span class="k1">return</span> <span class="k2">(</span><span class="k1">double</span><span class="k2">)</span>grad_angle<span class="k3">*</span>M_PI<span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
M_PI lives in &lt;math.h&gt; by the way...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Thu, 26 Oct 2006 00:56:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Try this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> grad2rad<span class="k2">(</span> <span class="k1">int</span> grad_angle <span class="k2">)</span><span class="k2">{</span>
  <span class="k1">double</span> new_angle<span class="k2">;</span>
  new_angle <span class="k3">=</span> <span class="k2">(</span>PI <span class="k3">*</span> grad_angle<span class="k2">)</span><span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">;</span>
  <span class="k1">return</span> new_angle<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

And if still doesn&#39;t works, then try this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> grad2rad<span class="k2">(</span> <span class="k1">int</span> grad_angle <span class="k2">)</span><span class="k2">{</span>
  <span class="k1">double</span> new_angle<span class="k2">;</span>
  new_angle <span class="k3">=</span> <span class="k2">(</span>PI <span class="k3">*</span> <span class="k2">(</span><span class="k2">(</span><span class="k1">double</span><span class="k2">)</span> grad_angle<span class="k2">)</span><span class="k2">)</span><span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">;</span>
  <span class="k1">return</span> new_angle<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Edit: Or simpliest, but equivalent to that:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> grad2rad<span class="k2">(</span> <span class="k1">int</span> grad_angle <span class="k2">)</span><span class="k2">{</span>
  <span class="k1">return</span> <span class="k2">(</span>PI <span class="k3">*</span> <span class="k2">(</span><span class="k2">(</span><span class="k1">double</span><span class="k2">)</span> grad_angle<span class="k2">)</span><span class="k2">)</span><span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Victor Williams Stafusa da Silva)</author>
		<pubDate>Thu, 26 Oct 2006 01:03:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you people, but the problem is not in the calculation, but in the data transfer (that&#39;s the reason I assign the value to a variable instead of just returning it). Before I return the variable, it has the right value, but afterwards not any more... <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /></p><p>By the way, the result should be 1.570796 and the caller actually receives 1227133513.000000... <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thats  me)</author>
		<pubDate>Thu, 26 Oct 2006 01:47:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Are you using malloc or arrays somewhere? That sounds like memory corruption.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Thu, 26 Oct 2006 01:48:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Show the &quot;caller&quot;&#39;s code then.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Epsi)</author>
		<pubDate>Thu, 26 Oct 2006 01:58:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Are you using malloc or arrays somewhere? That sounds like memory corruption.
</p></div></div><p>
Actually no. The problem was elsewhere...<br />The declaration of the function is in external header file and I forgot to include the corresponding header. The compiler didn&#39;t complain at all and I suspect it thought the  return value is by default int or something like that, so it interpreted the data wrong. Strange stuff... Hopefully my failure helps someone else, who is experiencing the same problem.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thats  me)</author>
		<pubDate>Thu, 26 Oct 2006 01:59:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>  you are right,in your situation the compiler will recognize the return value of <br />this function as int by default.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (stone830209)</author>
		<pubDate>Thu, 26 Oct 2006 06:03:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">miran said:</div><div class="quote"><p>
What&#39;s wrong with M_PI? Your code should look like this:</p><p>double grad2rad( int grad_angle ){<br />   return (double)grad_angle*M_PI/180.0;<br />}</p><p>M_PI lives in &lt;math.h&gt; by the way...
</p></div></div><p>
<i>Normally</i> lives in math.h, one should say. IIRC, M_PI is neither C89 nor C99. It might be POSIX, but I don&#39;t think MSVC supports it.</p><p>But either way, using 22/7 instead of a sensible value for pi is just silly. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Thu, 26 Oct 2006 12:18:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
but I don&#39;t think MSVC supports it
</p></div></div><p>
It does (math.h, MSVC 7.1).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Thu, 26 Oct 2006 12:54:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
PI is a constant: (22.0/7.0)
</p></div></div><p>
That is a <i>horrible</i> approximation.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The compiler didn&#39;t complain at all
</p></div></div><p>
It does if you enable warnings (which is <i>always</i> a good idea).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 26 Oct 2006 14:27:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That is a horrible approximation.
</p></div></div><p>
Well, at least it&#39;s better than 3. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Thu, 26 Oct 2006 14:41:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I usually just use 3.142, no need to get more precise.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FMC)</author>
		<pubDate>Thu, 26 Oct 2006 16:27:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You people make mathematicians and your math teachers cry. Like this: <img src="http://www.allegro.cc/forums/smileys/cry.gif" alt=":&#39;(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Corelian)</author>
		<pubDate>Thu, 26 Oct 2006 16:35:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It depends on what you are doing, in computer games it&#39;s a waste to use many significant figures (unless, maybe, in some kind of advanced physics simulation), usually one or two are more than enough...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FMC)</author>
		<pubDate>Thu, 26 Oct 2006 16:51:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s exactly that what Corelian was referring to... </p><p>And before you talk about performance, do some profiling to find out, where your program does spend its time. You will be surprised!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Thu, 26 Oct 2006 16:55:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
in computer games it&#39;s a waste to use many significant figures
</p></div></div><p>
How? It is just as fast to use more correct digits as it is to use the same number of incorrect ones. What are you wasting?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 26 Oct 2006 16:58:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Show me the advantage of using 10 instead of 3 significant figures in the average game... anyway using, for example, floats (less precision) instead of doubles is surely faster, likewise if i knew i was using 2 digits of precision i could simply treat everything as an int and just divide by 100 at the end, this would be much harder if i had to use M_PI (god knows how many digits of precision it uses).<br />I agree that this isn&#39;t a huge impact on performance but it just seems an overkill to use 20 digits of precision when at the end you&#39;re working with pixels (integers...)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FMC)</author>
		<pubDate>Thu, 26 Oct 2006 17:28:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I use AL_PI. Haven&#39;t a clue the difference as I never looked at M_PI and I know it&#39;ll work for every allegro program <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Thu, 26 Oct 2006 17:38:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Show me the advantage of using 10 instead of 3 significant figures in the average game...
</p></div></div><p>
Try shooting a missile or particle under a specific angle across the screen and see if it moves to where it&#39;s supposed to go. You&#39;ll be surprised at how far off course it will go. If you can do it right for the same cost as doing it wrong, why not do it right?</p><p>That wasn&#39;t what I asked you though. I asked you how it&#39;s wasteful to use the normal approximation of pi instead of, say, 22/7.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 26 Oct 2006 17:39:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I agree that this isn&#39;t a huge impact on performance but it just seems an overkill to use 20 digits of precision when at the end you&#39;re working with pixels (integers...)
</p></div></div><p>
Even in simple games with simple physics basically what you&#39;re doing is you&#39;re numerically solving differential equations. For this task there are several methods but because for a simple game fourth order Runge-Kutta would be overkill, most people just use the Newton method (or whatever it&#39;s actually called). That&#39;s when you do x_pos = x_pos + velocity_x and things like that. The advantage of this method is that it is extremely easy to implement. The disadvantage though is that all random errors in data or whatever add up over time (with Runge-Kutta and other methods they effectively get canceled out in successive steps), which results in what Evert said. You fire a missile in a certain direction, but after many steps of physics simulation with the Newton method, the missile is several pixels off because of round off error that kept piling on and on at each step...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Thu, 26 Oct 2006 17:49:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That wasn&#39;t what I asked you though. I asked you how it&#39;s wasteful to use the normal approximation of pi instead of, say, 22/7.
</p></div></div><p>Did you read the rest of the post?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Even in simple games with simple physics basically what you&#39;re doing is you&#39;re numerically solving differential equations. For this task there are several methods but because for a simple game fourth order Runge-Kutta would be overkill, most people just use the Newton method (or whatever it&#39;s actually called). That&#39;s when you do x_pos = x_pos + velocity_x and things like that. The advantage of this method is that it is extremely easy to implement. The disadvantage though is that all random errors in data or whatever add up over time (with Runge-Kutta and other methods they effectively get canceled out in successive steps), which results in what Evert said. You fire a missile in a certain direction, but after many steps of physics simulation with the Newton method, the missile is several pixels off because of round off error that kept piling on and on at each step...
</p></div></div><p>Agreed, but does it really matter much while playing?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FMC)</author>
		<pubDate>Thu, 26 Oct 2006 17:53:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>&lt;sarcasm on&gt;<br />Seems there&#39;s no reason to do it right if you can also do it wrong. <br />&lt;sarcasm off&gt;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Thu, 26 Oct 2006 17:54:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Agreed, but does it really matter much while playing?
</p></div></div><p>
With some types of games it does. Suppose you have a racing simulation where you simulate the suspension and all other things. Now if after a few seconds the suspension starts to wobble more and more and finally goes completely out of control because you&#39;re physics simulation is unstable, then I&#39;d say it matters.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Thu, 26 Oct 2006 17:57:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Did you read the rest of the post?
</p></div></div><p>
Of course I did. <img src="http://www.allegro.cc/forums/smileys/angry.gif" alt="&gt;:(" /><img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /><img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /><img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /><img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /><img src="http://www.allegro.cc/forums/smileys/angry.gif" alt="&gt;:(" /></p><p>Did you get my point?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 26 Oct 2006 17:59:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Did you read the rest of the post?
</p></div></div><p>
I did. And you still haven&#39;t provided an answer to what we&#39;re asking, namely: &quot;When you can do something <i>right</i> for no more cost or effort than doing it wrong - why the hell should you choose to do it the wrong way?&quot;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Thu, 26 Oct 2006 18:26:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ll restate what I was trying to point out in a more direct way: to no number of significant digits is it better to use 22/7 instead of the value of pi to said number of digits. The best case is 1 significant digit, for which case the difference is zero (since both are 3 in that case). The best case is for three digits, where pi is 3.14 and 22/7 is 3.15. It gets worse from there.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 26 Oct 2006 18:48:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>22/7 = 3.1428571428571428571428571428571<br />pi = 3.1415926535897932384626433832795<br />22/7 - pi = 0.00126448926734961868021375957764<br />pi - 3.14 = 0.0015926535897932384626433832795029</p><p>22/7 is a better aproximation to pi than 3.14. I frequently saw 3.14 being used as a aproximation to pi in several games without any noticeable glitch caused by that. So 22/7 should be ok for many practical purposes too.</p><p>However, something that should be noted is that 22/7 &gt; pi and 3.14 &lt; pi, this could make a difference in some cases.</p><p>[offtopic Edit:]<br />The ancient egiptyans used 19/6 for pi, which is worst than 22/7.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Victor Williams Stafusa da Silva)</author>
		<pubDate>Thu, 26 Oct 2006 19:16:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The small inacuraces would hardly be noticable but I believe it is just better practice, from a programming perspective, to use M_PI or AL_PI (or just memorize 3.141592653589793238462643383279502.....  Then you could create a multi-player game where the winner is the one who has more of PI memorized <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /> )
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ron Novy)</author>
		<pubDate>Thu, 26 Oct 2006 22:04:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The small inacuraces would hardly be noticable
</p></div></div><p>
It can be <i>very</i> noticable. The example I gave for firing missiles across the screen was from experience: a missile fired from the left of the screen towards a stationary target on the right of the screen under an angle would miss because I was trying to be clever and calculate with lower accuracy.</p><p>But yes, the point was that if youcan do it right, why wouldn&#39;t you?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 26 Oct 2006 22:20:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If accuracy doesn&#39;t count, you might gain a bit of speed by using floats rather than doubles.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Thu, 26 Oct 2006 22:29:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey, if everyone else is jumping into this...</p><p>I would think that defining your own PI variable would be more efficient than calling &quot;22/7&quot; each time the function is called.  Just less math that the computer has to compute in the routine.</p><p>That way, you have a 3- 5- or n- precision number to your liking that&#39;s been calculated ONCE and you don&#39;t need to run the math through every time.</p><p>I would think that M_PI, PI, AL_PI, or simply <tt>float MY_PI = 3.1415</tt> would increase the speed of the function.</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#define MY_PI 3.14159</span>

<span class="k1">double</span> grad2rad<span class="k2">(</span> <span class="k1">int</span> grad_angle <span class="k2">)</span><span class="k2">{</span>
  <span class="k1">return</span> <span class="k2">(</span><span class="k1">double</span><span class="k2">)</span><span class="k2">(</span>MY_PI <span class="k3">*</span> <span class="k2">(</span><span class="k2">(</span><span class="k1">double</span><span class="k2">)</span> grad_angle<span class="k2">)</span><span class="k2">)</span><span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Just my 2 cents...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (TeamTerradactyl)</author>
		<pubDate>Thu, 26 Oct 2006 22:44:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It isn&#39;t wrong to use floats instead of doubles, it&#39;s wrong to use a poor approximation to pi if you can use pi proper (to a given number of digits).<br />As I said above,
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
to no number of significant digits is it better to use 22/7 instead of the value of pi to said number of digits
</p></div></div><p>

EDIT
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
&quot;22/7&quot; each time the function is called. Just less math that the computer has to compute in the routine.
</p></div></div><p>
22/7 is a compile time constant, so it&#39;s evaluated at compile time (as opposed to runtime) by any decent compiler.<br />Anyway, if you&#39;re optimising that, you&#39;re probably optimising the wrong thing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 26 Oct 2006 22:44:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You&#39;re still dodging the core issue. Typing M_PI instead of 22/7 would give you superior accuracy at no cost whatsoever. How can one even possibly defend doing this wrong?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Thu, 26 Oct 2006 22:46:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Just less math that the computer has to compute in the routine.
</p></div></div><p>
That is wrong. AFAIK, the compiler calculates as many constant values as it can during compile-time (I think I&#39;ve read that in an old K&amp;R book, so it must be even better now) so it really doesn&#39;t matter. BTW, using that, you can still leave some constant expressions in your program to make it more readable, knowing that the compiler will take care of it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ron Ofir)</author>
		<pubDate>Thu, 26 Oct 2006 22:46:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There&#39;s a pretty good chance 22/7 will be calculated at compile time, yes. Constant expressions are a pretty trivial optimization.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Thu, 26 Oct 2006 22:53:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I would think that defining your own PI variable would be more efficient
</p></div></div><p>

I <u>have</u> seen where using a global variable is faster than a constant, as always, if it&#39;s not in a bottleneck, don&#39;t worry about it, else time it and see.  It&#39;d help if it was near to another recently used global if any.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">const</span> <span class="k1">float</span> mathpi <span class="k3">=</span> M_PI<span class="k2">;</span>
</pre></div></div><p>

Last time I looked at the asm output for a defined double it had to allocate and write two separate 32 bit values to the stack.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Thu, 26 Oct 2006 22:58:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Do some profiling, or write a simple benchmark program. </p><p>Is float really faster than double? On modern processors, I strongly doubt that... </p><p>A float is a float is a float, whatever number of digit you try to put into it (after the , of course). </p><p>Division needs several cycles, so a measure would be needed. </p><p>From my memory (can&#39;t find the article I&#39;ve read about this some time ago <img src="http://www.allegro.cc/forums/smileys/angry.gif" alt="&gt;:(" /> ) I recall that modern processors need 1 clock cycle for one multiplication. Regardless of the numbers being float or double. Have to find the reference article though...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Thu, 26 Oct 2006 23:01:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You&#39;re still dodging the core issue. Typing M_PI instead of 22/7 would give you superior accuracy at no cost whatsoever. How can one even possibly defend doing this wrong?
</p></div></div><p>Except I never said to use 22/7 instead</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I did. And you still haven&#39;t provided an answer to what we&#39;re asking, namely: &quot;When you can do something right for no more cost or effort than doing it wrong - why the hell should you choose to do it the wrong way?&quot;
</p></div></div><p>Because there are &quot;wrong&quot; ways that do the job at a lower cost?<br />For example (as i wrote before), imagine i have a complicated function  <br />(<span class="source-code"><span class="k1">float</span> f<span class="k2">(</span><span class="k1">float</span> x<span class="k2">)</span><span class="k2">;</span></span>), if i knew only 3 digits of precisions are needed, i could simply multiply the entering value by 1000 and storing it in an int do all calculation using integers (faster), divide by an integer 3142, continue my calculations and only at the end divide by 1000.0 - if the function is complex enough you gain much more by using ints than what you loose for the 2 extra operations...<br />but really it&#39;s not really something important as either way it doesn&#39;t really matter.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
With some types of games it does. Suppose you have a racing simulation where you simulate the suspension and all other things. Now if after a few seconds the suspension starts to wobble more and more and finally goes completely out of control because you&#39;re physics simulation is unstable, then I&#39;d say it matters.
</p></div></div><p>I&#39;d say a <i>simulation</i> is extensively based in physics, so it does make sense to use a good, even if complicated, model. That said i don&#39;t see how it hurts to use a rounded value if you are just using it for some particicle effects...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FMC)</author>
		<pubDate>Thu, 26 Oct 2006 23:02:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I made a little (ugly [I&#39;m still a beginner at it]) Haskell program:
</p><div class="source-code snippet"><div class="inner"><pre>fractions <span class="k3">=</span> <span class="k2">[</span><span class="k2">(</span>x,y<span class="k2">)</span> <span class="k3">|</span> x <span class="k3">&lt;</span><span class="k3">-</span><span class="k2">[</span><span class="n">1</span>.<span class="n">0</span>, <span class="n">2</span>.<span class="n">0</span>..<span class="n">250</span>.<span class="n">0</span><span class="k2">]</span> , y <span class="k3">&lt;</span><span class="k3">-</span><span class="k2">[</span><span class="n">1</span>.<span class="n">0</span>, <span class="n">2</span>.<span class="n">0</span>..<span class="n">250</span>.<span class="n">0</span><span class="k2">]</span> <span class="k2">]</span>
difference f <span class="k3">=</span> map <span class="k2">(</span>\<span class="k2">(</span>x,y<span class="k2">)</span> <span class="k3">-</span><span class="k3">&gt;</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a> <span class="k2">(</span>pi <span class="k3">-</span> x<span class="k3">/</span>y<span class="k2">)</span><span class="k2">)</span> f
fracwithdiff <span class="k3">=</span> zip fractions <span class="k2">(</span>difference fractions<span class="k2">)</span>

approx <span class="k2">[</span><span class="k2">]</span> <span class="k2">(</span><span class="k2">(</span>a,b<span class="k2">)</span>,c<span class="k2">)</span> <span class="k3">=</span> <span class="k2">(</span><span class="k2">(</span>a,b<span class="k2">)</span>,c<span class="k2">)</span>
approx <span class="k2">(</span><span class="k2">(</span><span class="k2">(</span>x,y<span class="k2">)</span>,z<span class="k2">)</span><span class="k2">:</span>xs<span class="k2">)</span> <span class="k2">(</span><span class="k2">(</span>a,b<span class="k2">)</span>,c<span class="k2">)</span> <span class="k3">=</span> <span class="k1">if</span> z <span class="k3">&lt;</span> c then approx xs <span class="k2">(</span><span class="k2">(</span>x,y<span class="k2">)</span>,z<span class="k2">)</span> <span class="k1">else</span> approx xs <span class="k2">(</span><span class="k2">(</span>a,b<span class="k2">)</span>,c<span class="k2">)</span>
</pre></div></div><p>
When I run it:
</p><div class="source-code snippet"><div class="inner"><pre>Main&gt; approx fracwithdiff <span class="k2">(</span><span class="k2">(</span><span class="n">1</span>,<span class="n">1</span><span class="k2">)</span>,<span class="n">1</span><span class="k2">)</span>
<span class="k2">(</span><span class="k2">(</span><span class="n">245</span>.<span class="n">0</span>,<span class="n">78</span>.<span class="n">0</span><span class="k2">)</span>,<span class="n">0</span>.<span class="n">000567012564152147</span><span class="k2">)</span>
Main&gt;
</pre></div></div><p>
So instead of writing 22.0/7.0 you could write 245.0/78.0 and get even better precision. <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /> Hehe, but that was of course just for fun. Using a well defined constant is the nicest approach.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kentl)</author>
		<pubDate>Thu, 26 Oct 2006 23:21:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
storing it in an int do all calculation using integers (faster)
</p></div></div><p>
It&#39;s not, floating point math isn&#39;t slow these days. With all those divisions (which may take dozens, if not hundreds of cycles) anyways. And floats aren&#39;t more processor efficient, they&#39;re only more memory efficient. Both floats and doubles are expanded to 96 bits before any calculations.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Fladimir da Gorf)</author>
		<pubDate>Fri, 27 Oct 2006 03:42:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Is float really faster than double? On modern processors, I strongly doubt that...
</p></div></div><p>
You&#39;re wrong then. Especially if you&#39;re doing a lot of floating point calculations you can really notice the difference. I suspect it is mainly a bandwith issue, however.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
A float is a float is a float, whatever number of digit you try to put into it (after the , of course).
</p></div></div><p>
Yes, except if the number of bytes in which it is stored is different (obviously).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Because there are &quot;wrong&quot; ways that do the job at a lower cost?
</p></div></div><p>
Re-read gnolam&#39;s post (or my earlier post). Especially the bit that says <i>When you can do something right <b>for no more cost</b> or effort than doing it wrong</i>.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
if i knew only 3 digits of precisions are needed
</p></div></div><p>
If you&#39;re only going to need three significant digits, then you <i>may</i> want to have pi to only three significant digits as well. That&#39;s not doing it wrong, that&#39;s doing it with limited precision. What&#39;s wrong is using a value of pi that is accurate to three digits while the rest of your numbers are accurate to sixteen (say) digits. Which is what I&#39;ve said two times already.<br />That said, cumulative errors from intermediate rounding reallydo matter, and you should not round until the last step if you can possibly avoid it. I&#39;ve once had a discussion about that when I was in highschool with my chemistry teacher, who maintained that it&#39;s ok. Which was why his answers were always a bit off in the last few digits &quot;due to rounding errors&quot;.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
if the function is complex enough you gain much more by using ints than what you loose for the 2 extra operations...
</p></div></div><p>
Did you benchmark that or did you pull that from thin air? float&lt;---&gt;int conversions are slow in themselves.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Fri, 27 Oct 2006 05:16:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
doing a lot of floating point calculations you can really notice the difference. I suspect it is mainly a bandwith issue, however.
</p></div></div><p>
I definitely have to find that article again. Also I&#39;m used to consider 64 bit processors (all brands), and there the difference between float and double computations is not noticable. In fact, as Fladimir 
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Both floats and doubles are expanded to 96 bits before any calculations
</p></div></div><p>
indicates, floats are expanded to the full length of the corresponding registers anyway. But as long as I don&#39;t find the already mentioned reference, most of this in only guessing...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Fri, 27 Oct 2006 12:06:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
full length of the corresponding registers anyway
</p></div></div><p>
I thought the registers in a 64-bit processor was of length 64-bit. And that&#39;s how you classify a 64-bit processor. Are you saying that todays 64-bit processors in fact have registers holding 96-bit?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kentl)</author>
		<pubDate>Fri, 27 Oct 2006 12:46:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve asked one of our specialists: In a 64 bit processor, the double registers hold 128 bits. The integer width is 64 bits. The number 64 mostly indicates the width of the data bus, so there&#39;s no reason why a 32 bit processor can&#39;t have 64 bit registers (as are the double registers in a P4 processor), but could have even more. I don&#39;t know about the MME or SSE registers, I&#39;ll try to find some references today. </p><p>How many bits does AGP have? And PCIe? I thought it was 128 bits, so you could push 4 integers from CPU to the GPU in 1 cycle!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Fri, 27 Oct 2006 12:59:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I definitely have to find that article again. Also I&#39;m used to consider 64 bit processors (all brands), and there the difference between float and double computations is not noticable.
</p></div></div><p>
My collaborator doing hydrodynamical simulations of colliding stars says otherwise from experience, and I sooner believe him. As I said, I suspect it&#39;s a bandwidth issue.<br />Personally I always use doubles for the extra precision.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Fri, 27 Oct 2006 13:17:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
In a 64 bit processor, the double registers hold 128 bits.
</p></div></div><p>
To my understanding a double register is just two normal registers which you can use for a limited number of things. If there are assembler directives which can handle double register arithmetic then those directives takes twice as long to execute as the ones which uses normal registers. Is that wrong?</p><p>In my book on Microprocessors it says that it&#39;s mostly the register width which is used to classify a processor as X-bit.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I&#39;ve asked one of our specialists
</p></div></div><p>
I rather read what someone here who knows this stuff has to say. I&#39;ve only taken one Microprocessor course as I study CS. But there are lots of people here with good knowledge of hardware. Let&#39;s hope they pop up.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kentl)</author>
		<pubDate>Fri, 27 Oct 2006 13:21:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m reading through the &quot;Intel 64 and IA-32 Architectures Software Developer&#39;s Manual&quot;, downloaded from www.intel.com. </p><p>The table on page 53 gives a lot of numbers: For all Pentium processors, the external data bus has 64 bits. The general purpose registers all have 32 bits. The floating point registers have 80 bits. Then there are MMX registers, which all have 64 bits, and XMM registers, used for SSE2 and later, which all have 128 bits. </p><p>Internal data paths are 2-4 times wider than the external data bus. </p><p>This paper does only describe HOW the assembler statements work, not how many cycles they use. Next paper... </p><p>&quot;IA-32 Intel Architecture Optimization Reference Manual&quot; gives a lot of hints how to improve speed when dealing with anything. However, it seems that all this stuff is pretty complicated, and again, no numbers of clock cycles are given. </p><p>One thing I now have learned from this is: Most probably, it&#39;s entirely unimportant how many cycles the multiplication (or any operation) exactly has. There are other limiting factors, which mostly concern the ability of the processor to do things in parallel (blocking events), and when to get or store data. Cache is important here, branch prediction, and more... </p><p>So I would only go for correctness, then do profiling if things are too slow.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Fri, 27 Oct 2006 13:51:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The MMX registers are often the same as the floating point registers (at least, they used to be). That&#39;s why if you&#39;re doing MMX calculations, you can&#39;t do any floating-point calculations before you&#39;ve called EMMS.</p><p>But yeah, I remembered wrong. The size of the FP register is 80 bits. But the same registers are used with both floats and doubles, so there&#39;s no difference in calculation speed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Fladimir da Gorf)</author>
		<pubDate>Fri, 27 Oct 2006 15:12:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
But the same registers are used with both floats and doubles, so there&#39;s no difference in calculation speed.
</p></div></div><p>
I think I&#39;ll write a small test program for this. From what I&#39;ve read this morning it might well be possible that the instructions of the P4 can specifiy how many of those 80 bits to use for calculations, as there are 3 different float formats possible for each register: 32 bit, 64 bit (double, same as MMX/SSE) and 80 bit (double extended precision). So it might be possible that multiplying two floats can be a little faster than multiplying two doubles. </p><p>I guess though, that the impact of this is small compared to the effects of pipelining instructions and branch predicition, as well as what is refered to as &#39;changing mode&#39; of the fpu registers. This suggests (as is also recommended in the Optimization Guide) that you stick to one particular floating format and don&#39;t mix different bit length.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Fri, 27 Oct 2006 15:24:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
But the same registers are used with both floats and doubles, so there&#39;s no difference in calculation speed.
</p></div></div><p>
Maybe. Which is why I think it&#39;s mainly a bandwidth issue.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Fri, 27 Oct 2006 16:29:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>For 387 registers, the 32 bit float, 64 bit double or whatever is always treated as an 80 bit long double unless the precision in the control word has been altered from the default (which gcc does for compatibility for some old and IMAO broken programs).  This is after loading the number from memory of course.  IIRC, a double can be slower if it crosses some certain boundary, and of course if you&#39;re using an array bigger than cache size you&#39;re wasting a lot of time.</p><p>You can&#39;t take those Intel docs too seriously, back in the &#39;90&#39;s I had an 8086/8087 Intel book of some kind, and it said the 8087 sped up floating point calculations from &quot;10 to 100 times faster&quot;, but eventually I got an XP and clone 286 and my asm floating point stuff was faster on the 286 (fpu slowed down to 2/3 cpu speed) and only slightly slower on the XP (8 bit bus).  Except for trancendentals of course <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Fri, 27 Oct 2006 22:43:44 +0000</pubDate>
	</item>
</rss>
