Skip to content

std.math.sign: Narrow integer return types to possible values #23595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jayschwa
Copy link
Contributor

@jayschwa jayschwa commented Apr 17, 2025

This may be a breaking change for callers that depend on the return type being identical to the input type. For example, sign(i32) * i8 + i8 could now overflow. Callers can add an explicit type annotation to avoid that situation.

If Zig ever allows small integer types to coerce to floats (see #18614), then it might make sense to always return an integer. In the meantime, I figured it would be too annoying for users dealing with floats.

@jayschwa jayschwa force-pushed the narrow-math-sign-ints branch from 7c5352f to b35e4ce Compare April 17, 2025 05:54
This may be a breaking change for callers that depend on the return type
being identical to the input type. For example, `sign(i32) * i8 + i8`
could now overflow. Callers can add an explicit type annotation to avoid
that situation.

If Zig ever allows small integer types to coerce to floats (see ziglang#18614),
then it might make sense to always return an integer. In the meantime,
I figured it would be too annoying for users dealing with floats.
@jayschwa jayschwa force-pushed the narrow-math-sign-ints branch from b35e4ce to 09f03f6 Compare April 18, 2025 19:12
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