Number in C ++
Normally, when we work with Number (numeric types), we use the original data types such as int, short, long, float and double, ... Digital data types, values possible and their range of values, discussed in the Data Type chapter in C ++.
Typically, when we work with Number (numeric types), we use the original data types such as int, short, long, float and double, . Numeric data types, possible values and their range of values, discussed in the Data Type chapter in C ++.
Defining Number in C ++
You have seen the definition of numbers in various examples in the previous chapters. Here is a general example to define diverse numeric types in C ++:
#include using namespace std ; int main () { // phan dinh nghia cac so: short s ; int i ; long l ; float f ; double d ; // phep gan cho cac so; s = 10 ; i = 1000 ; l = 1000000 ; f = 230.47 ; d = 30949.374 ; // in cac so; cout << "short s la: " << s << endl ; cout << "int i la: " << i << endl ; cout << "long l la: " << l << endl ; cout << "float f la: " << f << endl ; cout << "double d la: " << d << endl ; return 0 ; }
When the above code is compiled and executed, it gives the following result:
short s la : 10 int i la : 1000 long l la : 1000000 float f la : 230.47 double d la : 30949.4
Math function in C ++
Besides the various functions you can create, C ++ also includes some useful math functions for you to use. These functions are available in standard C and C ++ libraries, and are called built-in functions. These are functions that can be included in your program and then used.
C ++ has a diverse set of mathematical functions that can be implemented on different types of numbers. The table below lists some of the useful math functions available in C ++.
To use these functions, you need to include the header file .
STTHàm & Purpose1 double cos (double);This function returns the cosine of an angle (form a double)
2 double sin (double);This function returns the sine of an angle (form a double)
3 double tan (double);This function returns the tangent of an angle (form a double)
4 double log (double);This function returns the natural logarithm (ln) of that number
5 double pow (double, double);The exponential function with the base is the first double number and the exponent is the second double
6 double hypot (double, double);If you pass the length of the two sides of the triangle (ie, the double numbers), it will return the length of the hypotenuse
7 double sqrt (double);Returns the square root of the double
8 int abs (int);Returns the absolute value of int
9 double fabs (double);Returns the absolute value of any double
10 double floor (double);Find the integer number that is less than or equal to the parameter passed to it
The following example illustrates some math functions in C ++:
#include #include using namespace std ; int main () { // phan dinh nghia cac so: short s = 10 ; int i = - 1000 ; long l = 100000 ; float f = 230.47 ; double d = 200.374 ; // cac hoat dong toan hoc; cout << "sin(d) co gia tri la: " << sin ( d ) << endl ; cout << "abs(i) co gia tri la: " << abs ( i ) << endl ; cout << "floor(d) co gia tri la: " << floor ( d ) << endl ; cout << "sqrt(f) co gia tri la: " << sqrt ( f ) << endl ; cout << "pow( d, 2) co gia tri la: " << pow ( d , 2 ) << endl ; return 0 ; }
Running the above C ++ program will produce the following results:
Random Number in C ++
In some cases, you want to create a random number. There are two functions that can help you do this. The first function is rand () defined in the library: will only return a pseudo random number, which may not change through the program runs. To solve this anomaly, we will use the srand () function in the library.
The following example will generate some random numbers in C ++. For example, use the time () function to get the number of seconds on your System time. The value returned from time is through scrand, you note that the random number is generated before the rand call.
#include #include #include using namespace std ; int main () { int i , j ; srand ( ( unsigned ) time ( NULL ) ); /* tao 10 so ngau nhien. */ for ( i = 0 ; i < 10 ; i ++ ) { j = rand (); cout << " So ngau nhien la : " << j << endl ; } return 0 ; }
Running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Function in C / C ++
Next post: Array (Array) in C / C ++
You should read it
- Object Number in JavaScript
- Round a number in Excel (ROUND function)
- PERMUT function - The function returns the number of permutations of a given number of objects in Excel
- Why is the number 0 on the keypad to the right of the number 9?
- The INT (Function rounds a number down to the nearest integer of a number) in Excel
- The round () function in Python
- SIGN functions in SQL Server
- ROUNDUP function in Excel - Usage and examples
- Fees and how to switch networks keep the number of the network
- How to automatically turn off the strange number calling on iPhone
- DAY function in SQL Server
- Understanding Random Number Generators: The Science Behind Digital Games
Maybe you are interested
21 species of plants and flowers are beautiful but contain deadly poison Compare Vimeo and Youtube We may be able to see the 'second moon' in the sky with the naked eye next May How to add Services to Control Panel in Windows 7, 8 and 10 Candidates can 'rob' Youtube Embed video into simple website with 6 ways