<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Inline Math.ceil()</title>
	<atom:link href="http://jacksondunstan.com/articles/351/feed" rel="self" type="application/rss+xml" />
	<link>http://jacksondunstan.com/articles/351</link>
	<description>Mastering AS3</description>
	<lastBuildDate>Mon, 14 May 2012 17:02:12 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Patrick Denny</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-15072</link>
		<dc:creator>Patrick Denny</dc:creator>
		<pubDate>Sat, 29 Oct 2011 01:39:34 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-15072</guid>
		<description>Crap. Munged the code:
&lt;pre lang=&quot;actionscript3&quot;&gt;
var value,res;
value=1.1;
(value == (res = value&lt;&lt; 0 ? res++: res--)); // res == 2
value=2;
(value == (res = value&lt;&lt; 0 ? res++: res--)); // res == 2
value=-1.1;
(value == (res = value&lt;&lt; 0 ? res++: res--)); // res == -1
value=-2;
(value == (res = value&lt;&lt; 0 ? res++: res--)); // res == -2
&lt;/pre&gt;
and results
&lt;pre&gt;
Type    10000000 iterations
Math.ceil    75
your BW    51
my BW    21
your int()    48
my int()    19
&lt;/pre&gt;
If that didn&#039;t work I hope you can get the gist</description>
		<content:encoded><![CDATA[<p>Crap. Munged the code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">,</span>res<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">value</span>=<span style="color: #000000; font-weight:bold;">1.1</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == <span style="color: #000000;">&#40;</span>res = value<span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">?</span> res<span style="color: #000066; font-weight: bold;">++:</span> res<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// res == 2</span>
<span style="color: #004993;">value</span>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == <span style="color: #000000;">&#40;</span>res = value<span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">?</span> res<span style="color: #000066; font-weight: bold;">++:</span> res<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// res == 2</span>
<span style="color: #004993;">value</span>=<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1.1</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == <span style="color: #000000;">&#40;</span>res = value<span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">?</span> res<span style="color: #000066; font-weight: bold;">++:</span> res<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// res == -1</span>
<span style="color: #004993;">value</span>=<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == <span style="color: #000000;">&#40;</span>res = value<span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">?</span> res<span style="color: #000066; font-weight: bold;">++:</span> res<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// res == -2</span></pre></div></div>

<p>and results</p>
<pre>
Type    10000000 iterations
Math.ceil    75
your BW    51
my BW    21
your int()    48
my int()    19
</pre>
<p>If that didn&#8217;t work I hope you can get the gist</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Patrick Denny</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-15071</link>
		<dc:creator>Patrick Denny</dc:creator>
		<pubDate>Sat, 29 Oct 2011 01:35:35 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-15071</guid>
		<description>by moving the assignment inside the trinary, you can remove extraneous bitwise shifts (or int() calls) and it speeds up even more
&lt;pre lang=&quot;actionscript3&quot;&gt;
var value,res;
value=1.1;
(value == (res = value&lt; 0 ? res++: res--)); // res == 2
value=2;
(value == (res = value&lt; 0 ? res++: res--)); // res == 2
value=-1.1;
(value == (res = value&lt; 0 ? res++: res--)); // res == -1
value=-2;
(value == (res = value&lt; 0 ? res++: res--)); // res == -2
&lt;pre&gt;
and results
&lt;pre&gt;
Type	10000000 iterations
Math.ceil	75
your BW	51
my BW	21
your int()	48
my int()	19
&lt;/pre&gt;
http://wonderfl.net/c/330S</description>
		<content:encoded><![CDATA[<p>by moving the assignment inside the trinary, you can remove extraneous bitwise shifts (or int() calls) and it speeds up even more</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">,</span>res<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">value</span>=<span style="color: #000000; font-weight:bold;">1.1</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == <span style="color: #000000;">&#40;</span>res = value<span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">?</span> res<span style="color: #000066; font-weight: bold;">++:</span> res<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// res == 2</span>
<span style="color: #004993;">value</span>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == <span style="color: #000000;">&#40;</span>res = value<span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">?</span> res<span style="color: #000066; font-weight: bold;">++:</span> res<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// res == 2</span>
<span style="color: #004993;">value</span>=<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1.1</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == <span style="color: #000000;">&#40;</span>res = value<span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">?</span> res<span style="color: #000066; font-weight: bold;">++:</span> res<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// res == -1</span>
<span style="color: #004993;">value</span>=<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == <span style="color: #000000;">&#40;</span>res = value<span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">?</span> res<span style="color: #000066; font-weight: bold;">++:</span> res<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// res == -2</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>pre<span style="color: #000066; font-weight: bold;">&gt;</span>
and results
<span style="color: #000066; font-weight: bold;">&lt;</span>pre<span style="color: #000066; font-weight: bold;">&gt;</span>
Type    <span style="color: #000000; font-weight:bold;">10000000</span> iterations
<span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ceil</span>    <span style="color: #000000; font-weight:bold;">75</span>
your BW    <span style="color: #000000; font-weight:bold;">51</span>
my BW    <span style="color: #000000; font-weight:bold;">21</span>
your <span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>    <span style="color: #000000; font-weight:bold;">48</span>
my <span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>    <span style="color: #000000; font-weight:bold;">19</span></pre></div></div>

<p><a href="http://wonderfl.net/c/330S" rel="nofollow">http://wonderfl.net/c/330S</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-130</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Tue, 06 Oct 2009 05:18:21 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-130</guid>
		<description>Thanks for the follow up. Indeed this does fix the problem and now your solution looks just like mine except for how you do your casting. I was curious so I tried using your bitwise casts in the same test app on the same machine as I did the test in the article. Here&#039;s what I got:

&lt;pre lang=&quot;text&quot;&gt;
BW (positive only): 829
BW (positive and negative): 826
&lt;/pre&gt;

That&#039;s nearly twice as slow as just using the &lt;tt&gt;int()&lt;/tt&gt; cast. This seems to prove that the int() cast is, as Karl pointed out, much quicker than the bitwise trick in AS3. And as you point out, both of them blow away &lt;tt&gt;Math.ceil()&lt;/tt&gt;!</description>
		<content:encoded><![CDATA[<p>Thanks for the follow up. Indeed this does fix the problem and now your solution looks just like mine except for how you do your casting. I was curious so I tried using your bitwise casts in the same test app on the same machine as I did the test in the article. Here&#8217;s what I got:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">BW (positive only): 829
BW (positive and negative): 826</pre></div></div>

<p>That&#8217;s nearly twice as slow as just using the <tt>int()</tt> cast. This seems to prove that the int() cast is, as Karl pointed out, much quicker than the bitwise trick in AS3. And as you point out, both of them blow away <tt>Math.ceil()</tt>!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aruban</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-129</link>
		<dc:creator>aruban</dc:creator>
		<pubDate>Tue, 06 Oct 2009 04:55:42 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-129</guid>
		<description>(this test was done directly on Flash CS3, player 9 with an imac 2.16GHz C2D)</description>
		<content:encoded><![CDATA[<p>(this test was done directly on Flash CS3, player 9 with an imac 2.16GHz C2D)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aruban</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-128</link>
		<dc:creator>aruban</dc:creator>
		<pubDate>Tue, 06 Oct 2009 04:52:05 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-128</guid>
		<description>I wrote too quickly, that&#039;s true that (val&gt;&gt;0)+1 works only if u sure that your value is a positive float.
Anyway, made the test and as Karl is mentioning, it looks like the casting is slightly faster that the bit shifting, which surprised me ! :)
-------------------
res = value==(value&gt;&gt;0)?value:(value&gt;&gt;0)+1 //positive only
res = value==(value&gt;&gt;0)?value:(value&gt;&gt;0)+(value&gt;=0?1:0); // pos&amp;neg
-------------------
Inline:
Positive only:
	1.1: 2 == 2: PASS
	2: 2 == 2: PASS
Positive and negative:
	1.1: 2 == 2: PASS
	2: 2 == 2: PASS
	-1.1: -1 == -1: PASS
	-2: -2 == -2: PASS
BS:
Positive only:
	1.1: 2 == 2: PASS
	2: 2 == 2: PASS
Positive and negative:
	1.1: 2 == 2: PASS
	2: 2 == 2: PASS
	-1.1: -1 == -1: PASS
	-2: -2 == -2: PASS
	
BW (positive only): 793
Inline (positive only): 758
BW (positive and negative): 778
Inline (positive and negative): 796
Math.ceil(): 7145
-------------------
Both are anyway blowing away Math.ceil perf. 
As far as I know, for performances, avoiding the use of Math object is part of the good practices :)</description>
		<content:encoded><![CDATA[<p>I wrote too quickly, that&#8217;s true that (val&gt;&gt;0)+1 works only if u sure that your value is a positive float.<br />
Anyway, made the test and as Karl is mentioning, it looks like the casting is slightly faster that the bit shifting, which surprised me ! :)<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
res = value==(value&gt;&gt;0)?value:(value&gt;&gt;0)+1 //positive only<br />
res = value==(value&gt;&gt;0)?value:(value&gt;&gt;0)+(value&gt;=0?1:0); // pos&amp;neg<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Inline:<br />
Positive only:<br />
	1.1: 2 == 2: PASS<br />
	2: 2 == 2: PASS<br />
Positive and negative:<br />
	1.1: 2 == 2: PASS<br />
	2: 2 == 2: PASS<br />
	-1.1: -1 == -1: PASS<br />
	-2: -2 == -2: PASS<br />
BS:<br />
Positive only:<br />
	1.1: 2 == 2: PASS<br />
	2: 2 == 2: PASS<br />
Positive and negative:<br />
	1.1: 2 == 2: PASS<br />
	2: 2 == 2: PASS<br />
	-1.1: -1 == -1: PASS<br />
	-2: -2 == -2: PASS</p>
<p>BW (positive only): 793<br />
Inline (positive only): 758<br />
BW (positive and negative): 778<br />
Inline (positive and negative): 796<br />
Math.ceil(): 7145<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Both are anyway blowing away Math.ceil perf.<br />
As far as I know, for performances, avoiding the use of Math object is part of the good practices :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl Knocking</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-115</link>
		<dc:creator>Karl Knocking</dc:creator>
		<pubDate>Mon, 05 Oct 2009 18:00:52 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-115</guid>
		<description>Maybe he found it here:
http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/

Although the site claims that the casting is faster in AS3..</description>
		<content:encoded><![CDATA[<p>Maybe he found it here:<br />
<a href="http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/" rel="nofollow">http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/</a></p>
<p>Although the site claims that the casting is faster in AS3..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-113</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Mon, 05 Oct 2009 16:36:18 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-113</guid>
		<description>That&#039;s one crazy trick. Where did you find it? I tried it out for correctness:

&lt;pre lang=&quot;text&quot;&gt;
Positive only:
	1.1: 2 == 2: PASS
	2: 3 == 2: FAIL
Positive and negative:
	1.1: 2 == 2: PASS
	2: 3 == 2: FAIL
	-1.1: 0 == -1: FAIL
	-2: -1 == -2: FAIL
&lt;/pre&gt;

It seems to round up whole numbers as though you did:

&lt;pre lang=&quot;actionscript3&quot;&gt;
int(value)+1;
&lt;/pre&gt;

Which I suppose is what the bitwise shift is for. So I suppose if you knew that your input numbers were not whole numbers then it would be usable, but not in the general case.</description>
		<content:encoded><![CDATA[<p>That&#8217;s one crazy trick. Where did you find it? I tried it out for correctness:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Positive only:
    1.1: 2 == 2: PASS
    2: 3 == 2: FAIL
Positive and negative:
    1.1: 2 == 2: PASS
    2: 3 == 2: FAIL
    -1.1: 0 == -1: FAIL
    -2: -1 == -2: FAIL</pre></div></div>

<p>It seems to round up whole numbers as though you did:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Which I suppose is what the bitwise shift is for. So I suppose if you knew that your input numbers were not whole numbers then it would be usable, but not in the general case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aruban</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-108</link>
		<dc:creator>aruban</dc:creator>
		<pubDate>Mon, 05 Oct 2009 14:28:35 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-108</guid>
		<description>nope. quick test on my macbook Air

Inline (positive only): 1067
Inline (positive and negative): 1067
bitwise operation (value&gt;&gt;0)+1: 698
Math.ceil(): 6484

:)</description>
		<content:encoded><![CDATA[<p>nope. quick test on my macbook Air</p>
<p>Inline (positive only): 1067<br />
Inline (positive and negative): 1067<br />
bitwise operation (value&gt;&gt;0)+1: 698<br />
Math.ceil(): 6484</p>
<p>:)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aruban</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-107</link>
		<dc:creator>aruban</dc:creator>
		<pubDate>Mon, 05 Oct 2009 14:23:57 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-107</guid>
		<description>Is that faster than (val &gt;&gt; 0)+1 ?</description>
		<content:encoded><![CDATA[<p>Is that faster than (val &gt;&gt; 0)+1 ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Fritsch</title>
		<link>http://jacksondunstan.com/articles/351/comment-page-1#comment-100</link>
		<dc:creator>Ramon Fritsch</dc:creator>
		<pubDate>Mon, 05 Oct 2009 12:08:37 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=351#comment-100</guid>
		<description>Congrats man, very useful tip.

I haven&#039;t idea how much that is costs to cpu. great job.</description>
		<content:encoded><![CDATA[<p>Congrats man, very useful tip.</p>
<p>I haven&#8217;t idea how much that is costs to cpu. great job.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

