Namespaces
Variants

nearbyint, nearbyintf, nearbyintl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
nearbyint
(C99)
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
Definido en el encabezado <math.h>
float nearbyintf ( float arg ) ;
(1) (desde C99)
double nearbyint ( double arg ) ;
(2) (desde C99)
long double nearbyintl ( long double arg ) ;
(3) (desde C99)
Definido en el encabezado <tgmath.h>
#define nearbyint( arg )
(4) (desde C99)
1-3) Redondea el argumento de punto flotante arg a un valor entero en formato de punto flotante, utilizando el modo de redondeo actual .
4) Macro genérico de tipos: Si arg tiene tipo long double , nearbyintl es llamado. De lo contrario, si arg tiene tipo entero o el tipo double , nearbyint es llamado. De lo contrario, nearbyintf es llamado, respectivamente.

Contenidos

Parámetros

arg - valor de punto flotante

Valor de retorno

El valor entero más cercano a arg , de acuerdo con el modo de redondeo actual , es devuelto.

Manejo de errores

Esta función no está sujeta a ninguno de los errores especificados en math_errhandling .

Si la implementación soporta aritmética de punto flotante IEEE (IEC 60559),

  • FE_INEXACT nunca se activa.
  • Si arg es ±∞, se devuelve sin modificaciones.
  • Si arg es ±0, se devuelve sin modificaciones.
  • Si arg es NaN, se devuelve NaN.

Notas

La única diferencia entre nearbyint y rint es que nearbyint nunca genera FE_INEXACT .

Los valores de punto flotante más grandes representables son enteros exactos en todos los formatos estándar de punto flotante, por lo que nearbyint nunca desborda por sí mismo; sin embargo, el resultado puede desbordar cualquier tipo entero (incluyendo intmax_t ), cuando se almacena en una variable entera.

Si el modo de redondeo actual es FE_TONEAREST , esta función redondea al par en casos intermedios (como rint , pero a diferencia de round ).

Ejemplo

#include <fenv.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
// #pragma STDC FENV_ACCESS ON
    fesetround(FE_TONEAREST);
    printf("rounding to nearest:\nnearbyint(+2.3) = %+.1f  ", nearbyint(2.3));
    printf("nearbyint(+2.5) = %+.1f  ", nearbyint(2.5));
    printf("nearbyint(+3.5) = %+.1f\n", nearbyint(3.5));
    printf("nearbyint(-2.3) = %+.1f  ", nearbyint(-2.3));
    printf("nearbyint(-2.5) = %+.1f  ", nearbyint(-2.5));
    printf("nearbyint(-3.5) = %+.1f\n", nearbyint(-3.5));
    fesetround(FE_DOWNWARD);
    printf("rounding down: \nnearbyint(+2.3) = %+.1f  ", nearbyint(2.3));
    printf("nearbyint(+2.5) = %+.1f  ", nearbyint(2.5));
    printf("nearbyint(+3.5) = %+.1f\n", nearbyint(3.5));
    printf("nearbyint(-2.3) = %+.1f  ", nearbyint(-2.3));
    printf("nearbyint(-2.5) = %+.1f  ", nearbyint(-2.5));
    printf("nearbyint(-3.5) = %+.1f\n", nearbyint(-3.5));
    printf("nearbyint(-0.0) = %+.1f\n", nearbyint(-0.0));
    printf("nearbyint(-Inf) = %+.1f\n", nearbyint(-INFINITY));
}

Salida:

rounding to nearest:
nearbyint(+2.3) = +2.0  nearbyint(+2.5) = +2.0  nearbyint(+3.5) = +4.0
nearbyint(-2.3) = -2.0  nearbyint(-2.5) = -2.0  nearbyint(-3.5) = -4.0
rounding down:
nearbyint(+2.3) = +2.0  nearbyint(+2.5) = +2.0  nearbyint(+3.5) = +3.0
nearbyint(-2.3) = -3.0  nearbyint(-2.5) = -3.0  nearbyint(-3.5) = -4.0
nearbyint(-0.0) = -0.0
nearbyint(-Inf) = -inf

Referencias

  • Estándar C23 (ISO/IEC 9899:2024):
  • 7.12.9.3 Las funciones nearbyint (p: TBD)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: TBD)
  • F.10.6.3 Las funciones nearbyint (p: TBD)
  • Estándar C17 (ISO/IEC 9899:2018):
  • 7.12.9.3 Las funciones nearbyint (p: TBD)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: TBD)
  • F.10.6.3 Las funciones nearbyint (p: TBD)
  • Estándar C11 (ISO/IEC 9899:2011):
  • 7.12.9.3 Las funciones nearbyint (p: 251-252)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: 373-375)
  • F.10.6.3 Las funciones nearbyint (p: 526)
  • Estándar C99 (ISO/IEC 9899:1999):
  • 7.12.9.3 Las funciones nearbyint (p: 232)
  • 7.22 Matemáticas genéricas de tipos <tgmath.h> (p: 335-337)
  • F.9.6.3 Las funciones nearbyint (p: 463)

Véase también

(C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99)
redondea a un entero usando el modo de redondeo actual con
excepción si el resultado difiere
(función)
(C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99)
redondea al entero más cercano, redondeando lejos de cero en casos intermedios
(función)
obtiene o establece la dirección de redondeo
(función)