Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/degrees_converters.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ static gchar *convert_dec_to_dmm(gdouble dec, gchar pos_c, gchar neg_c)
{
gdouble tmp;
gchar sign_c = ' ';
gint val_d;
gdouble val_m;
gint val_d, carry;
gchar val_m[G_ASCII_DTOSTR_BUF_SIZE];
gchar *result = NULL;

if ( dec > 0 )
Expand All @@ -84,12 +84,13 @@ static gchar *convert_dec_to_dmm(gdouble dec, gchar pos_c, gchar neg_c)
tmp = fabs(dec);
val_d = (gint)tmp;

/* Minutes */
val_m = (tmp - val_d) * 60;
/* Round minutes before carrying into degrees. */
g_snprintf ( val_m, sizeof(val_m), "%f", (tmp - val_d) * 60 );
carry = g_str_has_prefix ( val_m, "60" );

/* Format */
result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%f'",
sign_c, val_d, val_m );
/* Skip the leading 6 when the rounded minutes carry. */
result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%s'",
sign_c, val_d + carry, val_m + carry );
return result;
}

Expand All @@ -111,8 +112,8 @@ static gchar *convert_dec_to_dms(gdouble dec, gchar pos_c, gchar neg_c)
{
gdouble tmp;
gchar sign_c = ' ';
gint val_d, val_m;
gdouble val_s;
gint val_d, val_m, carry;
gchar val_s[G_ASCII_DTOSTR_BUF_SIZE];
gchar *result = NULL;

if ( dec > 0 )
Expand All @@ -130,12 +131,14 @@ static gchar *convert_dec_to_dms(gdouble dec, gchar pos_c, gchar neg_c)
tmp = (tmp - val_d) * 60;
val_m = (gint)tmp;

/* Seconds */
val_s = (tmp - val_m) * 60;
/* Round seconds before splitting the carried minutes into degrees and minutes. */
g_snprintf ( val_s, sizeof(val_s), "%.4f", (tmp - val_m) * 60 );
carry = g_str_has_prefix ( val_s, "60" );
val_m += carry;

/* Format */
result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%d'%.4f\"",
sign_c, val_d, val_m, val_s );
/* Skip the leading 6 when the rounded seconds carry. */
result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%d'%s\"",
sign_c, val_d + val_m / 60, val_m % 60, val_s + carry );
return result;
}

Expand Down
18 changes: 18 additions & 0 deletions test/check_degrees_conversions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,22 @@ check_failure ()
check_success 3.5 3.5 3�30 "3�30'0.00"
check_failure 3.5 3.6 3�40 "3�30'1.00"

check_format ()
{
result=$(LC_ALL=C ./degrees_converter "$1") || exit 1
result=$(printf '%s\n' "$result" | cut -d' ' -f6-)
if [ "$result" != "$2" ]
then
echo "$1 -> $result != $2"
exit 1
fi
}

degree=$(printf '\302\260')
check_format 10.1 "N10${degree}6.000000' E10${degree}6.000000' N10${degree}6'0.0000\" E10${degree}6'0.0000\""
check_format 51.3 "N51${degree}18.000000' E51${degree}18.000000' N51${degree}18'0.0000\" E51${degree}18'0.0000\""
check_format -10.1 "S10${degree}6.000000' W10${degree}6.000000' S10${degree}6'0.0000\" W10${degree}6'0.0000\""
check_format 12.999999999 "N13${degree}0.000000' E13${degree}0.000000' N13${degree}0'0.0000\" E13${degree}0'0.0000\""
check_format 12.34567 "N12${degree}20.740200' E12${degree}20.740200' N12${degree}20'44.4120\" E12${degree}20'44.4120\""

exit 0