banner



How To Round Double In C#

Divers in header <math.h>

float roundf( float arg ) ;

(1) (since C99)

double round( double arg ) ;

(2) (since C99)

long double roundl( long double arg ) ;

(3) (since C99)

Defined in header <tgmath.h>

#define round( arg )

(4) (since C99)

Divers in header <math.h>

long lroundf( float arg ) ;

(5) (since C99)

long lround( double arg ) ;

(half-dozen) (since C99)

long lroundl( long double arg ) ;

(vii) (since C99)

Defined in header <tgmath.h>

#ascertain lround( arg )

(8) (since C99)

Divers in header <math.h>

long long llroundf( bladder arg ) ;

(9) (since C99)

long long llround( double arg ) ;

(10) (since C99)

long long llroundl( long double arg ) ;

(eleven) (since C99)

Defined in header <tgmath.h>

#define llround( arg )

(12) (since C99)

1-3) Computes the nearest integer value to arg (in floating-point format), rounding halfway cases away from nothing, regardless of the current rounding style.

5-seven, ix-11) Computes the nearest integer value to arg (in integer format), rounding halfway cases abroad from zero, regardless of the current rounding mode.

four,eight,12) Type-generic macros: If arg has blazon long double , roundl, lroundl, llroundl is called. Otherwise, if arg has integer type or the type double , round, lround, llround is called. Otherwise, roundf, lroundf, llroundf is called, respectively.

[edit] Parameters

arg - floating point value

[edit] Return value

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

Return value

math-round away zero.svg

Argument

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 lround or llround is outside the range representable by the return blazon, a domain fault or a range fault may occur.

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

For the round, roundf, and roundl role:
  • The current rounding mode has no result.
  • If arg is ±∞, it is returned, unmodified
  • If arg is ±0, it is returned, unmodified
  • If arg is NaN, NaN is returned
For lround and llround families of functions:
  • FE_INEXACT is never raised
  • The electric current rounding mode has no result.
  • If arg is ±∞, FE_INVALID is raised and an implementation-defined value is returned
  • If the result of the rounding is exterior the range of the render type, FE_INVALID is raised and an implementation-divers value is returned
  • If arg is NaN, FE_INVALID is raised and an implementation-divers value is returned

[edit] Notes

FE_INEXACT may be (merely isn't required to be) raised past round when rounding a non-integer finite value.

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

POSIX specifies that all cases where lround or llround raise FE_INVALID are domain errors.

The double version of round behaves as if implemented every bit follows:

            #include <math.h>            double            round(            double            x)            {            return            signbit            (ten)            ?            ceil            (x            -            0.v            )            :            floor            (x            +            0.5            )            ;            }          

[edit] Example

              #include <stdio.h>              #include <math.h>              #include <fenv.h>              #include <limits.h>              // #pragma STDC FENV_ACCESS ON              int              main(              void              )              {              // round              printf              (              "round(+two.3) = %+.1f  ", round(              2.three              )              )              ;              printf              (              "circular(+two.5) = %+.1f  ", circular(              2.v              )              )              ;              printf              (              "circular(+two.vii) = %+.1f\n", circular(              2.7              )              )              ;              printf              (              "round(-ii.three) = %+.1f  ", round(              -              2.iii              )              )              ;              printf              (              "round(-ii.5) = %+.1f  ", round(              -              two.5              )              )              ;              printf              (              "circular(-2.7) = %+.1f\due north", round(              -              2.7              )              )              ;              printf              (              "round(-0.0) = %+.1f\n", round(              -              0.0              )              )              ;              printf              (              "round(-Inf) = %+f\n",   circular(              -INFINITY)              )              ;              // lround              printf              (              "lround(+two.3) = %ld  ", lround(              2.3              )              )              ;              printf              (              "lround(+2.5) = %ld  ", lround(              two.5              )              )              ;              printf              (              "lround(+ii.seven) = %ld\northward", lround(              ii.7              )              )              ;              printf              (              "lround(-2.3) = %ld  ", lround(              -              2.three              )              )              ;              printf              (              "lround(-2.five) = %ld  ", lround(              -              two.5              )              )              ;              printf              (              "lround(-2.7) = %ld\north", lround(              -              ii.7              )              )              ;              printf              (              "lround(-0.0) = %ld\n", lround(              -              0.0              )              )              ;              printf              (              "lround(-Inf) = %ld\due north", lround(              -INFINITY)              )              ;              // FE_INVALID raised              // fault treatment              feclearexcept              (              FE_ALL_EXCEPT              )              ;              printf              (              "lround(LONG_MAX+1.five) = %ld\due north", lround(              LONG_MAX              +              ane.5              )              )              ;              if              (              fetestexcept              (              FE_INVALID              )              )              puts              (              "    FE_INVALID was raised"              )              ;              }            

Possible output:

circular(+2.3) = +ii.0  circular(+2.5) = +3.0  round(+ii.vii) = +iii.0 round(-2.3) = -2.0  circular(-2.5) = -3.0  round(-two.seven) = -3.0 round(-0.0) = -0.0 round(-Inf) = -inf lround(+two.3) = 2  lround(+2.v) = iii  lround(+ii.vii) = 3 lround(-2.3) = -two  lround(-ii.v) = -iii  lround(-two.7) = -3 lround(-0.0) = 0 lround(-Inf) = -9223372036854775808 lround(LONG_MAX+1.5) = -9223372036854775808     FE_INVALID was raised

[edit] References

  • C17 standard (ISO/IEC 9899:2018):
  • 7.12.nine.vi The round functions (p: 184)
  • vii.12.nine.7 The lround and llround functions (p: 184-185)
  • vii.25 Type-generic math <tgmath.h> (p: 272-273)
  • F.10.vi.half-dozen The circular functions (p: 384)
  • F.10.vi.7 The lround and llround functions (p: 385)
  • C11 standard (ISO/IEC 9899:2011):
  • 7.12.ix.6 The round functions (p: 253)
  • seven.12.9.vii The lround and llround functions (p: 253)
  • seven.25 Type-generic math <tgmath.h> (p: 373-375)
  • F.ten.half-dozen.half-dozen The circular functions (p: 527)
  • F.10.six.7 The lround and llround functions (p: 528)
  • C99 standard (ISO/IEC 9899:1999):
  • vii.12.9.6 The round functions (p: 233)
  • 7.12.9.7 The lround and llround functions (p: 234)
  • 7.22 Blazon-generic math <tgmath.h> (p: 335-337)
  • F.9.six.6 The round functions (p: 464)
  • F.nine.half-dozen.7 The lround and llround functions (p: 464)

[edit] Run into likewise

Source: https://en.cppreference.com/w/c/numeric/math/round

0 Response to "How To Round Double In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel