Floating-Point Numbers in Cpp
Floating-Point Numbers in C++
A. Floating-point data types in C++ (e.g., float, double, long double)
float: Represents single-precision floating-point numbers.
double: Represents double-precision floating-point numbers.
long double: Represents extended-precision floating-point numbers.
B. Precision and range of floating-point data types
float typically has 7 decimal digits of precision and a range of approximately ±3.4e±38.
double offers 15 decimal digits of precision and a range of approximately ±1.7e±308.
long double provides extended precision with varying precision and range depending on the implementation.
C. Declaring and initializing floating-point variables
#include <iostream>
int main() {
float myFloat = 3.14;
double myDouble = 2.71828;
long double myLongDouble = 1.41421356;
std::cout << "myFloat: " << myFloat << std::endl;
std::cout << "myDouble: " << myDouble << std::endl;
std::cout << "myLongDouble: " << myLongDouble << std::endl;
return 0;
}
D. Arithmetic operations with floating-point numbers
#include <iostream>
int main() {
double num1 = 1.234;
double num2 = 5.678;
double sum = num1 + num2;
double difference = num1 - num2
double product = num1 * num2;
double quotient = num1 / num2;
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
std::cout << "Quotient: " << quotient << std::endl;
return 0;
}
Constants and Literals in C++
A. Integer and floating-point literals
Integer literals are represented by whole numbers without any decimal points or exponent notation, such as 42 or -10.
Floating-point literals include a decimal point or an exponent notation to represent fractional or very large/small numbers, such as 3.14 or 1.23e-5.
B. Octal and hexadecimal literals
Octal literals start with a leading 0 followed by digits from 0 to 7, such as 012 (equivalent to decimal 10) or 077 (equivalent to decimal 63).
Hexadecimal literals start with a leading 0x or 0X followed by digits from 0 to 9 and letters from A to F (case-insensitive), such as 0xFF (equivalent to decimal 255) or 0x2A (equivalent to decimal 42).
C. Character literals and escape sequences
Character literals are enclosed in single quotes and represent a single character, such as 'A' or '5'.
Escape sequences are special character combinations used to represent non-printable characters or characters with special meanings, such as ' ' for a newline or ' ' for a tab.
Examples:
#include <iostream>
int main() {
// Integer and floating-point literals
int myInt = 42;
float myFloat = 3.14;
// Octal and hexadecimal literals
int octalValue = 012; // Octal 12, equivalent to decimal 10
int hexValue = 0x2A; // Hexadecimal 2A, equivalent to decimal 42
// Character literals and escape sequences
char myChar = 'A';
char newline = '\n';
std::cout << "myInt: " << myInt << std::endl;
std::cout << "myFloat: " << myFloat << std::endl;
std::cout << "octalValue: " << octalValue << std::endl;
std::cout << "hexValue: " << hexValue << std::endl;
std::cout << "myChar: " << myChar << std::endl;
std::cout << "newLine: " << newLine << "Hello, World!" << std::endl;
return 0;
}
Mathematical Functions in C++
A. Using the math library in C++
To use mathematical functions in C++, you need to include the
The math library provides a wide range of mathematical functions for various calculations.
B. Commonly used mathematical functions
sqrt(x): Calculates the square root of x.
pow(x, y): Raises x to the power of y.
sin(x), cos(x), tan(x): Calculate the trigonometric sine, cosine, and tangent of x (in radians).
exp(x): Calculates the exponential value of x.
log(x), log10(x): Calculate the natural logarithm and base-10 logarithm of x.
abs(x): Returns the absolute value of x.
ceil(x): Rounds x up to the nearest integer.
floor(x): Rounds x down to the nearest integer.
round(x): Rounds x to the nearest integer, rounding to the nearest even number in case of a tie.
min(x, y), max(x, y): Returns the minimum and maximum values between x and y.
Example:
#include <iostream>
#include <cmath>
int main() {
double x = 4.0;
double y = 2.0;
double squareRoot = sqrt(x);
double power = pow(x, y);
double sine = sin(x);
double cosine = cos(x);
double tangent = tan(x);
double exponential = exp(x);
double naturalLog = log(x);
double logBase10= log10(x);
double absoluteValue = abs(-5.0);
double cellValue = ceil(3.7);
double floorValue = floor(3.7);
double roundValue = round(3.5);
double minvalue = min(4.0, 2.0);
double maxValue = max(4.0, 2.0);
std::cout << "Square root: " << squareRoot << std::endl;
std::cout << "Power: " << power << std::endl;
std::cout << "Sine: " << sine << std::endl;
std::cout << "Cosine: " << cosine << std::endl;
std::cout << "Tangent: "<< tangent << std::endl;
std::cout << "Exponential: " << exponential << std::endl;
std::cout << "Natural logarithm: " << naturalLog << std::endl;
std::cout << "Base-10 logarithm: " << logBase10 << std::endl;
std::cout << "Absolute value: " << absoluteValue << std::endl;
std::cout << "Cell: " << cellValue << std::endl;
std::cout << "Floor: " << floorValue << std::endl;
std::cout << "Round: " << roundValue << std::endl;
std::cout << "Minimum value: " << minValue << std::endl;
return 0;
}
Random Numbers in C++
A. Generating random numbers in C++
To generate random numbers in C++, you can use the
The most commonly used random number generator is the std::mt19937 generator.
B. Seeding the random number generator
Before generating random numbers, you need to seed the random number generator using a seed value. The seed initializes the internal state of the random number generator.
You can seed the generator with a constant value or use a random device to obtain a random seed.
C. Generating random numbers within a specific range
To generate random numbers within a specific range, you can use various techniques such as scaling, modulo operation, or distribution classes from the