0
$\begingroup$

Compiling a node with the following snippet and -Wfloat-conversion leads to the following:

component.cpp:24:56: warning: conversion from ‘double’ to ‘float’ may change value [-Wfloat-conversion]
24 |   float linear_acceleration_stddev = declare_parameter<float>("linear_acceleration_stddev", 1e-3f);

even if:

  1. I used float as template param
  2. I assigned as default value a float literal
  3. I am assigning to a float variable

What am I doing wrong?

$\endgroup$
1
  • 1
    $\begingroup$ Honestly, it looks to me like you're doing everything correct... there might be a conversion happening under-the-hood with the declare_parameter implementation, where they're going from the float you provide to a double, and then you convert that double to a float. If that's what's happening you may be out of luck, you might just have to stick with doubles or accept this warning. $\endgroup$
    – cst0
    Commented Dec 18, 2023 at 18:45

1 Answer 1

1
$\begingroup$

All floating point Parameters end up being doubles under the hood - so your "declare_parameter" is basically being recast as "declare_parameter" - and then the implicit conversion from double to float generates the warning.

My suggestion would be to explicitly do the cast - which should get rid of the warning:

float linear_acceleration_stddev = static_cast<double>(declare_parameter("linear_acceleration_stddev", 1e-3));

As a side note - rclcpp will NOT do this automatic recasting the it is a vector (for instance, you can have a vector<double> parameter type - but vector<float> won't even compile). This very fun limitation is the sole reason I had once dug deep enough into rclcpp to be able to answer your question...

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.