Back to all reviewers

optimize mathematical mappings

commaai/openpilot
Based on 2 comments
Other

Use consistent units and data representations throughout mathematical calculations to avoid unnecessary computational overhead. When working with value ranges, prefer direct mathematical mappings over multiple conversions, and leverage utility functions for range transformations.

Algorithms Other

Reviewer Prompt

Use consistent units and data representations throughout mathematical calculations to avoid unnecessary computational overhead. When working with value ranges, prefer direct mathematical mappings over multiple conversions, and leverage utility functions for range transformations.

For example, instead of converting between different units multiple times:

// Avoid multiple conversions
int ir_percent = util::map_val(static_cast<int>(ir_pwr), 0, static_cast<int>(100*MAX_IR_POWER), 0, 100);

// Better: work in consistent units, then map once
int value = util::map_val(std::clamp(percent, 0, 100), 0, 100, 0, 255);

This approach reduces computational complexity, minimizes floating-point precision errors, and makes the code more maintainable by establishing clear data flow patterns. Choose the most natural unit for your domain and stick with it throughout the calculation pipeline.

2
Comments Analyzed
Other
Primary Language
Algorithms
Category

Source Discussions