Skip to content

GH-49123: [C++] Complete std::numeric_limits<Float16> specialization#49124

Open
HyukjinKwon wants to merge 1 commit intoapache:mainfrom
HyukjinKwon:GH-49123
Open

GH-49123: [C++] Complete std::numeric_limits<Float16> specialization#49124
HyukjinKwon wants to merge 1 commit intoapache:mainfrom
HyukjinKwon:GH-49123

Conversation

@HyukjinKwon
Copy link
Member

@HyukjinKwon HyukjinKwon commented Feb 3, 2026

Rationale for this change

The std::numeric_limits<Float16> specialization was incomplete, missing members.

What changes are included in this PR?

  • Completed std::numeric_limits<Float16> with missing members (type traits, precision/exponent values, special value functions)
  • Values follow C++ standard (cppreference) and IEEE 754-2008 binary16 specification

Are these changes tested?

Added Float16Test.NumericLimits validating all members against IEEE 754 semantics.

Are there any user-facing changes?

Yes. Users can now use all std::numeric_limits<Float16> members in generic template code.

@github-actions
Copy link

github-actions bot commented Feb 3, 2026

⚠️ GitHub issue #49123 has been automatically assigned in GitHub to PR creator.

Copy link
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

Thanks for doing this. I think this looks good, here are just a couple potential additions.

ASSERT_TRUE(F16::denorm_min() > Float16::FromBits(0));
ASSERT_TRUE(F16::denorm_min() < F16::min());

// Verify epsilon: 1 + epsilon != 1
Copy link
Member

Choose a reason for hiding this comment

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

We may also verify that 1 + (epsilon / 2) == 1

ASSERT_TRUE((-F16::infinity()).is_infinity());
ASSERT_TRUE(F16::min() > Float16::FromBits(0));
ASSERT_TRUE(F16::denorm_min() > Float16::FromBits(0));
ASSERT_TRUE(F16::denorm_min() < F16::min());
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps also verify that denorm_min / 2 == 0? Am I assuming right?

ASSERT_EQ(F16::max_exponent10, 4);

// Special values
ASSERT_FLOAT_EQ(F16::max().ToFloat(), 65504.0f); // Largest finite value
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we can also add this:

Suggested change
ASSERT_FLOAT_EQ(F16::max().ToFloat(), 65504.0f); // Largest finite value
ASSERT_FLOAT_EQ(F16::max().ToFloat(), 65504.0f); // Largest finite value
ASSERT_TRUE(Float16::FromBits(F16::max().bits() + 1U).is_infinity());

static constexpr bool has_infinity = true;
static constexpr bool has_quiet_NaN = true;
static constexpr bool has_signaling_NaN = true;
static constexpr std::float_denorm_style has_denorm = std::denorm_present;
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be deprecated in C++23, so perhaps we shouldn't bother?

@github-actions github-actions bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants