Skip to main content
Engineering LibreTexts

5.5: Mixed Mode

  • Page ID
    54259
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In general, mathematical operations should be performed on variables of the same type. When both integer and real values or variables are used in the same statement, it is called mixed mode.

    Real and integer operations:

    1/2 = 0
    1.0 + 1/4 = 1.0
    1.0 + 1.0/4 = 1.25
    

    Any integers values are converted to real only when mixed-mode is encountered on the same operation type. Conversion may also occur on assignment.

    Unexpected conversions can cause problems when calculating values. In order to avoid such problems, it is strongly recommended to not use mixed-mode. There are a series of rules associated with mixed mode operations. In some circumstances, these rules can be confusing. For simplicity, those rules are not covered in this text.

    If it is necessary to perform calculations with different data types, such as integers and reals, the intrinsic or built-in conversion functions should be used to ensure correct and predictable results. This also allows the programming greater control of when types are converted. In very complex calculations, this would help ensure clarity and address precision issues. Further recommendations to address highly precise calculations are not addressed in this text.


    This page titled 5.5: Mixed Mode is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?