Skip to main content

Section 2.2 Numeric Data

Numeric C++ data types include int for integer, float for floating point, double for double precision floating point.
The standard arithmetic operations, +, -, *, and / are used with optional parentheses to force the order of operations away from normal operator precedence.
In Python we can use // to get integer division. In C++, we declare all data types. When two integers are divided in C++, the integer portion of the quotient is returned and the fractional portion is removed. i.e. When two integers are divided, integer division is used. To get the whole quotient, declaring one of the numbers as a float will convert the entire result into floating point.
Exponentiation in C++ is done using pow() from the cmath library and the remainder (modulo) operator is done with %.
Run the following code to see that you understand each result.
When declaring numeric variables in C++, modifiers like short, long, and unsigned can optionally be used to help to ensure space is used as efficiently as possible.

Reading Questions Reading Questions

1.

what is the result of dividing 3/2 in C++?
  • Right! This is like ``3//2`` in Python.
  • 1.5
  • No. Integer division is used. Try again.
  • No. Integer division is used. Try again.
  • A run-time error will occur.
  • No, C++ generally will try to do as you ask.
  • none of the above
  • One of the above is correct.

2.

How do I raise 4 to 5th power in C++?
  • ``4**5``
  • No, ``**`` is used in Python, not C++.
  • ``5**4``
  • No, ``**`` is used in Python, not C++, and the operators are reversed.
  • ``4^5``
  • No. The ``^`` is a valid operator in C++, but it does something else.
  • ``pow(4, 5)``
  • You got it! Remember the cmath library will need to be included for pow() to work.
You have attempted 1 of 4 activities on this page.