From 08782a5b99f74b5825dc10b52849d5a25bdfc869 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20A=2E=20Holm?= Date: Thu, 25 Jul 2013 18:15:05 +0200 Subject: [PATCH] Revert "roundgpx: Behave just like gpst, truncate decimals instead of rounding" This reverts commit f5b60e233422c491319fa0c031c445b8a304018e. Can't use truncate because floating point sucks. Things like lon=4.8996 is changed to 4.899599. Fuck that shit. b1e7921e-f545-11e2-ac1d-001f3b596ec9 --- roundgpx | 16 ++++------------ tests/roundgpx/files/rounding.gpx | 4 ++-- tests/roundgpx/roundgpx.t | 2 +- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/roundgpx b/roundgpx index 7a8cf61..14a34f4 100755 --- a/roundgpx +++ b/roundgpx @@ -50,11 +50,11 @@ if ($Opt{'version'}) { while (<>) { if (/(.*?<(?:trkpt|rtept|wpt)\s+.*?)"(.*?)"(.*?)"(.*?)"(.*)$/) { - my ($num1, $num2) = (trunc_number($2, 6), trunc_number($4, 6)); - print("$1\"$num1\"$3\"$num2\"$5\n"); + my ($num1, $num2) = (sprintf("%.6f", $2), sprintf("%.6f", $4)); + print("$1\"" . num_expand($num1) . "\"$3\"" . num_expand($num2) . "\"$5\n"); } elsif (/^(.*?)(-?[\d\.]+)(<\/ele>.*)$/) { - my $ele = trunc_number($2, 3); - print("$1$ele$3\n"); + my $ele = sprintf("%.3f", $2); + print($1 . num_expand($ele) . "$3\n"); } else { print; } @@ -85,14 +85,6 @@ 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 ea7fc86..b18830b 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 0b88caf..17d1840 100755 --- a/tests/roundgpx/roundgpx.t +++ b/tests/roundgpx/roundgpx.t @@ -136,7 +136,7 @@ $gpx_header - 0.001 + 0.002 -- 2.11.4.GIT