LibreOffice Module sc (master) 1
utils.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include "utils.hxx"
11
12#include <cassert>
13#include <cmath>
14#include <cfloat>
15#include <limits>
16#include <sstream>
17
18namespace sc
19{
20namespace opencl
21{
22namespace
23{
24#ifdef SAL_LOG_INFO
25class outputstream_num_put : public std::num_put<char>
26{
27protected:
28 virtual iter_type do_put(iter_type s, std::ios_base&, char_type, double v) const override
29 {
30 std::string str = preciseFloat(v);
31 return std::copy(str.begin(), str.end(), s);
32 }
33 virtual iter_type do_put(iter_type, std::ios_base&, char_type, long double) const override
34 {
35 abort(); // we do not use these
36 }
37 using std::num_put<char>::do_put;
38};
39#endif
40} // namespace
41
43{
44 precision(DECIMAL_DIG);
45 setf(std::ios::showpoint);
46#ifdef SAL_LOG_INFO
47 // Calling precision() makes the output have trailing insignificant zeroes, which
48 // makes reading the source annoying. So override this stream's floating output
49 // handling to force usage of our preciseFloat(), which has a saner output.
50 imbue(std::locale(std::locale("C"), new outputstream_num_put));
51#endif
52}
53
54#undef stringstream
55std::string preciseFloat(double f)
56{
58 stream.precision(std::numeric_limits<double>::digits10 + 1);
59 stream.setf(std::ios::showpoint);
60 stream << f;
61 std::string str = stream.str();
62 size_t end = str.find('e');
63 if (end == std::string::npos)
64 end = str.size();
65 for (size_t pos = end - 1; pos > 0; --pos)
66 {
67 if (str[pos] != '0')
68 {
69 if (str[pos] == '.')
70 {
71 ++pos;
72 if (pos == end) // 10. without trailing 0
73 {
74 return str + '0';
75 }
76 }
77 ++pos;
78 assert(pos <= end);
79 str.resize(std::copy(str.begin() + end, str.end(), str.begin() + pos) - str.begin());
80 return str;
81 }
82 }
83 abort();
84}
85}
86} // namespace
87
88/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XOutputStream > stream
end
std::string preciseFloat(double f)
Definition: utils.cxx:55
CAUTION! The following defines must be in the same namespace as the respective type.
sal_Int32 precision
#define stringstream
Definition: utils.hxx:45
size_t pos