Skip to content
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

integer support for std.math.pow #1637

Closed
andrewrk opened this Issue Oct 5, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@andrewrk
Copy link
Member

andrewrk commented Oct 5, 2018

Right now std.math.pow only supports floating points. It should support integers too.

https://stackoverflow.com/a/101613/432

@andrewrk andrewrk added the userland label Oct 5, 2018

@andrewrk andrewrk added this to the 1.0.0 milestone Oct 5, 2018

@emekoi

This comment has been minimized.

Copy link
Contributor

emekoi commented Oct 6, 2018

is it okay to overflow, or do we need to check for that somehow?

@andrewrk

This comment has been minimized.

Copy link
Member Author

andrewrk commented Oct 6, 2018

The current pattern in std.math is to check for overflow. Idea being that callers that want to assert no overflow can happen would use catch unreachable. I don't think I've made a proposal for this yet, but one idea I had is that Zig could generate special versions of functions when catch unreachable is used - every instance of return (error) would be replaced with unreachable. This special version of the function would be just as optimized as a function that didn't have the possibility of error - in fact - potentially even more optimized since the unreachable communicates information to the optimizer.

@emekoi

This comment has been minimized.

Copy link
Contributor

emekoi commented Oct 6, 2018

when a possible overflow is detected what would be the proper response?

@andrewrk

This comment has been minimized.

Copy link
Member Author

andrewrk commented Oct 6, 2018

Return error.Overflow. For example:

zig/std/math/index.zig

Lines 245 to 267 in 4640853

pub fn mul(comptime T: type, a: T, b: T) (error{Overflow}!T) {
var answer: T = undefined;
return if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer;
}
pub fn add(comptime T: type, a: T, b: T) (error{Overflow}!T) {
var answer: T = undefined;
return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer;
}
pub fn sub(comptime T: type, a: T, b: T) (error{Overflow}!T) {
var answer: T = undefined;
return if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer;
}
pub fn negate(x: var) !@typeOf(x) {
return sub(@typeOf(x), 0, x);
}
pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
var answer: T = undefined;
return if (@shlWithOverflow(T, a, shift_amt, &answer)) error.Overflow else answer;
}

@andrewrk andrewrk closed this in a22d9da Oct 10, 2018

@andrewrk andrewrk modified the milestones: 1.0.0, 0.4.0 Apr 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.