Cpp round to int

round

Returns the integral value that is nearest to x , with halfway cases rounded away from zero.

Additional overloads are provided in this header ( ) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).

Parameters

Return Value

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* round vs floor vs ceil vs trunc */ /* printf */ /* round, floor, ceil, trunc */ int main () < const char * format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n"; printf ("value\tround\tfloor\tceil\ttrunc\n"); printf ("-----\t-----\t-----\t----\t-----\n"); printf (format, 2.3,round( 2.3),floor( 2.3),ceil( 2.3),trunc( 2.3)); printf (format, 3.8,round( 3.8),floor( 3.8),ceil( 3.8),trunc( 3.8)); printf (format, 5.5,round( 5.5),floor( 5.5),ceil( 5.5),trunc( 5.5)); printf (format,-2.3,round(-2.3),floor(-2.3),ceil(-2.3),trunc(-2.3)); printf (format,-3.8,round(-3.8),floor(-3.8),ceil(-3.8),trunc(-3.8)); printf (format,-5.5,round(-5.5),floor(-5.5),ceil(-5.5),trunc(-5.5)); return 0; >
 value round floor ceil trunc ----- ----- ----- ---- ----- 2.3 2.0 2.0 3.0 2.0 3.8 4.0 3.0 4.0 3.0 5.5 6.0 5.0 6.0 5.0 -2.3 -2.0 -3.0 -2.0 -2.0 -3.8 -4.0 -4.0 -3.0 -3.0 -5.5 -6.0 -6.0 -5.0 -5.0 

See also

floor Round down value (function) ceil Round up value (function) trunc Truncate value (function) nearbyint Round to nearby integral value (function) rint Round to integral value (function)

Читайте также:  Class program in php

Источник

round , roundf , roundl

Rounds a floating-point value to the nearest integer value.

Syntax

double round( double x ); float round( float x ); // C++ only long double round( long double x ); // C++ only float roundf( float x ); long double roundl( long double x ); #define round(X) // Requires C11 or higher 

Parameters

x
The floating-point value to round.

Return value

The round functions return a floating-point value that represents the nearest integer to x . Halfway values are rounded away from zero, regardless of the setting of the floating-point rounding mode. There’s no error return.

Input SEH exception _matherr exception
± QNaN, IND none _DOMAIN

Remarks

Because C++ allows overloading, you can call overloads of round that take and return float and long double values. In a C program, unless you’re using the macro to call this function, round always takes and returns a double .

If you use the round macro from , the type of the argument determines which version of the function is selected. See Type-generic math for details.

By default, this function’s global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Routine Required header
round , roundf , roundl
round macro

For more compatibility information, see Compatibility.

Example

// Build with: cl /W3 /Tc // This example displays the rounded // results of floating-point values #include #include int main() < printf("===== Round a float\n\n"); float floatValue = 2.4999999f; // float stores a value close to, but not exactly equal to, the initializer below. floatValue will contain 2.5 because it is the closest single precision value printf("roundf(%.1000g) is %.1000g\n", floatValue, roundf(floatValue)); printf("roundf(%.1000g) is %.1000g\n", -floatValue, roundf(-floatValue)); // double stores a value close to, but not exactly equal to, the initializer below. The closest double value is just slightly larger. double doubleValue = 2.4999999; printf("\n===== Round a double\n\n"); printf("round(%.1000g) is %.1000g\n", doubleValue, round(doubleValue)); printf("round(%.1000g) is %.1000g\n", -doubleValue, round(-doubleValue)); // long double stores a value close to, but not exactly equal to, the initializer below. The closest long double value is just slightly larger. long double longDoubleValue = 2.4999999L; printf("\n===== Round a long double\n\n"); printf("roundl(%.1000g) is %.1000g\n", longDoubleValue, roundl(longDoubleValue)); printf("roundl(%.1000g) is %.1000g\n", -longDoubleValue, roundl(-longDoubleValue)); return 0; >
===== Round a float roundf(2.5) is 3 roundf(-2.5) is -3 ===== Round a double round(2.499999900000000163657887242152355611324310302734375) is 2 round(-2.499999900000000163657887242152355611324310302734375) is -2 ===== Round a long double roundl(2.499999900000000163657887242152355611324310302734375) is 2 roundl(-2.499999900000000163657887242152355611324310302734375) is -2 

Источник

std:: round, std:: roundf, std:: roundl, std:: lround, std:: lroundf, std:: lroundl, std:: llround, std:: llroundf

1-3) Computes the nearest integer value to num (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode. The library provides overloads of std::round for all cv-unqualified floating-point types as the type of the parameter num . (since C++23)

4-9) Computes the nearest integer value to num (in integer format), rounding halfway cases away from zero, regardless of the current rounding mode. The library provides overloads of std::lround and std::llround for all cv-unqualified floating-point types as the type of the parameter num . (since C++23)

Contents

[edit] Parameters

[edit] Return value

If no errors occur, the nearest integer value to num , rounding halfway cases away from zero, is returned.

If a domain error occurs, an implementation-defined value is returned.

[edit] Error handling

Errors are reported as specified in math_errhandling .

If the result of std::lround or std::llround is outside the range representable by the return type, a domain error or a range error may occur.

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

  • The current rounding mode has no effect.
  • If num is ±∞, it is returned, unmodified
  • If num is ±0, it is returned, unmodified
  • If num is NaN, NaN is returned
  • FE_INEXACT is never raised
  • The current rounding mode has no effect.
  • If num is ±∞, FE_INVALID is raised and an implementation-defined value is returned
  • If the result of the rounding is outside the range of the return type, FE_INVALID is raised and an implementation-defined value is returned
  • If num is NaN, FE_INVALID is raised and an implementation-defined value is returned.

[edit] Notes

FE_INEXACT may be (but is not required to be) raised by std::round when rounding a non-integer finite value.

The largest representable floating-point values are exact integers in all standard floating-point formats, so std::round never overflows on its own; however the result may overflow any integer type (including std::intmax_t ), when stored in an integer variable.

POSIX specifies that all cases where std::lround or std::llround raise FE_INEXACT are domain errors.

The double version of std::round behaves as if implemented as follows:

#include #include #pragma STDC FENV_ACCESS ON double round(double x) { std::fenv_t save_env; std::feholdexcept(&save_env); double result = std::rint(x); if (std::fetestexcept(FE_INEXACT)) { auto const save_round = std::fegetround(); std::fesetround(FE_TOWARDZERO); result = std::rint(std::copysign(0.5 + std::fabs(x), x)); std::fesetround(save_round); } std::feupdateenv(&save_env); return result; }

The additional overloads are not required to be provided exactly as (A-C) . They only need to be sufficient to ensure that for their argument num of integer type:

  • std :: round ( num ) has the same effect as std :: round ( static_cast < double >( num ) ) .
  • std :: lround ( num ) has the same effect as std :: lround ( static_cast < double >( num ) ) .
  • std :: llround ( num ) has the same effect as std :: llround ( static_cast < double >( num ) ) .

[edit] Example

#include #include #include #include // #pragma STDC FENV_ACCESS ON int main() { // round std::cout  "round(+2.3) = "  std::round(2.3)  " round(+2.5) = "  std::round(2.5)  " round(+2.7) = "  std::round(2.7)  '\n'  "round(-2.3) = "  std::round(-2.3)  " round(-2.5) = "  std::round(-2.5)  " round(-2.7) = "  std::round(-2.7)  '\n'; std::cout  "round(-0.0) = "  std::round(-0.0)  '\n'  "round(-Inf) = "  std::round(-INFINITY)  '\n'; // lround std::cout  "lround(+2.3) = "  std::lround(2.3)  " lround(+2.5) = "  std::lround(2.5)  " lround(+2.7) = "  std::lround(2.7)  '\n'  "lround(-2.3) = "  std::lround(-2.3)  " lround(-2.5) = "  std::lround(-2.5)  " lround(-2.7) = "  std::lround(-2.7)  '\n'; std::cout  "lround(-0.0) = "  std::lround(-0.0)  '\n'  "lround(-Inf) = "  std::lround(-INFINITY)  '\n'; // error handling std::feclearexcept(FE_ALL_EXCEPT); std::cout  "std::lround(LONG_MAX+1.5) = "  std::lround(LONG_MAX + 1.5)  '\n'; if (std::fetestexcept(FE_INVALID)) std::cout  " FE_INVALID was raised\n"; }
round(+2.3) = 2 round(+2.5) = 3 round(+2.7) = 3 round(-2.3) = -2 round(-2.5) = -3 round(-2.7) = -3 round(-0.0) = -0 round(-Inf) = -inf lround(+2.3) = 2 lround(+2.5) = 3 lround(+2.7) = 3 lround(-2.3) = -2 lround(-2.5) = -3 lround(-2.7) = -3 lround(-0.0) = 0 lround(-Inf) = -9223372036854775808 std::lround(LONG_MAX+1.5) = -9223372036854775808 FE_INVALID was raised

Источник

Round a Double to an Int in C++

Round a Double to an Int in C++

  1. Use the round() Function for Double to Int Rounding
  2. Use the lround() Function to Convert Double to the Nearest Integer
  3. Use the trunc() Function for Double to Int Rounding

This article will explain several methods of how to round a double to an integer in C++.

Use the round() Function for Double to Int Rounding

The round() function is defined in the header. It can compute the nearest integer value of the argument that the user passes. Halfway cases are rounded away from zero, and the returned value is the same type as the argument. In the following example, we initialize an arbitrary vector of doubles and output the round() function for each element.

#include #include #include  using std::cout; using std::endl; using std::vector;  int main()   vectordouble> dfloats -3.5, -21.1, -1.99, 0.129, 2.5, 3.111>;   for (auto &item : dfloats)   cout  <"round("   <") = "     >   return EXIT_SUCCESS; > 
round(-3.5) = -4 round(-21.1) = -21 round(-1.99) = -2 round(0.129) = 0 round(2.5) = 3 round(3.111) = 3 

Use the lround() Function to Convert Double to the Nearest Integer

Alternatively, we can use the lround() function, which behaves almost identically as the round() with one difference: it returns the value in a long integer format. Note that, for both functions, the return value can be unexpected, caused by different rounding errors, which are ubiquitous. Corresponding scenarios should be handled as specified in the documentation here.

#include #include #include  using std::cout; using std::endl; using std::vector;  int main()   vectordouble> dfloats -3.5, -21.1, -1.99, 0.129, 2.5, 3.111>;   for (auto &item : dfloats)   cout  <"round("   <") = "     >   return EXIT_SUCCESS; > 

Use the trunc() Function for Double to Int Rounding

trunc() is another useful function from the header, which computes the nearest integer not greater than the number passed as an argument. trunc() calculates the return value in the floating-point format as round() . Note that these two functions don’t take any effect from the current rounding mode. Error handling cases are described on the manual page.

#include #include #include  using std::cout; using std::endl; using std::vector;  int main()   vectordouble> dfloats -3.5, -21.1, -1.99, 0.129, 2.5, 3.111>;   for (auto &item : dfloats)   cout  <"round("   <") = "     >   return EXIT_SUCCESS; > 
round(-3.5) = -3 round(-21.1) = -21 round(-1.99) = -1 round(0.129) = 0 round(2.5) = 2 round(3.111) = 3 

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

Related Article — C++ Double

Источник

Оцените статью