From f5b60e233422c491319fa0c031c445b8a304018e Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20A=2E=20Holm?= Date: Sat, 13 Jul 2013 19:51:04 +0200 Subject: [PATCH] roundgpx: Behave just like gpst, truncate decimals instead of rounding See the famous commit 2349bfd ("gpst: gpst_entry() truncates numbers instead of rounding", 2013-06-23 19:20:50 +0200) for more information. 10b836dc-ebe5-11e2-a667-001f3b596ec9 --- roundgpx | 16 ++++++++++++---- tests/roundgpx/files/rounding.gpx | 4 ++-- tests/roundgpx/roundgpx.t | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/roundgpx b/roundgpx index 14a34f4..7a8cf61 100755 --- a/roundgpx +++ b/roundgpx @@ -50,11 +50,11 @@ if ($Opt{'version'}) { while (<>) { if (/(.*?<(?:trkpt|rtept|wpt)\s+.*?)"(.*?)"(.*?)"(.*?)"(.*)$/) { - my ($num1, $num2) = (sprintf("%.6f", $2), sprintf("%.6f", $4)); - print("$1\"" . num_expand($num1) . "\"$3\"" . num_expand($num2) . "\"$5\n"); + my ($num1, $num2) = (trunc_number($2, 6), trunc_number($4, 6)); + print("$1\"$num1\"$3\"$num2\"$5\n"); } elsif (/^(.*?)(-?[\d\.]+)(<\/ele>.*)$/) { - my $ele = sprintf("%.3f", $2); - print($1 . num_expand($ele) . "$3\n"); + my $ele = trunc_number($2, 3); + print("$1$ele$3\n"); } else { print; } @@ -85,6 +85,14 @@ sub num_expand { # }}} } # num_expand() +sub trunc_number { + # Truncate decimals of floating point number {{{ + my ($num, $scale) = @_; + $num = int($num * (10**$scale)) / 10**$scale; + return(num_expand($num)); + # }}} +} # trunc_number() + sub print_version { # Print program version {{{ print("$progname v$VERSION\n"); diff --git a/tests/roundgpx/files/rounding.gpx b/tests/roundgpx/files/rounding.gpx index b18830b..ea7fc86 100644 --- a/tests/roundgpx/files/rounding.gpx +++ b/tests/roundgpx/files/rounding.gpx @@ -15,11 +15,11 @@ 3.00000009 - + 0.00150002 - + diff --git a/tests/roundgpx/roundgpx.t b/tests/roundgpx/roundgpx.t index 17d1840..0b88caf 100755 --- a/tests/roundgpx/roundgpx.t +++ b/tests/roundgpx/roundgpx.t @@ -136,7 +136,7 @@ $gpx_header - 0.002 + 0.001 -- 2.11.4.GIT