C + + Digital

Source: Internet
Author: User
Tags mathematical functions natural logarithm square root

C + + mathematical operations

In C + +, in addition to creating a variety of functions, there are a variety of useful functions for you to use. These functions are written in the standard C and C + + libraries, called built-in functions. You can refer to these functions in your program.

C + + contains rich mathematical functions that can be used to operate on a variety of numbers. The following table lists some of the useful built-in math functions in C + +.

To take advantage of these functions, you need to reference the math header file <cmath>.

Serial Number Functions & Descriptions
1 Double cos (double);
The function returns the cosine of the radian angle (double).
2 Double sin (double);
The function returns the sine of the radian angle (double type).
3 Double tan (double);
The function returns the tangent of the radian angle (double type).
4 Double log (double);
The function returns the natural logarithm of the parameter.
5 Double pow (double, double);
Assuming that the first argument is x and the second argument is Y, the function returns the Y-side of X.
6 Double Hypot (double, double);
The function returns the square root of the sum of squares of two parameters, that is, the parameter is a right triangle two right-angled edge, and the function returns the length of the hypotenuse.
7 Double sqrt (double);
The function returns the square root of the parameter.
8 int abs (int);
The function returns the absolute value of an integer.
9 Double fabs (double);
The function returns the absolute value of any one decimal number.
10 Double floor (double);
The function returns a maximum integer less than or equal to the passed-in parameter.
C + + random number

In many cases, you need to generate random numbers. About the random number generator, there are two related functions. One is rand (), and the function returns only a pseudo-random number. You must first call the srand () function before generating a random number.

Here is a simple example of generating a random number. The time () function is used in the instance to get the number of seconds in the system, and a random number is generated by calling the rand () function:

The Srand function is the initialization function of the random number generator.

Prototypes: void Srand (unsigned seed);

usage: It needs to provide a seed that will correspond to a random number, and the same random number will appear if the rand () function behind the same seed is used. such as: Srand (1); use directly to initialize the seed. However, in order to prevent random numbers from repeating every time, the system time is often used to initialize, that is, using the time function to obtain the system times, its return value is from 00:00:00 GMT, January 1, 1970 to the current number of seconds, and then the time_t type data into (unsigned ) to the Srand function, namely: Srand ((unsigned) time (&t)); There is also a regular usage that does not need to define the time_t t variable, namely: Srand ((unsigned) time (NULL)); Pass in a null pointer directly, because your program often does not require parameters to obtain the T data.

Example:

#include <stdlib.h>#include <stdio.h>#include <time.h> /* Use the time function, so have this header file */#defineMAX10 IntMain( void){ IntNumber[MAX] = {0}; IntI;Srand((Unsigned)Time(Null)); /* Seeding Child */ For(I= 0;I<MAX;I++) { Number[i]= Rand ()  %100; /* produces a random integer within 100 */ Printf ( "%d" , Number[i); } Printf ( return 0;                /span>                

C + + numbers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.