Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 318

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Integer Arithmetic - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Integer-Data-Types.html#Integer-Data-Types" title="Integer Data Types">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="Integer-Arithmetic"></a>
<p>
Up:&nbsp;<a rel="up" accesskey="u" href="Integer-Data-Types.html#Integer-Data-Types">Integer Data Types</a>
<hr>
</div>

<h4 class="subsection">4.4.1 Integer Arithmetic</h4>

<p>While many numerical computations can't be carried out in integers,
Octave does support basic operations like addition and multiplication
on integers.  The operators <code>+</code>, <code>-</code>, <code>.*</code>, and <code>./</code>
work on integers of the same type.  So, it is possible to add two 32 bit
integers, but not to add a 32 bit integer and a 16 bit integer.

   <p>When doing integer arithmetic one should consider the possibility of
underflow and overflow.  This happens when the result of the computation
can't be represented using the chosen integer type.  As an example it is
not possible to represent the result of 10 - 20 when using
unsigned integers.  Octave makes sure that the result of integer
computations is the integer that is closest to the true result.  So, the
result of 10 - 20 when using unsigned integers is zero.

   <p>When doing integer division Octave will round the result to the nearest
integer.  This is different from most programming languages, where the
result is often floored to the nearest integer.  So, the result of
<code>int32(5) ./ int32(8)</code> is <code>1</code>.

<!-- idivide scripts/general/idivide.m -->
   <p><a name="doc_002didivide"></a>

<div class="defun">
&mdash; Function File:  <b>idivide</b> (<var>x, y, op</var>)<var><a name="index-idivide-282"></a></var><br>
<blockquote><p>Integer division with different rounding rules.

        <p>The standard behavior of integer division such as <var>a</var><code> ./ </code><var>b</var>
is to round the result to the nearest integer.  This is not always the
desired behavior and <code>idivide</code> permits integer element-by-element
division to be performed with different treatment for the fractional
part of the division as determined by the <var>op</var> flag.  <var>op</var> is
a string with one of the values:

          <dl>
<dt>"fix"<dd>Calculate <var>a</var><code> ./ </code><var>b</var> with the fractional part rounded
towards zero.

          <br><dt>"round"<dd>Calculate <var>a</var><code> ./ </code><var>b</var> with the fractional part rounded
towards the nearest integer.

          <br><dt>"floor"<dd>Calculate <var>a</var><code> ./ </code><var>b</var> with the fractional part rounded
towards negative infinity.

          <br><dt>"ceil"<dd>Calculate <var>a</var><code> ./ </code><var>b</var> with the fractional part rounded
towards positive infinity. 
</dl>

     <p class="noindent">If <var>op</var> is not given it defaults to <code>"fix"</code>. 
An example demonstrating these rounding rules is

     <pre class="example">          idivide (int8 ([-3, 3]), int8 (4), "fix")
            &rArr; int8 ([0, 0])
          idivide (int8 ([-3, 3]), int8 (4), "round")
            &rArr; int8 ([-1, 1])
          idivide (int8 ([-3, 3]), int8 (4), "floor")
            &rArr; int8 ([-1, 0])
          idivide (int8 ([-3, 3]), int8 (4), "ceil")
            &rArr; int8 ([0, 1])
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002dldivide.html#doc_002dldivide">ldivide</a>, <a href="doc_002drdivide.html#doc_002drdivide">rdivide</a>. 
</p></blockquote></div>

   </body></html>