Skip to content

rstsr-dtype-traits: implement ext_sign for integers, floats and complex - #93

Merged
ajz34 merged 1 commit into
masterfrom
extnum-sign
Aug 14, 2026
Merged

rstsr-dtype-traits: implement ext_sign for integers, floats and complex#93
ajz34 merged 1 commit into
masterfrom
extnum-sign

Conversation

@ajz34

@ajz34 ajz34 commented Aug 14, 2026

Copy link
Copy Markdown
Member

Summary

  • Add ext_sign(self) -> Self to the ExtNum trait, following NumPy's np.sign semantics:
    • signed integers: -1/0/1 (comparison form, no overflow at the type minimum)
    • unsigned integers: 0/1
    • floats: NaN → NaN, ±inf → ±1, both signed zeros → +0.0
    • complex: z / |z|, zero magnitude → zero
    • half f16/bf16 follow the float rules (feature-gated)
  • rstsr-core: switch OpSignAPI device impls (DeviceCpuSerial, DeviceRayonAutoImpl) from the ComplexFloat + Div bound to ExtNum, so rt::sign now accepts integer dtypes. The closed ExtNum type set replaces the open ComplexFloat bound as the fallback for sign (not all ComplexFloat-bounded types remain covered).
  • Fixes two NumPy divergences of the previous x / |x| formula: sign(±inf) returned NaN instead of ±1, and sign(-0.0) kept -0.0 instead of +0.0.
  • Tests: core_func math sign tests extended to i32/u8 tensors plus float special-value coverage (NaN/inf/signed zeros). Local runs green: entry_row_cpu 257 passed, rstsr-core --lib 94/101 (cpu_serial/rayon), workspace check + clippy clean.

Most or all contents generated by AI (model glm-5.3).

🤖 Generated with Claude Code

Co-authored-by: Claude Code noreply@anthropic.com
Co-authored-by: glm-5.3 service@zhipuai.cn

Add ext_sign(self) -> Self to ExtNum following NumPy's np.sign semantics:
signed integers map to -1/0/1 (comparison form, no overflow at the type
minimum), unsigned to 0/1, floats map NaN to NaN, infinities to +-1 and
both signed zeros to +0.0, complex maps z/|z| with zero magnitude to
zero; half f16/bf16 follow the float rules.

rstsr-core: switch OpSignAPI device impls (DeviceCpuSerial and
DeviceRayonAutoImpl) from the ComplexFloat + Div bound to ExtNum, so
rt::sign now accepts integer dtypes. The closed ExtNum type set replaces
the open ComplexFloat bound as the fallback for sign.

This also fixes two NumPy divergences of the previous x/|x| formula:
sign(+-inf) returned NaN instead of +-1, and sign(-0.0) kept -0.0
instead of +0.0.

Tests: extend core_func math tests to i32/u8 sign and add float
special-value coverage (NaN/inf/signed zeros).

Most or all contents generated by AI (model glm-5.3).

Co-authored-by: Claude Code <noreply@anthropic.com>
Co-authored-by: glm-5.3 <service@zhipuai.cn>
@ajz34
ajz34 merged commit 9715fa7 into master Aug 14, 2026
8 checks passed
@ajz34
ajz34 deleted the extnum-sign branch August 14, 2026 10:53
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