Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Numbers

C++ Numbers

Usually, when we need to use numbers, we use primitive data types such as int, short, long, float, and double. The possible values and ranges of these numeric data types were discussed in the C++ Data Types chapter.

Defining Numbers in C++

We have defined numbers in various examples in previous chapters. The following is a comprehensive example of defining various types of numbers in C++:

Example

#include

<

iostream

using

namespace

std

;

int

main

(

)

{

// Number definitions

short

s

;

int

i

;

long

l

;

float

f

;

double

d

;

// Number assignments

s

=

10

;

i

=

1000

;

l

=

1000000

;

f

=

230

.47

;

d

=

30949

.374

;

// Number output

cout

<<

"

short s :

"

<<

s

<<

endl

;

cout

<<

"

int i :

"

<<

i

<<

endl

;

cout

<<

"

long l :

"

<<

l

<<

endl

;

cout

<<

"

float f :

"

<<

f

<<

endl

;

cout

<<

"

double d :

"

<<

d

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



short  s :10

int    i :1000

long   l :1000000

float  f :230.47

double d :30949.4

C++ Math Operations

In C++, besides being able to create various functions, it also includes various useful functions for you to use. These functions are written in the standard C and C++ libraries and are calledbuilt-infunctions. You can reference these functions in your programs.

C++ has a rich set of built-in math functions that operate on various numbers. The table below lists some useful built-in math functions in C++.

To use these functions, you need to include the math header file .

No. Function & Description
1 double cos(double);
This function returns the cosine of an angle in radians (double type).
2 double sin(double);
This function returns the sine of an angle in radians (double type).
3 double tan(double);
This function returns the tangent of an angle in radians (double type).
4 double log(double);
This function returns the natural logarithm of the argument.
5 double pow(double, double);
Assuming the first argument is x and the second is y, this function returns x raised to the power of y.
6 double hypot(double, double);
This function returns the square root of the sum of squares of its two arguments; that is, given the two legs of a right triangle as arguments, it returns the length of the hypotenuse.
7 double sqrt(double);
This function returns the square root of the argument.
8 int abs(int);
This function returns the absolute value of an integer.
9 double fabs(double);
This function returns the absolute value of any floating-point number.
10 double floor(double);
This function returns the largest integer less than or equal to the passed argument.

The following is a simple example of math operations:

Example

#include

<

iostream

#include

<

cmath

using

namespace

std

;

int

main

(

)

{

// Number definitions

short

s

=

10

;

int

i

= -

1000

;

long

l

=

100000

;

float

f

=

230

.47

;

double

d

=

200

.374

;

// Math operations

cout

<<

"

sin(d) :

"

<<

sin

(

d

)

<<

endl

;

cout

<<

"

abs(i) :

"

<<

abs

(

i

)

<<

endl

;

cout

<<

"

floor(d) :

"

<<

floor

(

d

)

<<

endl

;

cout

<<

"

sqrt(f) :

"

<<

sqrt

(

f

)

<<

endl

;

cout

<<

"

pow( d, 2) :

"

<<

pow

(

d

,

2

)

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



sin(d) :-0.634939

abs(i)  :1000

floor(d) :200

sqrt(f) :15.1812

pow( d, 2 ) :40149.7

C++ Random Numbers

In many cases, random numbers need to be generated. There are two related functions for random number generators. One is rand(), which only returns a pseudo-random number. You must call srand() function in Vue 3.

before generating random numbers. The following is a simple example of generating random numbers. The example uses time() a function to get the system time in seconds, and generates random numbers by calling the rand() function:

Example

#include

<

iostream

#include

<

ctime

#include

<

cstdlib

using

namespace

std

;

int

main

(

)

{

int

i

,

j

;

// Set the seed

srand

(

(

unsigned

)

time

(

NULL

)

)

;

/*

Generate 10 random numbers

*/

for

(

i

=

0

;

i

<

10

;

i

++

)

{

// Generate the actual random numbers

j

=

rand

(

)

;

cout

<<

"

Random numbers:

"

<<

j

<<

endl

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result:



随机数: 1748144778

随机数: 630873888

随机数: 2134540646

随机数: 219404170

随机数: 902129458

随机数: 920445370

随机数: 1319072661

随机数: 257938873

随机数: 1256201101

随机数: 580322989


C++ Math Constants

In C++, math constants (such as PI, e, the golden ratio, etc.) are indispensable parts of many algorithms and applications. Although earlier versions of C++ did not directly provide these constants, starting from C++20 the standard library introduced several commonly used math constants and provides more efficient and unified ways to access them.

For more information, refer to:The C++ standard library .

π

Example

#include

#include

int

main

(

)

{

    std

::

cout

<<

"pi: "

<<

std

::

numbers

::

pi

<<

std

::

endl

;

}

Base of the natural logarithm e (Euler's Number)



std::cout << "e: " << std::numbers::e << std::endl;

Golden ratio PHI (Golden Ratio)

std::cout << "phi: " << std::numbers::phi << std::endl;

Example

#include

#include

#include

int

main

(

)

{

    std

::

cout

<<

"pi: "

<<

std

::

numbers

::

pi

<<

std

::

endl

;

    std

::

cout

<<

"e: "

<<

std

::

numbers

::

e

<<

std

::

endl

;

    std

::

cout

<<

"phi: "

<<

std

::

numbers

::

phi

<<

std

::

endl

;

   

return

0

;

}

The output is:

pi: 3.14159

e: 2.71828

phi: 1.61803