English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Utilizzo e esempio della funzione llround() in C++

库函数 <cmath>C++

La funzione llround() di C++ arrotonda i valori a virgola mobile al numero intero più vicino. Il valore restituito è di tipo long long int. È simile alround()La funzione, ma restituisce long long int, mentre lround restituisce long int.

Prospettiva llround() [dalla versione C++ 11]

long long int llround(double x);
long long int llround(float x);
long long int llround(long double x);
long long int llround(T x); // per interi

La funzione llround() accetta un singolo parametro e restituisce un valore di tipo long long int. Questa funzione è disponibile<cmath>Definito nel file di intestazione.

Parametro llround()

La funzione llround() arrotonda un singolo valore di parametro.

Valore di llround()

La funzione llround() restituisce il valore intero più vicino a x, arrotondando al zero nei casi intermedi. Il valore restituito è di tipo long long int.

Esempio 1: Come funziona llround() in C++?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{   
    long long int result;
    double x = 11.16;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    x = 13.87;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = 50.5;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = -11.16;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    x = -13.87;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = -50.5;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    return 0;
}

运行该程序时,输出为:

llround(11.16) = 11
llround(13.87) = 14
llround(50.5) = 51
llround(-11.16) = -11
llround(-13.87) = -14
llround(-50.5) = -51

Esempio 2: La funzione llround() di tipo intero

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int x = 15;
    long long int result;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    return 0;
}

运行该程序时,输出为:

llround(15) = 15

对于整数值,应用函数llround将返回与输入相同的值。所以它在实际中并不常用来表示整数值。

库函数 <cmath>C++