<?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>Honza's perspective &#187; Programming</title>
	<atom:link href="http://blog.stastnarodina.com/honza-en/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stastnarodina.com/honza-en</link>
	<description></description>
	<lastBuildDate>Mon, 03 May 2010 09:30:45 +0000</lastBuildDate>
	<language>cs</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>C++ round</title>
		<link>http://blog.stastnarodina.com/honza-en/spot/c-round/</link>
		<comments>http://blog.stastnarodina.com/honza-en/spot/c-round/#comments</comments>
		<pubDate>Sun, 20 Jan 2008 03:53:00 +0000</pubDate>
		<dc:creator>honza</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.stastnarodina.com/honza-en/?p=8</guid>
		<description><![CDATA[
<p>27th January Update: as somebody has pointed via email (thanks for that), the
function I gave only worked for positive numbers. This is the solution he
offered (altered a bit not to give warnings so that you can cut&amp;paste ):
#include &lt;cmath&gt; int function round(double num) { char sign =
static_cast&lt;char&gt;(num/­fabs(num)); return static_cast&lt;in­t&gt;(sign*
(fabs(num)+ 0.5)); } Actually, […]</p>

<!-- by Texy2! -->]]></description>
			<content:encoded><![CDATA[
<p><em>27th January <strong>Update:</strong></em> as somebody has pointed via
email (thanks for that), the function I gave only worked for positive numbers.
This is the solution he offered (altered a bit not to give warnings so that you
can cut&amp;paste ):</p>

<pre><code>#include &lt;cmath&gt;
int function round(double num) {
  char sign = static_cast&lt;char&gt;(num/fabs(num));
  return static_cast&lt;int&gt;(sign* (fabs(num)+ 0.5));
}</code></pre>

<p>Actually, the solution I gave first (and then suddenly changed it to the one
that only works for positive number…) worked perfectly with both positive and
negative too. This is it:</p>

<pre><code>#include &lt;cmath&gt;
int function round(double num) {
  return static_cast&lt;int&gt;(floor(num+0.5));
}</code></pre>

<p>I was a bit shocked first, finding out there was no <em>round</em> function
in C++.</p>

<p>It didn't fortunately take me long to google a nice substitute for it. It can
be as simple as this (<strong>Warning:</strong> this works for positive numbers
only. See above for better solution):</p>

<pre><code>int round(double num) {
 return (int)(num+0.5);
}</code></pre>

<p>I may be one of the more dumb and lazy programmers (isn't „laziness“ in
the flag of all programmers?), but I would expect a function like that to be a
part of a modern language. Yes, it didn't take me long to google it (unlike some
other C++ problems, which can take hours), but no googling is faster than short
googling, isn't it?</p>

<p>I think it is worth seeing, what <a href="http://java.sun.com/">The
Others</a> <a
href="http://java.sun.com/javase/6/docs/api/java/lang/Math.html">offer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stastnarodina.com/honza-en/spot/c-round/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
