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

float== #61

Merged
merged 1 commit into from
Feb 19, 2025
Merged

float== #61

merged 1 commit into from
Feb 19, 2025

Conversation

PhilipDeegan
Copy link
Member

@PhilipDeegan PhilipDeegan commented Feb 19, 2025

Summary by CodeRabbit

  • New Features

    • Introduced functionality for comparing floating-point numbers with customizable tolerances, enabling more reliable numerical equality checks.
  • Refactor

    • Streamlined internal assertion mechanisms for improved parameter handling, maintaining the same behavior while potentially enhancing performance.

Copy link

coderabbitai bot commented Feb 19, 2025

"""

Walkthrough

This update modifies function parameter passing in inc/mkn/kul/assert.hpp by changing the inline functions abort_if and abort_if_not from accepting a reference (bool const&) to a value (bool const). Additionally, a new header file, inc/mkn/kul/float.hpp, is introduced with two inline functions, both named float_equals, to compare floating-point numbers (one for float and one for double) using default tolerance values.

Changes

File(s) Change Summary
inc/mkn/kul/assert.hpp Modified parameter types for inline functions abort_if and abort_if_not from bool const& to bool const.
inc/mkn/kul/float.hpp New file added. Provides inline functions float_equals for comparing float and double values with default tolerances (1e-6 for float and 1e-12 for double).

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant FloatEquals
    Note over Caller, FloatEquals: Floating-point equality check
    Caller->>FloatEquals: Call float_equals(a, b, tolerance)
    FloatEquals->>FloatEquals: Compute |a - b| using std::abs
    FloatEquals-->>Caller: Return boolean result
Loading

Poem

I'm a hopping rabbit in a digital land,
Skipping through code with a twitch of my hand.
New floats compare with careful delight,
And asserts now pass arguments just right.
In a carrot-filled world of clean code, I cheer all day and night!
🥕🐇🌟
"""

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
inc/mkn/kul/float.hpp (2)

38-40: Add documentation to explain the tolerance value.

Consider adding documentation to explain why 1e-6 was chosen as the default tolerance for single-precision floats. This helps users understand if this tolerance is appropriate for their use case.

Add a doc comment like this:

+/**
+ * Compares two float values for equality within a specified tolerance.
+ * @param a First float value
+ * @param b Second float value
+ * @param diff Tolerance value (default: 1e-6 suitable for most single-precision calculations)
+ * @return true if the absolute difference between a and b is less than diff
+ */
 auto inline float_equals(float const& a, float const& b, float diff = 1e-6) {

42-44: Add documentation and handle special cases.

  1. Add documentation to explain the tolerance value.
  2. Consider handling special cases like NaN and infinity.

Apply these improvements:

+/**
+ * Compares two double values for equality within a specified tolerance.
+ * @param a First double value
+ * @param b Second double value
+ * @param diff Tolerance value (default: 1e-12 suitable for most double-precision calculations)
+ * @return true if the absolute difference between a and b is less than diff
+ */
 auto inline float_equals(double const& a, double const& b, double diff = 1e-12) {
+  // Handle NaN
+  if (std::isnan(a) || std::isnan(b)) return false;
+  // Handle infinity
+  if (std::isinf(a) || std::isinf(b)) return a == b;
   return std::abs(a - b) < diff;
 }

Don't forget to add the necessary header:

 #include <cstdlib>
+#include <cmath>
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e98e7ba and f63d89f.

📒 Files selected for processing (2)
  • inc/mkn/kul/assert.hpp (1 hunks)
  • inc/mkn/kul/float.hpp (1 hunks)
🔇 Additional comments (2)
inc/mkn/kul/assert.hpp (2)

55-57: LGTM! Good improvement in parameter type.

Changing from bool const& to bool const is a good improvement since passing a bool by reference has no performance benefit and can make the code less readable.


59-61: LGTM! Good improvement in parameter type.

Same improvement as abort_if - changing from bool const& to bool const is better since bool is already a small type.

@PhilipDeegan PhilipDeegan merged commit f2bc2b9 into master Feb 19, 2025
3 checks passed
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
inc/mkn/kul/float.hpp (2)

34-34: Use <cmath> instead of <cstdlib> for mathematical functions.

<cmath> is more appropriate for mathematical functions like std::abs when working with floating-point types.

-#include <cstdlib>
+#include <cmath>

38-40: Consider improvements to the floating-point comparison functions.

  1. The return type could be more explicit:
-auto inline float_equals(float const a, float const b, float const diff = 1e-6) {
+inline bool float_equals(float const a, float const b, float const diff = 1e-6) {
  1. The functions should handle special cases like NaN and infinity:
 inline bool float_equals(float const a, float const b, float const diff = 1e-6) {
+  if (std::isnan(a) || std::isnan(b)) return false;
+  if (std::isinf(a) || std::isinf(b)) return a == b;
   return std::abs(a - b) < diff;
 }
  1. Consider adding documentation to explain:
    • The purpose of the default tolerance values
    • How special cases are handled
    • That the comparison is absolute (not relative)

Also applies to: 42-44

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f63d89f and f460484.

📒 Files selected for processing (2)
  • inc/mkn/kul/assert.hpp (1 hunks)
  • inc/mkn/kul/float.hpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • inc/mkn/kul/assert.hpp
🔇 Additional comments (1)
inc/mkn/kul/float.hpp (1)

36-36: LGTM!

The namespace declaration uses modern C++ nested namespace syntax.

Comment on lines +31 to +32
#ifndef _MKN_KUL_FLOAT_HPP_
#define _MKN_KUL_FLOAT_HPP_
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Rename header guard to avoid reserved identifiers.

The current header guard starts with an underscore followed by a capital letter (_MKN_KUL_FLOAT_HPP_), which is reserved for the implementation in C++. Consider renaming it to MKN_KUL_FLOAT_HPP.

-#ifndef _MKN_KUL_FLOAT_HPP_
-#define _MKN_KUL_FLOAT_HPP_
+#ifndef MKN_KUL_FLOAT_HPP
+#define MKN_KUL_FLOAT_HPP
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#ifndef _MKN_KUL_FLOAT_HPP_
#define _MKN_KUL_FLOAT_HPP_
#ifndef MKN_KUL_FLOAT_HPP
#define MKN_KUL_FLOAT_HPP


} // namespace mkn::kul

#endif /* _MKN_KUL_FLOAT_HPP_ */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Update the end header guard comment to match the new header guard name.

For consistency with the earlier header guard change:

-#endif /* _MKN_KUL_FLOAT_HPP_ */
+#endif /* MKN_KUL_FLOAT_HPP */
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#endif /* _MKN_KUL_FLOAT_HPP_ */
#endif /* MKN_KUL_FLOAT_HPP */

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.

1 participant