π C++ Programming Lesson 3: Arithmetic Operations and the World of Floating Points

π Course Navigation
1γπ’ Arithmetic Operators: Addition, Subtraction, Multiplication, Division, and Modulus Operations2γπ Data Types: Principles and Applications of float and double3γπ’ Next Lesson Preview: bool Type and Relational Operators
1. π’ Arithmetic Operators: Teaching the Program to Calculate
One of the most powerful functions of a computer is performing mathematical calculations. C++ provides arithmetic operators just like those we learned in math class.
(1) Basic Operators: +, -, *, /
These are the most commonly used operators, corresponding to addition, subtraction, multiplication, and division.
|
Operator |
Name |
Example Code |
Result |
|
+ |
Addition |
5 + 3 |
8 |
|
– |
Subtraction |
10 – 4 |
6 |
|
* |
Multiplication |
4 * 7 |
28 |
|
/ |
Division |
15 / 2 |
??? |
Programming Example: Basic Four Arithmetic Operations
#include <iostream>
using namespace std;
int main() {
int a = 15;
int b = 2;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
cout << "a = " << a << ", b = " << b << endl;
cout << "a + b = " << sum << endl;
cout << "a - b = " << difference << endl;
cout << "a * b = " << product << endl;
cout << "a / b = " << quotient << endl; // Guess what the result is?
return 0;
}
Running Result:
a = 15, b = 2
a + b = 18
a - b = 13
a * b = 30
a / b = 7
π‘ Note: When using the int (integer) type for division, the result will discard the decimal part, rather than rounding. The result of 15 / 2 is 7, not 7.5.
(2) Special Operator: % (Modulus / Remainder)
The % operator calculates the remainder after division.
|
Operator |
Name |
Example Code |
Calculation Process |
Result |
|
% |
Modulus / Remainder |
15 % 2 |
15 = 2 * 7 + 1 |
1 |
|
% |
Modulus / Remainder |
10 % 3 |
10 = 3 * 3 + 1 |
1 |
|
% |
Modulus / Remainder |
8 % 4 |
8 = 4 * 2 + 0 |
0 |
Programming Example: Application of Modulus Operation
The modulus operation is very useful, for example, to determine whether a number is odd or even.
#include <iostream>
using namespace std;
int main() {
int number = 7; // If a number divided by 2 has a remainder of 0, it is even; otherwise, it is odd
if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
return 0;
}
Running Result:
7 is odd.
2. π Data Types: Principles and Applications of float and double
In the previous lesson, we learned about the int type, which is used to store integers. But if we need to handle numbers with decimal points (like height 1.75 meters, weight 62.5 kilograms), int is not enough. We need float and double.
(1) What are float and double?
float and double are used to storefloating-point numbers, which are commonly referred to as decimals.
- float: is short for “floating point”, meaningsingle-precision floating-point number.
- double: is short for “double precision”, meaningdouble-precision floating-point number.
(2) Core Differences: Precision and Range
The main differences lie inprecision (the number of decimal places that can be represented) andstorage range (the maximum/minimum values that can be represented).
|
Type |
Chinese Name |
Storage Size (Approx.) |
Significant Digits (Approx.) |
Main Features |
|
float |
Single-precision floating-point number |
4 bytes |
6 – 7 digits |
Occupies less memory, relatively low precision |
|
double |
Double-precision floating-point number |
8 bytes |
15 – 16 digits |
Occupies more memory, higher precision |
π‘ Simple Understanding:
float is like a regular ruler, accurate to the millimeter;
double is like a high-precision caliper, accurate to the micrometer.
In most cases, it is recommended to use double because it has higher precision, can avoid many calculation errors, and on modern computers, the speed of double operations is not much slower than that of float.
(3) Programming Example: Using double for Precise Calculations
Letβs rewrite the previous division example, this time using the double type.
#include <iostream>
using namespace std;
int main() {
// Use double type to store decimal numbers
double a = 15.0;
double b = 2.0;
// Now perform division
double precise_quotient = a / b;
cout << "a = " << a << ", b = " << b << endl;
cout << "a / b = " << precise_quotient << endl; // What will the result be this time?
// Letβs look at another example
double height = 1.75; // Height 1.75 meters
double weight = 62.5; // Weight 62.5 kilograms
double bmi = weight / (height * height); // Calculate BMI index
cout << "\nHeight: " << height << " m" << endl;
cout << "Weight: " << weight << " kg" << endl;
cout << "BMI Index: " << bmi << endl;
return 0;
}
Running Result:
a = 15, b = 2
a / b = 7.5
Height: 1.75 m
Weight: 62.5 kg
BMI Index: 20.4082
π‘ Note: To ensure the result of the operation is a floating-point number, we wrote one or both operands in decimal form (like 15.0 and 2.0), rather than integers 15 and 2.
3. π’ Next Lesson Preview: bool Type and Relational Operators
In this lesson, we learned how to make the program perform mathematical calculations and how to store and process decimals.
In the next lesson, we will learn:
- bool Type: A type that has only two values, true and false, which is the basis for logical judgments.
- Relational Operators: Such as == (equal), != (not equal), > (greater than), < (less than), etc., used to compare the size or equality of two values.
This knowledge will enable our programs to make logical judgments, such as “if the score is greater than 60, then display ‘passed'”, which is a key step in writing complex programs!
