Skip to content

BitConverter.HalfToInt16Bits(Half) result comparison issue #82680

@gcsizmadia

Description

@gcsizmadia

Description

I am playing around the negative zero number and I found an interesting thing while I used BitConverter.HalfToInt16(Half) method.

The two comparison gives different result

  • Compare the method result directly to 0.
  • Store the method result in a short variable then compare the variable to 0.

Reproduction Steps

Half half = (Half)(-0.0f);

// It will be -32768.
short bits = BitConverter.HalfToInt16Bits(half);

// It will be true.
bool isMethodResultGreaterThanOrEqualToZero = BitConverter.HalfToInt16Bits(half) >= 0;

// It will be false.
bool isVariableGreaterThanOrEqualToZero = bits >= 0;

Expected behavior

The expected behavior is that the two comparison method should have the same result: false

Actual behavior

  • When directly check if the method result is >= 0 then the comparison result will be true.
  • When storing the method result first in a variable and then check if the variable is >= 0 then the comparison result will be false.

Regression?

No response

Known Workarounds

No response

Configuration

  • Which version of .NET is the code running on?
    .NET 6.0.14 and .NET 7.0.3
  • What OS and version, and what distro if applicable?
    Windows 11 - Microsoft Windows [Version 10.0.22621.1265]
  • What is the architecture (x64, x86, ARM, ARM64)?
    x64
  • Do you know whether it is specific to that configuration?
    I don't know.

Other information

No response

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions