<html lang="en"> <head> <title>Promotion and Demotion of Data Types - 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="Numeric-Data-Types.html#Numeric-Data-Types" title="Numeric Data Types"> <link rel="prev" href="Logical-Values.html#Logical-Values" title="Logical Values"> <link rel="next" href="Predicates-for-Numeric-Objects.html#Predicates-for-Numeric-Objects" title="Predicates for Numeric Objects"> <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="Promotion-and-Demotion-of-Data-Types"></a> <p> Next: <a rel="next" accesskey="n" href="Predicates-for-Numeric-Objects.html#Predicates-for-Numeric-Objects">Predicates for Numeric Objects</a>, Previous: <a rel="previous" accesskey="p" href="Logical-Values.html#Logical-Values">Logical Values</a>, Up: <a rel="up" accesskey="u" href="Numeric-Data-Types.html#Numeric-Data-Types">Numeric Data Types</a> <hr> </div> <h3 class="section">4.7 Promotion and Demotion of Data Types</h3> <p>Many operators and functions can work with mixed data types. For example, <pre class="example"> uint8 (1) + 1 ⇒ 2 </pre> <p class="noindent">where the above operator works with an 8-bit integer and a double precision value and returns an 8-bit integer value. Note that the type is demoted to an 8-bit integer, rather than promoted to a double precision value as might be expected. The reason is that if Octave promoted values in expressions like the above with all numerical constants would need to be explicitly cast to the appropriate data type like <pre class="example"> uint8 (1) + uint8 (1) ⇒ 2 </pre> <p class="noindent">which becomes difficult for the user to apply uniformly and might allow hard to find bugs to be introduced. The same applies to single precision values where a mixed operation such as <pre class="example"> single (1) + 1 ⇒ 2 </pre> <p class="noindent">returns a single precision value. The mixed operations that are valid and their returned data types are <p><table summary=""><tr align="left"><th valign="top" width="20%"></th><th valign="top" width="30%">Mixed Operation </th><th valign="top" width="30%">Result </th><th valign="top" width="20%"> <br></th></tr><tr align="left"><td valign="top" width="20%"></td><td valign="top" width="30%">double OP single </td><td valign="top" width="30%">single </td><td valign="top" width="20%"> <br></td></tr><tr align="left"><td valign="top" width="20%"></td><td valign="top" width="30%">double OP integer </td><td valign="top" width="30%">integer </td><td valign="top" width="20%"> <br></td></tr><tr align="left"><td valign="top" width="20%"></td><td valign="top" width="30%">double OP char </td><td valign="top" width="30%">double </td><td valign="top" width="20%"> <br></td></tr><tr align="left"><td valign="top" width="20%"></td><td valign="top" width="30%">double OP logical </td><td valign="top" width="30%">double </td><td valign="top" width="20%"> <br></td></tr><tr align="left"><td valign="top" width="20%"></td><td valign="top" width="30%">single OP integer </td><td valign="top" width="30%">integer </td><td valign="top" width="20%"> <br></td></tr><tr align="left"><td valign="top" width="20%"></td><td valign="top" width="30%">single OP char </td><td valign="top" width="30%">single </td><td valign="top" width="20%"> <br></td></tr><tr align="left"><td valign="top" width="20%"></td><td valign="top" width="30%">single OP logical </td><td valign="top" width="30%">single </td><td valign="top" width="20%"> <br></td></tr></table> <p>The same logic applies to functions with mixed arguments such as <pre class="example"> min (single (1), 0) ⇒ 0 </pre> <p class="noindent">where the returned value is single precision. <p>In the case of mixed type indexed assignments, the type is not changed. For example, <pre class="example"> x = ones (2, 2); x (1, 1) = single (2) ⇒ x = 2 1 1 1 </pre> <p class="noindent">where <code>x</code> remains of the double precision type. </body></html>