Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix constant generation for C++ floats #772

Merged
merged 3 commits into from
Oct 11, 2023

Conversation

clalancette
Copy link
Contributor

While working on a package, I came across a build warning on Windows that said:

warning C4305: 'initializing': truncation from 'double' to 'const float'

After looking into it, I realized that the problem was that when we generate float constants for C++, the value we assign to it is a double, not a float. For instance, given the Constants.msg file, we currently generate (truncated for brevity):

  // constant declarations
  static constexpr float FLOAT32_CONST =
    1.125;
  static constexpr double FLOAT64_CONST =
    1.125;

But that is not quite right; that first line should be:

  static constexpr float FLOAT32_CONST =
    1.125f;

Interestingly, the warning only shows up when assigning certain constants to the float, like 0.05.

So this PR does three things:

  1. Does a small refactoring of the tests, removing bits we don't need.
  2. Adds a test for the problematic constants (by default, the warning will only show up on Windows).
  3. Fixes the code generation to append the f as appropriate.

Get rid of some unnecessary macros, refactor the
header includes, and make things static where possible.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
In particular, we want to test float constants.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
Signed-off-by: Chris Lalancette <clalancette@gmail.com>
@clalancette
Copy link
Contributor Author

CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Windows Build Status

@clalancette clalancette merged commit 2f90010 into rolling Oct 11, 2023
4 checks passed
@delete-merged-branch delete-merged-branch bot deleted the clalancette/rosidl-generator-test-cleanup branch October 11, 2023 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants