Repository navigation
Improve Python interoperability of System.Half - #2153
Merged
Merged
Conversation
slozier
approved these changes
Nov 7, 2026
slozier
left a comment
Contributor
There was a problem hiding this comment.
Looks good to me. Just the small suggestion about __bool__ (unless it was intentionally omitted).
| public static Half Getimag(Half self) => default; | ||
|
|
||
| public static Half conjugate(Half self) => self; | ||
|
|
Contributor
There was a problem hiding this comment.
public static bool __bool__(Half self) => !self.IsZero;might be worth it...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
System.Halfhad no Python-level support beyond what .NET exposes on its own: instances could only be created withclr.Convert, and several common Python operations silently produced wrong results. This PR started with adding a proper constructor toHalf: in C#, it's easy to write(Half)0.5, but in IronPython, one needed the verboseclr.Convert(0.5, Half). There is no 16-bit sized float in CPython, butHalfis basically the same asNumPy.float16, so I took that as the guide to add more functionality toHalf.The goal is to make
Halfbehave correctly as a Python object, not to turn it into a Pythonfloator evenSystem.Single. Mixed-type arithmetic and comparisons (Half(1) + 1,Half(1) < 2.0) stay unsupported, as they are in C#, which has no implicit conversions betweenHalfand other numeric types (though surprisingly,Bytegets automatically widened toHalfin C#; not in IronPython). Same-type operators onHalfare supported by the generic math in .NET.Changes
System.Half(1.5)→TypeError: CreateInstance() takes no argumentsSystem.Single(...): numbers, strings, objects with__float__copy/pickleHalf(0)__getnewargs__, as forSingle)format(clr.Convert(1.5, Half), '.2f')'22f'(spec passed toHalf.ToStringas a .NET custom format)'1.50': formatted like afloatwith the same exact value;format(h, '')equalsstr(h)str/repr<System.Half object at 0x… [0.1]>;strwas culture-dependent and lossy (65500(as MaxValue),NaN,∞)6.55e+04nan,inf)Math.Abs(clr.Convert(1.5, Half))1(bound toAbs(Int32)via__int__);inf→2147483647,nan→01.5(binds to the losslessAbs(Double))real,imag,conjugate()AttributeErrorSingle, typed asHalf