You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add an explicit truncate-toward-zero mode for float-to-integer conversions in nk.astype.
Why
nk.astype currently rounds float inputs to the nearest even integer and saturates overflow. That is useful for image operations that need rounded pixels. Other image pipelines already clip values to the destination range and then rely on NumPy astype to discard the fractional part. Those paths need 1.9 -> 1, while the current NumKong conversion produces 1.9 -> 2.
This matters independently of the buffer-first API from #368: the current conversion cannot replace a NumPy cast when truncation is part of the result contract.
rounding="nearest_even" can remain the default, preserving the current behavior. The exact parameter name is flexible; the important part is an explicit, documented truncation mode.
Contract
For float-to-integer conversions with truncation enabled:
discard the fractional part toward zero: -1.9 -> -1, -0.5 -> 0, 0.5 -> 0, 1.9 -> 1;
saturate values outside the destination integer range after truncation;
Cover positive and negative fractional values, half-integers, integer boundaries, clipped and unclipped overflow, NaN, infinities, contiguous and strided inputs, and caller-owned out buffers.
Request
Add an explicit truncate-toward-zero mode for float-to-integer conversions in
nk.astype.Why
nk.astypecurrently rounds float inputs to the nearest even integer and saturates overflow. That is useful for image operations that need rounded pixels. Other image pipelines already clip values to the destination range and then rely on NumPyastypeto discard the fractional part. Those paths need1.9 -> 1, while the current NumKong conversion produces1.9 -> 2.This matters independently of the buffer-first API from #368: the current conversion cannot replace a NumPy cast when truncation is part of the result contract.
Proposed API
rounding="nearest_even"can remain the default, preserving the current behavior. The exact parameter name is flexible; the important part is an explicit, documented truncation mode.Contract
For float-to-integer conversions with truncation enabled:
-1.9 -> -1,-0.5 -> 0,0.5 -> 0,1.9 -> 1;out=contracts from Feature: Buffer-first N-D cast with out= and explicit conversion semantics #368;For the common clipped uint8 route, the intended behavior is:
Tests
Cover positive and negative fractional values, half-integers, integer boundaries, clipped and unclipped overflow, NaN, infinities, contiguous and strided inputs, and caller-owned
outbuffers.