genmsg icon indicating copy to clipboard operation
genmsg copied to clipboard

Special Float Generation

Open sye8 opened this issue 5 years ago • 3 comments

In cases where constants in messages are set to special floats, for example:

float32 a = NaN
float32 p = Inf
float32 n = -Inf 

The successfully generated Python script declare those as:

a = nan
p = inf
n = -inf

However, those causes NameError as nan, inf don't exist.

The following works in Python:

a = float('nan')
p = float('inf')
n = float('-inf')

sye8 avatar Jun 18 '19 13:06 sye8

That would be a great addition. Please consider to provide a pull request for that enhancement.

dirk-thomas avatar Jul 15 '19 17:07 dirk-thomas

I think the issue may be with this line in genpy: https://github.com/ros/genpy/blob/5008399c9e8f936c0a7067505c2b7cc4c6c09748/src/genpy/generator.py#L864

yield '  %s = %s' % (c.name, c.val)
# where c.name is the constant name and c.val is the actual value
# when converted to string, c.val just becomes nan

There should probably be a check for special values and provide the appropriate strings OR we should add the lines

nan = float('nan')
inf = float('inf')

at the top.

I have created a new issue https://github.com/ros/genpy/issues/126. Perhaps this issue can be closed, as I don't think we can fix it from genmsg?

glennib avatar Jun 10 '20 14:06 glennib

Perhaps this issue can be closed, as I don't think we can fix it from genmsg?

genmsg provides the parsing logic and the cross-language API. So the concept needs to be added here first and then genpy can use it to generate Python specific code. gencpp (and other languages) will need to be updated accordingly.

dirk-thomas avatar Jun 10 '20 16:06 dirk-thomas