Skip to content

Commit 372fb0c

Browse files
committed
minor refactoring of STL example
1 parent 3843be6 commit 372fb0c

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

examples/stl.cxx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,25 @@ constexpr dsga::vec3 right_handed_normal(const dsga::vec3 &v1, const dsga::vec3
5555

5656
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5757

58-
// iostream output operator for STL ASCII output -- need to set scientific and precision==9 on the stream
58+
// iostream output operator for STL ASCII output -- need to set scientific and precision==9 on the stream (actually 8, because
59+
// std::scientific format only counts digits *after* the decimal point.
60+
// see https://www.zverovich.net/2023/06/04/printing-double.html
5961
template <dsga::dimensional_scalar T, std::size_t Size>
6062
inline std::ostream &operator<<(std::ostream &o, const dsga::basic_vector<T, Size> &v)
6163
{
6264
o << v[0];
6365
for (int i = 1; i < v.length(); ++i)
64-
o << " " << v[i];
66+
o << " " << std::scientific << v[i];
6567
return o;
6668
}
6769

6870
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6971

7072
bool maybe_binary_stl(std::ifstream &some_file, uintmax_t size)
7173
{
72-
constexpr auto facet_size = 50u;
73-
constexpr auto header_size = 80u;
74-
constexpr auto num_facets_size = 4u;
74+
constexpr uintmax_t facet_size = 50u;
75+
constexpr uintmax_t header_size = 80u;
76+
constexpr uintmax_t num_facets_size = 4u;
7577
bool maybe_val = false;
7678
unsigned int num_facets{};
7779

@@ -190,8 +192,7 @@ bool convert_binary_stl_to_ascii(std::ifstream &some_file, std::ofstream &out_fi
190192
some_file.seekg(header_size + num_facets_size);
191193

192194
// iostream ASCII STL float format
193-
out_file.setf(std::ios::scientific);
194-
out_file.precision(9);
195+
out_file.precision(std::numeric_limits<float>::max_digits10 - 1);
195196

196197
// convert input to output
197198
out_file << solid_open;

include/dsga.hxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// (See accompanying file LICENSE_1_0.txt or copy at
44
// https://www.boost.org/LICENSE_1_0.txt)
55

6+
// https://github.com/davidbrowne/dsga
7+
68
// opening include guard
79
#if !defined(DSGA_DSGA_HXX)
810
#define DSGA_DSGA_HXX

0 commit comments

Comments
 (0)