This repository has been archived on 2024-09-22. You can view files and clone it, but cannot push or open issues or pull requests.
pbi-ide/help/BasicStampHelp/Content/LanguageTopics/Reference/OpsPrecedence.htm

105 lines
7.5 KiB
HTML

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="PBASIC Language Reference|PBASIC Operators" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
<head><title>Operator Precedence</title>
<link href="../../SkinSupport/MadCap.css" rel="stylesheet" />
<link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
<script src="../../SkinSupport/MadCapAll.js" type="text/javascript">
</script>
</head>
<body>
<div class="MCBreadcrumbsBox_0"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="AlphaRef.htm">PBASIC Language Reference</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="Operators.htm">PBASIC Operators</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Operator Precedence</span>
</div>
<h1>Operator Precedence</h1>
<div class="ImagePlusCaption">
<div class="Col2">
<p>
<img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" />
<img src="../../graphics/pgm_icon2.gif" border="0" alt="BS2 icon" title="BS2 icon" />
<img src="../../graphics/pgm_icon2e.gif" border="0" alt="BS2e icon" title="BS2e icon" />
<img src="../../graphics/pgm_icon2sx.gif" border="0" alt="BS2sx icon" title="BS2sx icon" />
<img src="../../graphics/pgm_icon2p.gif" border="0" alt="BS2p icon" title="BS2p icon" />
<img src="../../graphics/pgm_icon2pe.gif" border="0" alt="BS2pe icon" title="BS2pe icon" />
<img src="../../graphics/pgm_icon2px.gif" border="0" alt="BS2px icon" title="BS2px icon" />
</p>
</div>
<p style="text-align: right;">&#160;</p>
<p>&#160;</p>
</div>
<p class="clear">&#160;</p>
<p>&#160;</p>
<p>(See <a href="Operators.htm" target="" title="" alt="" class="MCXref_0">PBASIC Operators</a> for a complete list of all PBASIC&#160;operators, with links to explanations of each.)</p>
<p>&#160;</p>
<h2>Left-to-Right Evaluation</h2>
<p class="PlainText">The BASIC Stamp solves math problems in the order they are written;
from left to right. The result of each operation is fed into the next
operation. So to compute:</p>
<p>&#160; &#160; 12 + 3 * 2 / 4</p>
<p>&#160;</p>
<p class="PlainText">...the BASIC Stamp goes through a
sequence like this:</p>
<p>&#160; &#160; 12 + 3 = 15<br></br>&#160; &#160; 15 * 2 = 30<br></br>&#160; &#160; 30 / 4 = 7</p>
<p>&#160;</p>
<p class="PlainText">Note that since the BASIC Stamp performs integer math (whole numbers
only) 30 / 4 results in 7, not 7.5. </p>
<p class="PlainText">Some other dialects of BASIC would compute that same expression based
on their precedence of operators, which requires that multiplication and
division be done before addition. So the result would be:</p>
<p>&#160; &#160; 3 * 2 = 6<br></br>&#160; &#160; 6 / 4 = 1<br></br>&#160; &#160; 12 + 1 = 13</p>
<p>&#160;</p>
<p class="PlainText">Once again, because of integer math, the fractional portion of 6 / 4 is
dropped, so we get 1 instead of 1.5.</p>
<p class="PlainText">Unary operators (those with one argument) take precedence over binary operators (those with two arguments); the unary operation is always performed first. For example, on all BS2 models, SQR is the unary operator for square root. In the expression:</p>
<p class="PlainText">10 - SQR 16</p>
<p class="PlainText">...the BASIC Stamp first takes the square root of 16, then subtracts it from 10.</p>
<h2>Using Parentheses </h2>
<img src="../../graphics/bs1_inline.gif" border="0" alt="">
</img>
<p class="PlainText">The BS1 does not allow parentheses in expressions. Unfortunately, all
expressions have to be written so that they evaluate as intended strictly
from left to right. The negative operator is the only unary operator for the BS1.</p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="">
</img>
<p class="PlainText">The BS2 family modules, however, allow parentheses to be
used to change the order of evaluation. Enclosing a math operation in
parentheses gives it priority over other operations. To make the BASIC Stamp
compute the previous expression in the conventional way, you would write it
as 12 + (3 * 2 / 4). Within the parentheses, the BASIC Stamp works from left
to right. If you wanted to be even more specific, you could write 12 +
((3 * 2) / 4). When there are parentheses within parentheses, the BASIC
Stamp works from the innermost parentheses outward. Parentheses
placed within parentheses are called nested parentheses.</p>
<h2>Integer Math</h2>
<p class="PlainText">The BASIC Stamp performs all math operations by the rules of positive integer math. That is, it handles only whole numbers, and drops any fractional portions from the results of computations. The BASIC Stamp handles negative numbers using two's complement rules. </p>
<p class="PlainText">All BS2 models can interpret two's complement negative numbers correctly in <span class="keyword_in_text">DEBUG</span> and <span class="keyword_in_text">SEROUT</span> instructions using formatters like SDEC (for signed decimal). In calculations, however, it assumes that all values are positive. This yields correct results with two's complement negative numbers for addition, subtraction, and multiplication, but not for division. </p>
<div class="Col2">
<div class="MasterFoot">
<p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a>
</p>
</div>
</div>
<div class="Col2">
<div class="MasterFoot">
<p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p>
<p style="text-align: right;">Copyright ©&#160;<span class="ContactInfoCompanyName">Parallax Inc.</span></p>
<p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span>
</p>
</div>
</div>
<script type="text/javascript">/* <![CDATA[ */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-285614-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
/* ]]> */</script>
<script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
</script>
</body>
</html>