<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sgn | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/sgn/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Mon, 04 May 2020 09:31:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>sgn | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>The missing SGN function</title>
		<link>https://learncgames.com/the-missing-sgn-function/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-missing-sgn-function</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sat, 07 Mar 2020 00:00:35 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[sgn]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=75</guid>

					<description><![CDATA[<p>Both C and C++ lack a sgn (short for signum apparently!) function (unless C++ has added it in recent changes. It&#8217;s also available in Boost but this blog is not about C++ so who cares! Sgn() for those who don&#8217;t know is a function that returns -1 if the passed in int value is negative, [&#8230;]</p>
The post <a href="https://learncgames.com/the-missing-sgn-function/">The missing SGN function</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p>Both C and C++ lack a sgn (short for signum apparently!) function (unless C++ has added it in recent changes. It&#8217;s also available in Boost but this blog is not about C++ so who cares!</p>
<p>Sgn() for those who don&#8217;t know is a function that returns -1 if the passed in int value is negative, 1 if the value was positive or 0 otherwise. Even BASIC includes it but not C.</p>
<p>However it&#8217;s easy to add. Something like</p>
<pre><code class="language-c" data-line="">int sgn(in x) {
  if (x=0) return 0;
    else
  if (x&gt;0) return 1;
  else 
    return -1;
}</code></pre>
<p>Of course if you are using longints or floats or doubles, you have to write those as well. However an alternative is to make it into a <strong>macro</strong>.</p>
<pre><code class="language-c" data-line="">#define sgn(x) (x &lt; 0) ? -1 : (x &gt; 0)
int a = sgn(10);</code></pre>
<p>The only downside if you make lots of calls to sgn() is that it will slightly bulk up your code compared to calling it as a function but it will run fractionally faster!</p>The post <a href="https://learncgames.com/the-missing-sgn-function/">The missing SGN function</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">75</post-id>	</item>
	</channel>
</rss>
