Buenas días estimado lector, hace unos días dando clases apareció un problema, con interesante solución, yu es acerca de comparar números flotantes en C. Para quiénes hayan programado en Java, este lenguaje de programación te deja muy fácil el tema de comparación de números, incluso podríamos emplear “trucos” para convertir el numero a String, y compararlo desde ahí.
El mundo de C es un mundo muy interesante, a la vez de complicado, y en esta ocasión dejo un trozo de código donde puedas comparar 2 valores de tipo float en C.¡
#include <stdio.h>
int comparar_flotante(float f1, float f2);
int main()
{
double variable = (0.3 * 3) + 5.1;
printf("%f \n", variable);
int variableInt=comparar_flotante(variable, 6.0);
if (variableInt==1)
{ // 1 es entero y double son de diferente tipo
printf("Es igual a 6 \n ");
}
else
{
printf("Es no igual a 6 \n ");
}
}
int comparar_flotante(float f1, float f2)
{
float precision = 0.00001
;
if (((f1 - precision) < f2) &&
((f1 + precision) > f2))
{
return 1;
}
else
{
return 0;
}
}
Un saludo y gracias por leer este artículo

NUESTRAS ÚLTIMAS PUBLICACIONES
- LA IMPORTANCIA DE LAS CERTIFICACIONES OFICIALES DE IDIOMAS DE LAS ESCUELAS OFICIALES DE IDIOMAS PARA EL ALUMNADO DE SECUNDARIA

- CURSO INTENSIVO DE SMR: SISTEMAS MICROINFORMÁTICOS Y REDES

- ANÁLISIS DE LOS RESULTADOS DE ESPAÑA EN PISA 2025 (PUBLICADOS EN 2026): EVOLUCIÓN DURANTE LOS ÚLTIMOS VEINTE AÑOS – PARTE 2.

- CURSOS INTENSIVOS ASIR: ADMINISTRACIÓN DE SISTEMAS INFORMÁTICOS EN RED

- ANÁLISIS DE LOS RESULTADOS DE ESPAÑA EN PISA 2025 (PUBLICADOS EN 2026): UNA REFLEXIÓN SOBRE LAS COMPETENCIAS CLAVE PARA EL SIGLO XXI – PARTE 1.

- CURSOS INTENSIVOS PARA LAS OPOSICIONES AL CUERPO DE INFORMÁTICOS DEL ESTADO Y LAS ADMINISTRACIONES PÚBLICAS.

- CURSOS INTENSIVOS PARA LAS OPOSICIONES A LAS ADMINISTRACIONES PÚBLICAS

- ACADEMIA SANROQUE CUIDA LAS LETRAS Y LAS HUMANIDADES.

- CLASES DE PSICOLOGÍA, PSICOMETRÍA, PSICOBIOLOGÍA Y OTRAS.

- ACADEMIA SANROQUE: LAS CIENCIAS SON UNA ESPECIALIDAD MOTIVANTE E ILUSIONANTE.

- PRUEBAS DE ACCESO A GRADOS FORMATIVOS

- PRUEBAS DE ACCESO A LA UNIVERSIDAD PÚBLICA O PRIVADA: PAU, PAM25-40-45, PCE, ETC.

Contenido restringido


Comments are closed