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

ros msg float64 to float value

asked 2020-08-05 03:51:57 -0500

benthebear93 gravatar image

updated 2022-05-23 09:21:23 -0500

lucasw gravatar image

i am trying to remove 3 under decimal point of my data. when i use float to store some data , it gives "-9.082400" but when i use this data to float64 for ros topic, it gives "-9.08240001381"

how can i remove "1381" value? is it happening during conversion between float to float 64?

any idea will help me thx!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-08-05 05:29:32 -0500

Weasfas gravatar image

@benthebear93, that is the natural behaviour of floating points numbers, it a representationm problem.

  1. Doubles AKA float64 can store technically up to 16 significant digits, so you can try to use floats AKA float32 that stores only 7.
  2. You can round the number to the decimal digit you want with, asumming you are using C++: round(value * 1000) / 1000; That is rounder the value number to the 4th decimal digit. So the number -9.08240001381 will be -9.0824.

But at the end of the day you will have decimals that you do not want since thats is how floating point representation works.

If anyone knows any other trick to solve this problem I will glad to discuss it here.


edit flag offensive delete link more


thx for the answer. i guess my question was more related with floating point rather than ROS.

benthebear93 gravatar image benthebear93  ( 2020-08-05 21:44:15 -0500 )edit

Question Tools



Asked: 2020-08-05 03:51:57 -0500

Seen: 1,350 times

Last updated: Aug 05 '20