Skip to content

Commit ebd9ab4

Browse files
authored
fix(json_render): json_render is not accurate enough for extremely sm… (#6531)
1 parent 376282d commit ebd9ab4

File tree

7 files changed

+81
-20
lines changed

7 files changed

+81
-20
lines changed

.github/workflows/osrm-backend.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,9 @@ jobs:
376376
uses: actions/cache@v3
377377
with:
378378
path: ${{github.workspace}}/test/cache
379-
key: v3-test-${{ matrix.name }}-${{ github.sha }}
379+
key: v4-test-${{ matrix.name }}-${{ github.sha }}
380380
restore-keys: |
381-
v3-test-${{ matrix.name }}-
381+
v4-test-${{ matrix.name }}-
382382
383383
- name: Prepare environment
384384
run: |

features/testbot/annotations.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,4 @@ Feature: Annotations
115115

116116
When I route I should get
117117
| from | to | route | a:speed | a:distance | a:duration | a:nodes |
118-
| a | c | abc,abc | 10:10 | 249.987618946:299.962882039 | 25:30 | 1:2:3 |
118+
| a | c | abc,abc | 10:10 | 249.9876189:299.962882 | 25:30 | 1:2:3 |

features/testbot/matching.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,6 @@ Feature: Basic Map Matching
810810
# These should have the same weights/duration in either direction
811811
When I match I should get
812812
| trace | geometry | a:distance | a:duration | a:weight | duration |
813-
| 2345 | 1.00018,1,1.000314,1 | 14.914666491 | 1.4 | 1.4 | 1.4 |
814-
| 4321 | 1.00027,1,1.000135,1 | 15.025969972 | 1.5 | 1.5 | 1.5 |
813+
| 2345 | 1.00018,1,1.000314,1 | 14.91466649 | 1.4 | 1.4 | 1.4 |
814+
| 4321 | 1.00027,1,1.000135,1 | 15.02596997 | 1.5 | 1.5 | 1.5 |
815815

features/testbot/snap_intersection.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ Feature: Snapping at intersections
570570
| a,f,k | ac,cf,cf,fj,kj,kj | 132.8s | 132.8 |
571571
| k,f | ik,fi,fi | 54.3s | 54.3 |
572572
| f,a | ef,ae,ae | 66.6s | 66.6 |
573-
| k,f,a | kj,fj,fj,ef,ae,ae | 141.399999999s | 141.399999999 |
573+
| k,f,a | kj,fj,fj,ef,ae,ae | 141.4s | 141.4 |
574574

575575
When I request a travel time matrix I should get
576576
| | a | f | k |
@@ -626,4 +626,4 @@ Feature: Snapping at intersections
626626
| a,f,k | ad,df,df,fj,kj,kj | 105.6s | 105.6 |
627627
| k,f | ik,fi,fi | 54.3s | 54.3 |
628628
| f,a | ef,ae,ae | 66.6s | 66.6 |
629-
| k,f,a | ik,fi,fi,ef,ae,ae | 120.899999999s | 120.899999999 |
629+
| k,f,a | ik,fi,fi,ef,ae,ae | 120.9s | 120.9 |

features/testbot/weight.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ Feature: Weight tests
5353

5454
When I route I should get
5555
| waypoints | route | distances | weights | times | a:distance | a:duration | a:weight | a:speed |
56-
| s,t | abc,abc | 20m,0m | 2,0 | 2s,0s | 20.034626629 | 2 | 2 | 10 |
57-
| t,s | abc,abc | 20m,0m | 2,0 | 2s,0s | 20.034626629 | 2 | 2 | 10 |
58-
| s,e | abc,abc | 40m,0m | 3.9,0 | 3.9s,0s | 29.940636463:10.017313314 | 3:0.9 | 3:0.9 | 10:11.1 |
59-
| e,s | abc,abc | 40m,0m | 3.9,0 | 3.9s,0s | 10.017313314:29.940636463 | 0.9:3 | 0.9:3 | 11.1:10 |
56+
| s,t | abc,abc | 20m,0m | 2,0 | 2s,0s | 20.03462663 | 2 | 2 | 10 |
57+
| t,s | abc,abc | 20m,0m | 2,0 | 2s,0s | 20.03462663 | 2 | 2 | 10 |
58+
| s,e | abc,abc | 40m,0m | 3.9,0 | 3.9s,0s | 29.94063646:10.01731331 | 3:0.9 | 3:0.9 | 10:11.1 |
59+
| e,s | abc,abc | 40m,0m | 3.9,0 | 3.9s,0s | 10.01731331:29.94063646 | 0.9:3 | 0.9:3 | 11.1:10 |
6060

6161

6262
Scenario: Step weights -- way_function: fail if no weight or weight_per_meter property

include/util/json_renderer.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <string>
1515
#include <vector>
1616

17+
#include <boost/assert.hpp>
18+
1719
#include <fmt/compile.h>
1820

1921
namespace osrm::util::json
@@ -47,17 +49,12 @@ template <typename Out> struct Renderer
4749

4850
void operator()(const Number &number)
4951
{
52+
// we don't want to print NaN or Infinity
53+
BOOST_ASSERT(std::isfinite(number.value));
5054
// `fmt::memory_buffer` stores first 500 bytes in the object itself(i.e. on stack in this
5155
// case) and then grows using heap if needed
5256
fmt::memory_buffer buffer;
53-
fmt::format_to(std::back_inserter(buffer), FMT_COMPILE("{}"), number.value);
54-
55-
// Truncate to 10 decimal places
56-
size_t decimalpos = std::find(buffer.begin(), buffer.end(), '.') - buffer.begin();
57-
if (buffer.size() > (decimalpos + 10))
58-
{
59-
buffer.resize(decimalpos + 10);
60-
}
57+
fmt::format_to(std::back_inserter(buffer), FMT_COMPILE("{:.10g}"), number.value);
6158

6259
write(buffer.data(), buffer.size());
6360
}

unit_tests/util/json_render.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BOOST_AUTO_TEST_CASE(number_truncating)
1616

1717
// this number would have more than 10 decimals if not truncated
1818
renderer(Number{42.9995999594999399299});
19-
BOOST_CHECK_EQUAL(str, "42.999599959");
19+
BOOST_CHECK_EQUAL(str, "42.99959996");
2020
}
2121

2222
BOOST_AUTO_TEST_CASE(integer)
@@ -27,4 +27,68 @@ BOOST_AUTO_TEST_CASE(integer)
2727
BOOST_CHECK_EQUAL(str, "42");
2828
}
2929

30+
BOOST_AUTO_TEST_CASE(test_json_issue_6531)
31+
{
32+
std::string output;
33+
osrm::util::json::Renderer<std::string> renderer(output);
34+
renderer(0.0000000000017114087924596788);
35+
BOOST_CHECK_EQUAL(output, "1.711408792e-12");
36+
37+
output.clear();
38+
renderer(42.0);
39+
BOOST_CHECK_EQUAL(output, "42");
40+
41+
output.clear();
42+
renderer(42.1);
43+
BOOST_CHECK_EQUAL(output, "42.1");
44+
45+
output.clear();
46+
renderer(42.12);
47+
BOOST_CHECK_EQUAL(output, "42.12");
48+
49+
output.clear();
50+
renderer(42.123);
51+
BOOST_CHECK_EQUAL(output, "42.123");
52+
53+
output.clear();
54+
renderer(42.1234);
55+
BOOST_CHECK_EQUAL(output, "42.1234");
56+
57+
output.clear();
58+
renderer(42.12345);
59+
BOOST_CHECK_EQUAL(output, "42.12345");
60+
61+
output.clear();
62+
renderer(42.123456);
63+
BOOST_CHECK_EQUAL(output, "42.123456");
64+
65+
output.clear();
66+
renderer(42.1234567);
67+
BOOST_CHECK_EQUAL(output, "42.1234567");
68+
69+
output.clear();
70+
renderer(42.12345678);
71+
BOOST_CHECK_EQUAL(output, "42.12345678");
72+
73+
output.clear();
74+
renderer(42.123456789);
75+
BOOST_CHECK_EQUAL(output, "42.12345679");
76+
77+
output.clear();
78+
renderer(0.12345678912345);
79+
BOOST_CHECK_EQUAL(output, "0.1234567891");
80+
81+
output.clear();
82+
renderer(0.123456789);
83+
BOOST_CHECK_EQUAL(output, "0.123456789");
84+
85+
output.clear();
86+
renderer(0.12345678916);
87+
BOOST_CHECK_EQUAL(output, "0.1234567892");
88+
89+
output.clear();
90+
renderer(123456789123456789);
91+
BOOST_CHECK_EQUAL(output, "1.234567891e+17");
92+
}
93+
3094
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)