Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

A ton of issues with 'half' (including bugs)

未关闭
#185 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
20/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
csharp
领域
backend

调研方向

该 issue 没有指明文件、测试或入口点;首先定位 Unity.Mathematics 中的 half 类型。将请求的比较、算术、转换、常量、解析和接口工作拆分为范围明确的更改,然后使用针对性测试验证列出的每项行为。

由索引模型根据 Issue 内容生成。

描述

Soooo... ;D

The (not) equals operators are broken: -0 against +0 equality is not being considered
Proof:

UnityEngine.Debug.Log(math.asfloat(1u << 31) == math.asfloat(0u));  

// output: TRUE

Also, inequality of NaN against NaN is not being considered.
Proof:

float x = float.NaN;
UnityEngine.Debug.Log(x == x); 

// output: FALSE

This also applies to IEquatable.Equals().

The operators '<', '>', '<=' and '>=' are missing (internally, just convert the operands to floats). Those would be user-friendly wrapper functions instead of having to cast two halfs explicitly all the time.

Consequently, an IComparable implementation is missing.

Obviously, all the arithmetic operators are missing, which could also be wrapped. I understand that that's harder to do both for you aswell as the computer (casting back and forth instead of just promoting to floats and returning a boolean evaluation), but numbers of any kind should at the very least be (I)comparable. If the arithmetic operators are implemented, though, they should return floats (identical to (u)short operations).

Typecasts from/to (u)ints are missing. Currently we have to type (half)(float)myInt, which, once again, could be wrapped. If you decide to do this, do it for (u)long in order to cover all possibilites.

MinValue and MaxValue should ONLY be halfs (just like System.Single and System.Double), if anyone references them as floats or doubles they get compile time evaluated anyway; that would be a backwards-compatible change due to implicit casting.
Mark "Min/MaxValueAsHalf" as obsolete.
Btw the float values, if they have any reason to exist, should be constants so we can see them in the IDE (or you can write XML documentation for the half-only values i.e. "65XXX as a float"). Mathematics is very strange in that regard. Most constants of custom types are static readonly variables, which clutters RAM unnecessarily and causes performance "problems" in managed code (RAM read) whereas they should be static readonly properties i.e. an inlined function call to "XOR regA regA" (mostly talking about "zero" here...), while float values, which can be const, like half.Min/MaxValue are implemented as static readonly properties :D
alt text

Missing static properties:
Epsilon is missing which is (half)0.000000059604644775390625d or new half { value = 1 }
NaN is missing which is new half { value = 0xFE00 }
NegativeInfinity is missing which is new half { value = 0xFC00 }
PositiveInfinity is missing which is new half { value = 0x7C00 }

More static functions are missing which is not user-friendly (having to call float.IsNaN etc) and also causes software implemented casting all the time (also in Burst, where efficient 16-bit SIMD ops could be used instead of casting and performing a float comparison):

public static bool IsInfinity(half h)
{
    return (h.value & 0x7FFF) == 0x7C00;
}

public static bool IsNaN(half h)
{
    return (h.value & 0x7FFF) > 0x7C00;
}

public static bool IsNegativeInfinity(half h)
{
    return h.value == 0xFC00;
}

public static bool IsPositiveInfinity(half h)
{
    return h.value == 0x7C00;
}

...which would result in the following branch-free, ILP implementations of '==' and '!='

public static bool operator == (half left, half right)
{
     bool nan   = !IsNaN(left) & !IsNaN(right);
     bool zero  = ((left.value & 0x7FFF) == 0) & ((right.value & 0x7FFF) == 0);
     bool value = left.value == right.value;

     return nan & (zero | value);
}

public static bool operator != (half left, half right)
{
     bool nan   = IsNaN(left) | IsNaN(right);
     bool zero  = ((left.value & 0x7FFF) != 0) | ((right.value & 0x7FFF) != 0);
     bool value = left.value != right.value;

     return nan | (zero & value); 
} 

Additionally, there's .NET standard stuff that nobody really needs but which is trivial to implement (especially since it would only have to be done for the half type itself and not the vectors):

public half Parse(string s)
{
    return (half)float.Parse(s);
}

public bool TryParse(string s, out half result)
{
    bool success = float.TryParse(s, out float cvt);
    
    result = (half)cvt;

    return success && cvt <= MaxValue && cvt >= MinValue;
}

... Aswell as an implementation of the "IConvertible" interface, which is probably more meaningful, since I know a lot of software that uses this among others as a generic constraint to identify numeric types.

Thanks for taking the time to read this! - And sorry for the edits; I'm an iterative guy.

主要语言
C#
星标
1.4k
派生
159
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Unity-Technologies/Unity.Mathematics 的其他 Issue

查看 Unity-Technologies/Unity.Mathematics 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。