diff options
-rw-r--r-- | libmateweather/weather-priv.h | 6 | ||||
-rw-r--r-- | libmateweather/weather.c | 45 |
2 files changed, 38 insertions, 13 deletions
diff --git a/libmateweather/weather-priv.h b/libmateweather/weather-priv.h index c0c0944..08c828e 100644 --- a/libmateweather/weather-priv.h +++ b/libmateweather/weather-priv.h @@ -122,9 +122,9 @@ const gchar * weather_conditions_string (WeatherConditions cond); /* Units conversions and names */ -#define TEMP_F_TO_C(f) (((f) - 32.0) * 0.555556) -#define TEMP_F_TO_K(f) (TEMP_F_TO_C (f) + 273.15) -#define TEMP_C_TO_F(c) (((c) * 1.8) + 32.0) +#define TEMP_F_TO_C(f) (((f) - 32.0) * (5.0/9.0)) +#define TEMP_F_TO_K(f) ((f + 459.67) * (5.0/9.0)) +#define TEMP_C_TO_F(c) (((c) * (9.0/5.0)) + 32.0) #define WINDSPEED_KNOTS_TO_KPH(knots) ((knots) * 1.851965) #define WINDSPEED_KNOTS_TO_MPH(knots) ((knots) * 1.150779) diff --git a/libmateweather/weather.c b/libmateweather/weather.c index c63877f..0572f2e 100644 --- a/libmateweather/weather.c +++ b/libmateweather/weather.c @@ -26,6 +26,7 @@ #include <string.h> #include <ctype.h> #include <math.h> +#include <fenv.h> #ifdef HAVE_VALUES_H #include <values.h> @@ -752,7 +753,7 @@ weather_info_get_conditions (WeatherInfo *info) } static const gchar * -temperature_string (gfloat temp_f, TempUnit to_unit, gboolean want_round) +temperature_string (gdouble temp, TempUnit to_unit, gboolean want_round) { static gchar buf[100]; @@ -760,28 +761,52 @@ temperature_string (gfloat temp_f, TempUnit to_unit, gboolean want_round) case TEMP_UNIT_FAHRENHEIT: if (!want_round) { /* Translators: This is the temperature in degrees Fahrenheit (\302\260 is U+00B0 DEGREE SIGN) */ - g_snprintf (buf, sizeof (buf), _("%.1f \302\260F"), temp_f); + g_snprintf (buf, sizeof (buf), _("%.1f \302\260F"), temp); } else { - /* Translators: This is the temperature in degrees Fahrenheit (\302\260 is U+00B0 DEGREE SIGN) */ - g_snprintf (buf, sizeof (buf), _("%d \302\260F"), (int)floor (temp_f + 0.5)); + const int range_problem = FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW; + gdouble temp_r; + + feclearexcept(range_problem); + temp_r = round (temp); + if (fetestexcept(range_problem)) + g_snprintf (buf, sizeof (buf), _("n/a")); + else + /* Translators: This is the temperature in degrees Fahrenheit (\302\260 is U+00B0 DEGREE SIGN) */ + g_snprintf (buf, sizeof (buf), _("%d \302\260F"), (int)temp_r); } break; case TEMP_UNIT_CENTIGRADE: if (!want_round) { /* Translators: This is the temperature in degrees Celsius (\302\260 is U+00B0 DEGREE SIGN) */ - g_snprintf (buf, sizeof (buf), _("%.1f \302\260C"), TEMP_F_TO_C (temp_f)); + g_snprintf (buf, sizeof (buf), _("%.1f \302\260C"), TEMP_F_TO_C (temp)); } else { - /* Translators: This is the temperature in degrees Celsius (\302\260 is U+00B0 DEGREE SIGN) */ - g_snprintf (buf, sizeof (buf), _("%d \302\260C"), (int)floor (TEMP_F_TO_C (temp_f) + 0.5)); + const int range_problem = FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW; + gdouble temp_r; + + feclearexcept(range_problem); + temp_r = round (TEMP_F_TO_C (temp)); + if (fetestexcept(range_problem)) + g_snprintf (buf, sizeof (buf), _("n/a")); + else + /* Translators: This is the temperature in degrees Celsius (\302\260 is U+00B0 DEGREE SIGN) */ + g_snprintf (buf, sizeof (buf), _("%d \302\260C"), (int)temp_r); } break; case TEMP_UNIT_KELVIN: if (!want_round) { /* Translators: This is the temperature in kelvin */ - g_snprintf (buf, sizeof (buf), _("%.1f K"), TEMP_F_TO_K (temp_f)); + g_snprintf (buf, sizeof (buf), _("%.1f K"), TEMP_F_TO_K (temp)); } else { - /* Translators: This is the temperature in kelvin */ - g_snprintf (buf, sizeof (buf), _("%d K"), (int)floor (TEMP_F_TO_K (temp_f))); + const int range_problem = FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW; + gdouble temp_r; + + feclearexcept(range_problem); + temp_r = round (TEMP_F_TO_K (temp)); + if (fetestexcept(range_problem)) + g_snprintf (buf, sizeof (buf), _("n/a")); + else + /* Translators: This is the temperature in kelvin */ + g_snprintf (buf, sizeof (buf), _("%d K"), (int)temp_r); } break; |