ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Why can't you use floats for accessing parameters in roscpp?

asked 2011-08-17 08:39:14 -0500

Chad Rockey gravatar image

updated 2011-08-18 02:23:10 -0500

I've encountered a possible annoying design choice that the parameter server doesn't accept setting floats.

Although this page erroneously lists floats as acceptable:, you have to use doubles. I see that the yaml specification doesn't have or need a distinction and so only uses doubles. As a result this doesn't work:

float foo;
pnh_->param("foo", foo, 0.1f);

Is there a reason to not set floats and simply lose unneeded precision from the double version? It would be nice since the error message is not easy to understand for beginners:

In file included from /opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/include/ros/ros.h:45,
from ...
/opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/include/ros/node_handle.h: In member function ‘void ros::NodeHandle::param(const std::string&, T&, const T&) const [with T = float]’:
...:61:   instantiated from here
/opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/include/ros/node_handle.h:1441:          error: no matching function for call to ‘ros::NodeHandle::getParam(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, float&) const’
/opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/include/ros/node_handle.h:1285: note: candidates are: bool ros::NodeHandle::getParam(const std::string&, std::string&) const
/opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/include/ros/node_handle.h:1294: note:                 bool ros::NodeHandle::getParam(const std::string&, double&) const
/opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/include/ros/node_handle.h:1303: note:                 bool ros::NodeHandle::getParam(const std::string&, int&) const
/opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/include/ros/node_handle.h:1312: note:                 bool ros::NodeHandle::getParam(const std::string&, bool&) const
/opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/include/ros/node_handle.h:1321: note:                 bool ros::NodeHandle::getParam(const std::string&, XmlRpc::XmlRpcValue&) const

I think the answers might be too focused on the fact that you can lose precision. I'm not proposing we store the parameters as floats in the server, but simply that we allow access to them as floats. The alternative is that we force people to understand the error and then write code like this:

double bar;
nh.param("foo", bar, 0.1);
float foo = (float)bar;

If you pass in a float, we should be able to assume that you are okay not using double precision.

edit retag flag offensive close merge delete


I remember encountering this when first starting to use the parameter server as well.
Thomas D gravatar image Thomas D  ( 2011-08-17 08:50:44 -0500 )edit

I prefer the float foo = (float) bar; version because that makes it all explicit.

joq gravatar image joq  ( 2012-04-14 06:43:02 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2011-08-18 01:56:58 -0500

Python does not have C-float as a native type (see here), but only C-double. This means that when you do something like:

a = 1.2

a is mapped to the C equivalent of double. Therefore loss-of-precision issues might arise when both C and python nodes access the same parameter.

I think that using as few native types as possible is a good choice, as it limits problems related to inter-languages compatibility.

edit flag offensive delete link more


I don't intend to send it out to ROS again. For lots of things, precision just doesn't matter and you can use a float if you want. In my case, I need to talk down to some lower level hardware that only supports floats. I'd rather just be able to access my value as a float instead of casting.
Chad Rockey gravatar image Chad Rockey  ( 2011-08-22 04:05:49 -0500 )edit

answered 2011-08-17 12:23:01 -0500

dornhege gravatar image

One reason might be that if there is an overload for float you will not see the loss of precision, so you might do this unnoticed.

edit flag offensive delete link more


I think if you decided to use float and the loss of precision goes unnoticed and affects your operation, you have bigger problems. :)
Chad Rockey gravatar image Chad Rockey  ( 2011-08-22 04:06:28 -0500 )edit

Question Tools


Asked: 2011-08-17 08:39:14 -0500

Seen: 3,756 times

Last updated: Aug 18 '11