Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output stream operator #376

Merged
merged 7 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
1. Added Equal functions with a tolerance parameter to Pose3 and Quaternion.
* [BitBucket pull request 319](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/319)

1. Updates per issue #101.
1. Updates per issue [#101](https://github.com/ignitionrobotics/ign-math/issues/101).
* Matrix3: [BitBucket pull request 328](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/328)
* Pose: [BitBucket pull request 329](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/329)
* Quaternion: [BitBucket pull request 327](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/327)
Expand All @@ -32,19 +32,23 @@

1. Defer regex construction to avoid static initialization.
* [Pull request 289](https://github.com/ignitionrobotics/ign-math/pull/289)

1. Defer Material::Predefined construction to avoid static initialization.
* [Pull request 290](https://github.com/ignitionrobotics/ign-math/pull/290)

1. Resolve cppcheck errors by adding explicit constructors and consts.
* [Pull request 291](https://github.com/ignitionrobotics/ign-math/pull/291)

1. Remove virtual from destructors of copyable classes.
* [Pull request 293](https://github.com/ignitionrobotics/ign-math/pull/293)

1. Use constexpr for simple static constants.
* [Pull request 283](https://github.com/ignitionrobotics/ign-math/pull/283)

1. Deprecated `appendToStream(std::ostream, T, int)`. Use `appendToStream(std::ostream, T)`
instead.
* [Pull request 376](https://github.com/ignitionrobotics/ign-math/pull/376)

## Ignition Math 6.x

### Ignition Math 6.x.x
Expand Down
5 changes: 4 additions & 1 deletion Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ release will remove the deprecated code.
+ ***Deprecation:*** public: void W(T)
+ ***Replacement:*** public: void SetW(T)

1. **Helpers.hh**
+ **Deprecation:** template<typename T> inline void appendToStream(std::ostream, T, int)
+ **Replacement:** template<typename T> inline void appendToStream(std::ostream, T)

### Modifications

1. The out stream operator is guaranteed to return always plain 0 and not to
Expand Down Expand Up @@ -266,4 +270,3 @@ release will remove the deprecated code.

+ ***Deprecation:*** IGN_I64_INF
+ ***Replacement:*** ignition::math::INF_I64

2 changes: 1 addition & 1 deletion include/ignition/math/Color.hh
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ namespace ignition
if (i > 0)
_out << " ";

appendToStream(_out, _color[i], 6);
appendToStream(_out, _color[i]);
}
return _out;
}
Expand Down
20 changes: 19 additions & 1 deletion include/ignition/math/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,10 @@ namespace ignition
/// \param[out] _out Output stream.
/// \param[in] _number Number to append.
/// \param[in] _precision Precision for floating point numbers.
/// \deprecated Use appendToStream(std::ostream, T) instead.
template<typename T>
inline void appendToStream(std::ostream &_out, T _number, int _precision)
inline void IGN_DEPRECATED(7) appendToStream(
std::ostream &_out, T _number, int _precision)
{
if (std::fpclassify(_number) == FP_ZERO)
{
Expand All @@ -476,6 +478,22 @@ namespace ignition
}
}

/// \brief Append a number to a stream. Makes sure "-0" is returned as "0".
/// \param[out] _out Output stream.
/// \param[in] _number Number to append.
template<typename T>
inline void appendToStream(std::ostream &_out, T _number)
{
if (std::fpclassify(_number) == FP_ZERO)
jennuine marked this conversation as resolved.
Show resolved Hide resolved
{
_out << 0;
}
else
{
_out << _number;
}
}

/// \brief Append a number to a stream, specialized for int.
/// \param[out] _out Output stream.
/// \param[in] _number Number to append.
Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Matrix3.hh
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ namespace ignition
if (!(i == 0 && j == 0))
_out << " ";

appendToStream(_out, _m(i, j), 6);
appendToStream(_out, _m(i, j));
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Matrix4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ namespace ignition
if (!(i == 0 && j == 0))
_out << " ";

appendToStream(_out, _m(i, j), 6);
appendToStream(_out, _m(i, j));
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Vector2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ namespace ignition
if (i > 0)
_out << " ";

appendToStream(_out, _pt[i], 6);
appendToStream(_out, _pt[i]);
}
return _out;
}
Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Vector3.hh
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ namespace ignition
if (i > 0)
_out << " ";

appendToStream(_out, _pt[i], 6);
appendToStream(_out, _pt[i]);
}

return _out;
Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Vector4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ namespace ignition
if (i > 0)
_out << " ";

appendToStream(_out, _pt[i], 6);
appendToStream(_out, _pt[i]);
}
return _out;
}
Expand Down
40 changes: 32 additions & 8 deletions src/Helpers_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#include <gtest/gtest.h>

#include <iomanip>
#include <cmath>
#include <limits>

#include "ignition/math/Rand.hh"
#include "ignition/math/Vector3.hh"
#include "ignition/math/Helpers.hh"
Expand Down Expand Up @@ -963,21 +967,41 @@ TEST(HelpersTest, AppendToStream)
{
std::ostringstream out;

math::appendToStream(out, 0.12345678, 3);
azeey marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ(out.str(), "0.123");
math::appendToStream(out, 0.0f);
EXPECT_EQ(out.str(), "0");

out << " ";

math::appendToStream(out, 0.0f, 5);
EXPECT_EQ(out.str(), "0.123 0");
math::appendToStream(out, 456);
EXPECT_EQ(out.str(), "0 456");

out << " ";

math::appendToStream(out, 456, 3);
EXPECT_EQ(out.str(), "0.123 0 456");
math::appendToStream(out, 0);
EXPECT_EQ(out.str(), "0 456 0");

out << " ";

math::appendToStream(out, 0, 3);
EXPECT_EQ(out.str(), "0.123 0 456 0");
// ref: https://en.cppreference.com/w/cpp/io/manip/setprecision
const long double pi = std::acos(-1.L);
math::appendToStream(out, pi);
EXPECT_EQ(out.str(), "0 456 0 3.14159");

out << " "
<< std::setprecision(10);

math::appendToStream(out, pi);
EXPECT_EQ(out.str(), "0 456 0 3.14159 3.141592654");

out << " "
<< std::setprecision(std::numeric_limits<long double>::digits10 + 1);

math::appendToStream(out, pi);
EXPECT_EQ(out.str(), "0 456 0 3.14159 3.141592654 3.141592653589793239");

out << " "
<< std::setprecision(3);

math::appendToStream(out, pi);
EXPECT_EQ(out.str(), "0 456 0 3.14159 3.141592654 3.141592653589793239 3.14");
}