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

How should I express a percentage in a message definition?

asked 2018-06-14 12:46:35 -0500

Ricardo Angeli gravatar image

I'm creating a new custom message type for one of my nodes and I need a field that expresses a percentage value (from 0% to 100%).

What is the standard way to express this? Should I use a floating point variable from 0.0 to 100.0? Or a floating point from 0.00 to 1.00?

edit retag flag offensive close merge delete

Comments

Do you need non-real values, e.g. 12.34%?

Humpelstilzchen gravatar image Humpelstilzchen  ( 2018-06-20 02:21:59 -0500 )edit

Yes, I do.

Ricardo Angeli gravatar image Ricardo Angeli  ( 2018-06-20 08:57:36 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-06-19 14:37:56 -0500

Ricardo Angeli gravatar image

I believe I found an answer. Gvdhoorn is right that there is no defined standard. However, the sensor_msgs/RelativeHumidity message defines relative_humidity as a float64 value from 0.0 to 1.0. So that's at least one standard message that uses that format for what's traditionally a percentage value.

edit flag offensive delete link more

Comments

So the recommendation is to use a 8 Byte variable to store a value that can fit into 1 Byte?

Humpelstilzchen gravatar image Humpelstilzchen  ( 2018-06-20 02:20:37 -0500 )edit

You are making assumptions in your assessment Humpelstilzchen. One byte is in general not sufficient. What about the representation of 37.7% or even 1/3%? float64 provides very good precision between 0.0 and 1.0, thus covering the majority of use cases vs something like a uint8.

BrettHemes gravatar image BrettHemes  ( 2018-06-20 07:57:41 -0500 )edit

You are now making the assumption that there are fractional. We don't know that since the questioner has not replied to my comment yet.

Humpelstilzchen gravatar image Humpelstilzchen  ( 2018-06-20 08:46:13 -0500 )edit
1

In my particular implementation, it is indeed fractional. But even if it wasn't, wouldn't we want the field type to be able to support a higher-precision value if we want a truly generalized and not implementation-specific message definition?

Ricardo Angeli gravatar image Ricardo Angeli  ( 2018-06-20 08:55:21 -0500 )edit

Still you are wasting a lot of bandwidth since float was not made to cover a small area, but a real large one. If you do not need human readable I would use an unsigned int and define 0% = 0 and 100% = max value.

Humpelstilzchen gravatar image Humpelstilzchen  ( 2018-06-20 09:02:48 -0500 )edit

You are now making the assumption that there are fractional

I was not actually, thank you. I was merely stating that, for the sake of generality, the choice of uint8 would be unwise and gave two examples. You are now at 4 bytes with your choice of unsigned int (uint32)... is that too much? too

BrettHemes gravatar image BrettHemes  ( 2018-06-20 09:35:38 -0500 )edit

little? If bandwidth is a concern then yes, go ahead and sacrifice readability, make assumptions, and make your own types (that is why such functionality exists) but I wouldn't expect any standard messages to follow suit unless it was in the nature of the underlying data.

BrettHemes gravatar image BrettHemes  ( 2018-06-20 09:35:44 -0500 )edit
1

answered 2018-06-14 13:10:49 -0500

gvdhoorn gravatar image

I don't believe there is a standard way to encode a percentage.

Should I use a floating point variable from 0.0 to 100.0?

That would be one option, yes.

Or a floating point from 0.00 to 1.00?

I would interprete that as a ratio, not a percentage.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-06-14 12:46:35 -0500

Seen: 23,795 times

Last updated: Jun 19 '18