diff options
Diffstat (limited to 'lib')
26 files changed, 240 insertions, 217 deletions
diff --git a/lib/std/crypto/tlcsprng.zig b/lib/std/crypto/tlcsprng.zig index 115a7ab882..7c43125920 100644 --- a/lib/std/crypto/tlcsprng.zig +++ b/lib/std/crypto/tlcsprng.zig @@ -12,6 +12,7 @@ const std = @import("std"); const root = @import("root"); const mem = std.mem; +const os = std.os; /// We use this as a layer of indirection because global const pointers cannot /// point to thread-local variables. @@ -42,16 +43,12 @@ const maybe_have_wipe_on_fork = std.Target.current.os.isAtLeast(.linux, .{ .minor = 14, }) orelse true; -const WipeMe = struct { - init_state: enum { uninitialized, initialized, failed }, +const Context = struct { + init_state: enum(u8) { uninitialized = 0, initialized, failed }, gimli: std.crypto.core.Gimli, }; -const wipe_align = if (maybe_have_wipe_on_fork) mem.page_size else @alignOf(WipeMe); -threadlocal var wipe_me: WipeMe align(wipe_align) = .{ - .gimli = undefined, - .init_state = .uninitialized, -}; +threadlocal var wipe_mem: []align(mem.page_size) u8 = &[_]u8{}; fn tlsCsprngFill(_: *const std.rand.Random, buffer: []u8) void { if (std.builtin.link_libc and @hasDecl(std.c, "arc4random_buf")) { @@ -64,35 +61,69 @@ fn tlsCsprngFill(_: *const std.rand.Random, buffer: []u8) void { if (comptime std.meta.globalOption("crypto_always_getrandom", bool) orelse false) { return fillWithOsEntropy(buffer); } - switch (wipe_me.init_state) { + + if (wipe_mem.len == 0) { + // Not initialized yet. + if (want_fork_safety and maybe_have_wipe_on_fork) { + // Allocate a per-process page, madvise operates with page + // granularity. + wipe_mem = os.mmap( + null, + @sizeOf(Context), + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS, + -1, + 0, + ) catch |err| { + // Could not allocate memory for the local state, fall back to + // the OS syscall. + return fillWithOsEntropy(buffer); + }; + // The memory is already zero-initialized. + } else { + // Use a static thread-local buffer. + const S = struct { + threadlocal var buf: Context align(mem.page_size) = .{ + .init_state = .uninitialized, + .gimli = undefined, + }; + }; + wipe_mem = mem.asBytes(&S.buf); + } + } + const ctx = @ptrCast(*Context, wipe_mem.ptr); + + switch (ctx.init_state) { .uninitialized => { - if (want_fork_safety) { - if (maybe_have_wipe_on_fork) { - if (std.os.madvise( - @ptrCast([*]align(mem.page_size) u8, &wipe_me), - @sizeOf(@TypeOf(wipe_me)), - std.os.MADV_WIPEONFORK, - )) |_| { - return initAndFill(buffer); - } else |_| if (std.Thread.use_pthreads) { - return setupPthreadAtforkAndFill(buffer); - } else { - // Since we failed to set up fork safety, we fall back to always - // calling getrandom every time. - wipe_me.init_state = .failed; - return fillWithOsEntropy(buffer); - } - } else if (std.Thread.use_pthreads) { - return setupPthreadAtforkAndFill(buffer); - } else { - // We have no mechanism to provide fork safety, but we want fork safety, - // so we fall back to calling getrandom every time. - wipe_me.init_state = .failed; - return fillWithOsEntropy(buffer); - } - } else { + if (!want_fork_safety) { return initAndFill(buffer); } + + if (maybe_have_wipe_on_fork) wof: { + // Qemu user-mode emulation ignores any valid/invalid madvise + // hint and returns success. Check if this is the case by + // passing bogus parameters, we expect EINVAL as result. + if (os.madvise(wipe_mem.ptr, 0, 0xffffffff)) |_| { + break :wof; + } else |_| {} + + os.madvise( + wipe_mem.ptr, + wipe_mem.len, + os.MADV_WIPEONFORK, + ) catch |_| { + return initAndFill(buffer); + }; + } + + if (std.Thread.use_pthreads) { + return setupPthreadAtforkAndFill(buffer); + } + + // Since we failed to set up fork safety, we fall back to always + // calling getrandom every time. + ctx.init_state = .failed; + return fillWithOsEntropy(buffer); }, .initialized => { return fillWithCsprng(buffer); @@ -110,7 +141,8 @@ fn tlsCsprngFill(_: *const std.rand.Random, buffer: []u8) void { fn setupPthreadAtforkAndFill(buffer: []u8) void { const failed = std.c.pthread_atfork(null, null, childAtForkHandler) != 0; if (failed) { - wipe_me.init_state = .failed; + const ctx = @ptrCast(*Context, wipe_mem.ptr); + ctx.init_state = .failed; return fillWithOsEntropy(buffer); } else { return initAndFill(buffer); @@ -118,21 +150,21 @@ fn setupPthreadAtforkAndFill(buffer: []u8) void { } fn childAtForkHandler() callconv(.C) void { - const wipe_slice = @ptrCast([*]u8, &wipe_me)[0..@sizeOf(@TypeOf(wipe_me))]; - std.crypto.utils.secureZero(u8, wipe_slice); + std.crypto.utils.secureZero(u8, wipe_mem); } fn fillWithCsprng(buffer: []u8) void { + const ctx = @ptrCast(*Context, wipe_mem.ptr); if (buffer.len != 0) { - wipe_me.gimli.squeeze(buffer); + ctx.gimli.squeeze(buffer); } else { - wipe_me.gimli.permute(); + ctx.gimli.permute(); } - mem.set(u8, wipe_me.gimli.toSlice()[0..std.crypto.core.Gimli.RATE], 0); + mem.set(u8, ctx.gimli.toSlice()[0..std.crypto.core.Gimli.RATE], 0); } fn fillWithOsEntropy(buffer: []u8) void { - std.os.getrandom(buffer) catch @panic("getrandom() failed to provide entropy"); + os.getrandom(buffer) catch @panic("getrandom() failed to provide entropy"); } fn initAndFill(buffer: []u8) void { @@ -147,11 +179,12 @@ fn initAndFill(buffer: []u8) void { fillWithOsEntropy(&seed); } - wipe_me.gimli = std.crypto.core.Gimli.init(seed); + const ctx = @ptrCast(*Context, wipe_mem.ptr); + ctx.gimli = std.crypto.core.Gimli.init(seed); // This is at the end so that accidental recursive dependencies result // in stack overflows instead of invalid random data. - wipe_me.init_state = .initialized; + ctx.init_state = .initialized; return fillWithCsprng(buffer); } diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index abac923cdd..614d9d0ae1 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -38,9 +38,12 @@ pub fn Complex(comptime T: type) type { /// Imaginary part. im: T, + + /// Deprecated, use init() + pub const new = init; /// Create a new Complex number from the given real and imaginary parts. - pub fn new(re: T, im: T) Self { + pub fn init(re: T, im: T) Self { return Self{ .re = re, .im = im, @@ -110,32 +113,32 @@ pub fn Complex(comptime T: type) type { const epsilon = 0.0001; test "complex.add" { - const a = Complex(f32).new(5, 3); - const b = Complex(f32).new(2, 7); + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2, 7); const c = a.add(b); try testing.expect(c.re == 7 and c.im == 10); } test "complex.sub" { - const a = Complex(f32).new(5, 3); - const b = Complex(f32).new(2, 7); + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2, 7); const c = a.sub(b); try testing.expect(c.re == 3 and c.im == -4); } test "complex.mul" { - const a = Complex(f32).new(5, 3); - const b = Complex(f32).new(2, 7); + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2, 7); const c = a.mul(b); try testing.expect(c.re == -11 and c.im == 41); } test "complex.div" { - const a = Complex(f32).new(5, 3); - const b = Complex(f32).new(2, 7); + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2, 7); const c = a.div(b); try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and @@ -143,14 +146,14 @@ test "complex.div" { } test "complex.conjugate" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = a.conjugate(); try testing.expect(c.re == 5 and c.im == -3); } test "complex.reciprocal" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = a.reciprocal(); try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 5) / 34, epsilon) and @@ -158,7 +161,7 @@ test "complex.reciprocal" { } test "complex.magnitude" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = a.magnitude(); try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon)); diff --git a/lib/std/math/complex/abs.zig b/lib/std/math/complex/abs.zig index a2678d21db..6890d15f8a 100644 --- a/lib/std/math/complex/abs.zig +++ b/lib/std/math/complex/abs.zig @@ -18,7 +18,7 @@ pub fn abs(z: anytype) @TypeOf(z.re) { const epsilon = 0.0001; test "complex.cabs" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = abs(a); try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon)); } diff --git a/lib/std/math/complex/acos.zig b/lib/std/math/complex/acos.zig index 72abea47fe..3d02ad6358 100644 --- a/lib/std/math/complex/acos.zig +++ b/lib/std/math/complex/acos.zig @@ -13,13 +13,13 @@ const Complex = cmath.Complex; pub fn acos(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); const q = cmath.asin(z); - return Complex(T).new(@as(T, math.pi) / 2 - q.re, -q.im); + return Complex(T).init(@as(T, math.pi) / 2 - q.re, -q.im); } const epsilon = 0.0001; test "complex.cacos" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = acos(a); try testing.expect(math.approxEqAbs(f32, c.re, 0.546975, epsilon)); diff --git a/lib/std/math/complex/acosh.zig b/lib/std/math/complex/acosh.zig index 4f76dea01a..c239936d47 100644 --- a/lib/std/math/complex/acosh.zig +++ b/lib/std/math/complex/acosh.zig @@ -13,13 +13,13 @@ const Complex = cmath.Complex; pub fn acosh(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); const q = cmath.acos(z); - return Complex(T).new(-q.im, q.re); + return Complex(T).init(-q.im, q.re); } const epsilon = 0.0001; test "complex.cacosh" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = acosh(a); try testing.expect(math.approxEqAbs(f32, c.re, 2.452914, epsilon)); diff --git a/lib/std/math/complex/arg.zig b/lib/std/math/complex/arg.zig index c583b9e360..8a79daa073 100644 --- a/lib/std/math/complex/arg.zig +++ b/lib/std/math/complex/arg.zig @@ -18,7 +18,7 @@ pub fn arg(z: anytype) @TypeOf(z.re) { const epsilon = 0.0001; test "complex.carg" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = arg(a); try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon)); } diff --git a/lib/std/math/complex/asin.zig b/lib/std/math/complex/asin.zig index 7ce200fae2..f2c377df56 100644 --- a/lib/std/math/complex/asin.zig +++ b/lib/std/math/complex/asin.zig @@ -15,17 +15,17 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re)) { const x = z.re; const y = z.im; - const p = Complex(T).new(1.0 - (x - y) * (x + y), -2.0 * x * y); - const q = Complex(T).new(-y, x); + const p = Complex(T).init(1.0 - (x - y) * (x + y), -2.0 * x * y); + const q = Complex(T).init(-y, x); const r = cmath.log(q.add(cmath.sqrt(p))); - return Complex(T).new(r.im, -r.re); + return Complex(T).init(r.im, -r.re); } const epsilon = 0.0001; test "complex.casin" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = asin(a); try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon)); diff --git a/lib/std/math/complex/asinh.zig b/lib/std/math/complex/asinh.zig index 821218acf7..b1083c81bf 100644 --- a/lib/std/math/complex/asinh.zig +++ b/lib/std/math/complex/asinh.zig @@ -12,15 +12,15 @@ const Complex = cmath.Complex; /// Returns the hyperbolic arc-sine of z. pub fn asinh(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); - const q = Complex(T).new(-z.im, z.re); + const q = Complex(T).init(-z.im, z.re); const r = cmath.asin(q); - return Complex(T).new(r.im, -r.re); + return Complex(T).init(r.im, -r.re); } const epsilon = 0.0001; test "complex.casinh" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = asinh(a); try testing.expect(math.approxEqAbs(f32, c.re, 2.459831, epsilon)); diff --git a/lib/std/math/complex/atan.zig b/lib/std/math/complex/atan.zig index d9a95c8dc1..b26b1e7ef3 100644 --- a/lib/std/math/complex/atan.zig +++ b/lib/std/math/complex/atan.zig @@ -50,14 +50,14 @@ fn atan32(z: Complex(f32)) Complex(f32) { if ((x == 0.0) and (y > 1.0)) { // overflow - return Complex(f32).new(maxnum, maxnum); + return Complex(f32).init(maxnum, maxnum); } const x2 = x * x; var a = 1.0 - x2 - (y * y); if (a == 0.0) { // overflow - return Complex(f32).new(maxnum, maxnum); + return Complex(f32).init(maxnum, maxnum); } var t = 0.5 * math.atan2(f32, 2.0 * x, a); @@ -67,12 +67,12 @@ fn atan32(z: Complex(f32)) Complex(f32) { a = x2 + t * t; if (a == 0.0) { // overflow - return Complex(f32).new(maxnum, maxnum); + return Complex(f32).init(maxnum, maxnum); } t = y + 1.0; a = (x2 + (t * t)) / a; - return Complex(f32).new(w, 0.25 * math.ln(a)); + return Complex(f32).init(w, 0.25 * math.ln(a)); } fn redupif64(x: f64) f64 { @@ -99,14 +99,14 @@ fn atan64(z: Complex(f64)) Complex(f64) { if ((x == 0.0) and (y > 1.0)) { // overflow - return Complex(f64).new(maxnum, maxnum); + return Complex(f64).init(maxnum, maxnum); } const x2 = x * x; var a = 1.0 - x2 - (y * y); if (a == 0.0) { // overflow - return Complex(f64).new(maxnum, maxnum); + return Complex(f64).init(maxnum, maxnum); } var t = 0.5 * math.atan2(f64, 2.0 * x, a); @@ -116,18 +116,18 @@ fn atan64(z: Complex(f64)) Complex(f64) { a = x2 + t * t; if (a == 0.0) { // overflow - return Complex(f64).new(maxnum, maxnum); + return Complex(f64).init(maxnum, maxnum); } t = y + 1.0; a = (x2 + (t * t)) / a; - return Complex(f64).new(w, 0.25 * math.ln(a)); + return Complex(f64).init(w, 0.25 * math.ln(a)); } const epsilon = 0.0001; test "complex.catan32" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = atan(a); try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon)); @@ -135,7 +135,7 @@ test "complex.catan32" { } test "complex.catan64" { - const a = Complex(f64).new(5, 3); + const a = Complex(f64).init(5, 3); const c = atan(a); try testing.expect(math.approxEqAbs(f64, c.re, 1.423679, epsilon)); diff --git a/lib/std/math/complex/atanh.zig b/lib/std/math/complex/atanh.zig index 420f401f17..41232570b5 100644 --- a/lib/std/math/complex/atanh.zig +++ b/lib/std/math/complex/atanh.zig @@ -12,15 +12,15 @@ const Complex = cmath.Complex; /// Returns the hyperbolic arc-tangent of z. pub fn atanh(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); - const q = Complex(T).new(-z.im, z.re); + const q = Complex(T).init(-z.im, z.re); const r = cmath.atan(q); - return Complex(T).new(r.im, -r.re); + return Complex(T).init(r.im, -r.re); } const epsilon = 0.0001; test "complex.catanh" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = atanh(a); try testing.expect(math.approxEqAbs(f32, c.re, 0.146947, epsilon)); diff --git a/lib/std/math/complex/conj.zig b/lib/std/math/complex/conj.zig index 960295830a..ea4ba7356c 100644 --- a/lib/std/math/complex/conj.zig +++ b/lib/std/math/complex/conj.zig @@ -12,11 +12,11 @@ const Complex = cmath.Complex; /// Returns the complex conjugate of z. pub fn conj(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); - return Complex(T).new(z.re, -z.im); + return Complex(T).init(z.re, -z.im); } test "complex.conj" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = a.conjugate(); try testing.expect(c.re == 5 and c.im == -3); diff --git a/lib/std/math/complex/cos.zig b/lib/std/math/complex/cos.zig index 2de3735d12..0760485bb0 100644 --- a/lib/std/math/complex/cos.zig +++ b/lib/std/math/complex/cos.zig @@ -12,14 +12,14 @@ const Complex = cmath.Complex; /// Returns the cosine of z. pub fn cos(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); - const p = Complex(T).new(-z.im, z.re); + const p = Complex(T).init(-z.im, z.re); return cmath.cosh(p); } const epsilon = 0.0001; test "complex.ccos" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = cos(a); try testing.expect(math.approxEqAbs(f32, c.re, 2.855815, epsilon)); diff --git a/lib/std/math/complex/cosh.zig b/lib/std/math/complex/cosh.zig index 2514e72bdb..7f82fb98d4 100644 --- a/lib/std/math/complex/cosh.zig +++ b/lib/std/math/complex/cosh.zig @@ -40,55 +40,55 @@ fn cosh32(z: Complex(f32)) Complex(f32) { if (ix < 0x7f800000 and iy < 0x7f800000) { if (iy == 0) { - return Complex(f32).new(math.cosh(x), y); + return Complex(f32).init(math.cosh(x), y); } // small x: normal case if (ix < 0x41100000) { - return Complex(f32).new(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); + return Complex(f32).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); } // |x|>= 9, so cosh(x) ~= exp(|x|) if (ix < 0x42b17218) { // x < 88.7: exp(|x|) won't overflow const h = math.exp(math.fabs(x)) * 0.5; - return Complex(f32).new(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); + return Complex(f32).init(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); } // x < 192.7: scale to avoid overflow else if (ix < 0x4340b1e7) { - const v = Complex(f32).new(math.fabs(x), y); + const v = Complex(f32).init(math.fabs(x), y); const r = ldexp_cexp(v, -1); - return Complex(f32).new(r.re, r.im * math.copysign(f32, 1, x)); + return Complex(f32).init(r.re, r.im * math.copysign(f32, 1, x)); } // x >= 192.7: result always overflows else { const h = 0x1p127 * x; - return Complex(f32).new(h * h * math.cos(y), h * math.sin(y)); + return Complex(f32).init(h * h * math.cos(y), h * math.sin(y)); } } if (ix == 0 and iy >= 0x7f800000) { - return Complex(f32).new(y - y, math.copysign(f32, 0, x * (y - y))); + return Complex(f32).init(y - y, math.copysign(f32, 0, x * (y - y))); } if (iy == 0 and ix >= 0x7f800000) { if (hx & 0x7fffff == 0) { - return Complex(f32).new(x * x, math.copysign(f32, 0, x) * y); + return Complex(f32).init(x * x, math.copysign(f32, 0, x) * y); } - return Complex(f32).new(x, math.copysign(f32, 0, (x + x) * y)); + return Complex(f32).init(x, math.copysign(f32, 0, (x + x) * y)); } if (ix < 0x7f800000 and iy >= 0x7f800000) { - return Complex(f32).new(y - y, x * (y - y)); + return Complex(f32).init(y - y, x * (y - y)); } if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) { if (iy >= 0x7f800000) { - return Complex(f32).new(x * x, x * (y - y)); + return Complex(f32).init(x * x, x * (y - y)); } - return Complex(f32).new((x * x) * math.cos(y), x * math.sin(y)); + return Complex(f32).init((x * x) * math.cos(y), x * math.sin(y)); } - return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y)); + return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y)); } fn cosh64(z: Complex(f64)) Complex(f64) { @@ -108,61 +108,61 @@ fn cosh64(z: Complex(f64)) Complex(f64) { // nearly non-exceptional case where x, y are finite if (ix < 0x7ff00000 and iy < 0x7ff00000) { if (iy | ly == 0) { - return Complex(f64).new(math.cosh(x), x * y); + return Complex(f64).init(math.cosh(x), x * y); } // small x: normal case if (ix < 0x40360000) { - return Complex(f64).new(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); + return Complex(f64).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); } // |x|>= 22, so cosh(x) ~= exp(|x|) if (ix < 0x40862e42) { // x < 710: exp(|x|) won't overflow const h = math.exp(math.fabs(x)) * 0.5; - return Complex(f64).new(h * math.cos(y), math.copysign(f64, h, x) * math.sin(y)); + return Complex(f64).init(h * math.cos(y), math.copysign(f64, h, x) * math.sin(y)); } // x < 1455: scale to avoid overflow else if (ix < 0x4096bbaa) { - const v = Complex(f64).new(math.fabs(x), y); + const v = Complex(f64).init(math.fabs(x), y); const r = ldexp_cexp(v, -1); - return Complex(f64).new(r.re, r.im * math.copysign(f64, 1, x)); + return Complex(f64).init(r.re, r.im * math.copysign(f64, 1, x)); } // x >= 1455: result always overflows else { const h = 0x1p1023; - return Complex(f64).new(h * h * math.cos(y), h * math.sin(y)); + return Complex(f64).init(h * h * math.cos(y), h * math.sin(y)); } } if (ix | lx == 0 and iy >= 0x7ff00000) { - return Complex(f64).new(y - y, math.copysign(f64, 0, x * (y - y))); + return Complex(f64).init(y - y, math.copysign(f64, 0, x * (y - y))); } if (iy | ly == 0 and ix >= 0x7ff00000) { if ((hx & 0xfffff) | lx == 0) { - return Complex(f64).new(x * x, math.copysign(f64, 0, x) * y); + return Complex(f64).init(x * x, math.copysign(f64, 0, x) * y); } - return Complex(f64).new(x * x, math.copysign(f64, 0, (x + x) * y)); + return Complex(f64).init(x * x, math.copysign(f64, 0, (x + x) * y)); } if (ix < 0x7ff00000 and iy >= 0x7ff00000) { - return Complex(f64).new(y - y, x * (y - y)); + return Complex(f64).init(y - y, x * (y - y)); } if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) { if (iy >= 0x7ff00000) { - return Complex(f64).new(x * x, x * (y - y)); + return Complex(f64).init(x * x, x * (y - y)); } - return Complex(f64).new(x * x * math.cos(y), x * math.sin(y)); + return Complex(f64).init(x * x * math.cos(y), x * math.sin(y)); } - return Complex(f64).new((x * x) * (y - y), (x + x) * (y - y)); + return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); } const epsilon = 0.0001; test "complex.ccosh32" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = cosh(a); try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon)); @@ -170,7 +170,7 @@ test "complex.ccosh32" { } test "complex.ccosh64" { - const a = Complex(f64).new(5, 3); + const a = Complex(f64).init(5, 3); const c = cosh(a); try testing.expect(math.approxEqAbs(f64, c.re, -73.467300, epsilon)); diff --git a/lib/std/math/complex/exp.zig b/lib/std/math/complex/exp.zig index a7eb3d51b5..fa74300dea 100644 --- a/lib/std/math/complex/exp.zig +++ b/lib/std/math/complex/exp.zig @@ -39,25 +39,25 @@ fn exp32(z: Complex(f32)) Complex(f32) { const hy = @bitCast(u32, y) & 0x7fffffff; // cexp(x + i0) = exp(x) + i0 if (hy == 0) { - return Complex(f32).new(math.exp(x), y); + return Complex(f32).init(math.exp(x), y); } const hx = @bitCast(u32, x); // cexp(0 + iy) = cos(y) + isin(y) if ((hx & 0x7fffffff) == 0) { - return Complex(f32).new(math.cos(y), math.sin(y)); + return Complex(f32).init(math.cos(y), math.sin(y)); } if (hy >= 0x7f800000) { // cexp(finite|nan +- i inf|nan) = nan + i nan if ((hx & 0x7fffffff) != 0x7f800000) { - return Complex(f32).new(y - y, y - y); + return Complex(f32).init(y - y, y - y); } // cexp(-inf +- i inf|nan) = 0 + i0 else if (hx & 0x80000000 != 0) { - return Complex(f32).new(0, 0); + return Complex(f32).init(0, 0); } // cexp(+inf +- i inf|nan) = inf + i nan else { - return Complex(f32).new(x, y - y); + return Complex(f32).init(x, y - y); } } @@ -70,7 +70,7 @@ fn exp32(z: Complex(f32)) Complex(f32) { // - x = nan else { const exp_x = math.exp(x); - return Complex(f32).new(exp_x * math.cos(y), exp_x * math.sin(y)); + return Complex(f32).init(exp_x * math.cos(y), exp_x * math.sin(y)); } } @@ -87,7 +87,7 @@ fn exp64(z: Complex(f64)) Complex(f64) { // cexp(x + i0) = exp(x) + i0 if (hy | ly == 0) { - return Complex(f64).new(math.exp(x), y); + return Complex(f64).init(math.exp(x), y); } const fx = @bitCast(u64, x); @@ -96,19 +96,19 @@ fn exp64(z: Complex(f64)) Complex(f64) { // cexp(0 + iy) = cos(y) + isin(y) if ((hx & 0x7fffffff) | lx == 0) { - return Complex(f64).new(math.cos(y), math.sin(y)); + return Complex(f64).init(math.cos(y), math.sin(y)); } if (hy >= 0x7ff00000) { // cexp(finite|nan +- i inf|nan) = nan + i nan if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) { - return Complex(f64).new(y - y, y - y); + return Complex(f64).init(y - y, y - y); } // cexp(-inf +- i inf|nan) = 0 + i0 else if (hx & 0x80000000 != 0) { - return Complex(f64).new(0, 0); + return Complex(f64).init(0, 0); } // cexp(+inf +- i inf|nan) = inf + i nan else { - return Complex(f64).new(x, y - y); + return Complex(f64).init(x, y - y); } } @@ -121,14 +121,14 @@ fn exp64(z: Complex(f64)) Complex(f64) { // - x = nan else { const exp_x = math.exp(x); - return Complex(f64).new(exp_x * math.cos(y), exp_x * math.sin(y)); + return Complex(f64).init(exp_x * math.cos(y), exp_x * math.sin(y)); } } const epsilon = 0.0001; test "complex.cexp32" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = exp(a); try testing.expect(math.approxEqAbs(f32, c.re, -146.927917, epsilon)); @@ -136,7 +136,7 @@ test "complex.cexp32" { } test "complex.cexp64" { - const a = Complex(f64).new(5, 3); + const a = Complex(f64).init(5, 3); const c = exp(a); try testing.expect(math.approxEqAbs(f64, c.re, -146.927917, epsilon)); diff --git a/lib/std/math/complex/ldexp.zig b/lib/std/math/complex/ldexp.zig index 3ae0382fe3..9f31aae549 100644 --- a/lib/std/math/complex/ldexp.zig +++ b/lib/std/math/complex/ldexp.zig @@ -48,7 +48,7 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) { const half_expt2 = exptf - half_expt1; const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23); - return Complex(f32).new(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2); + return Complex(f32).init(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2); } fn frexp_exp64(x: f64, expt: *i32) f64 { @@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { const half_expt2 = exptf - half_expt1; const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20); - return Complex(f64).new( + return Complex(f64).init( math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2, ); diff --git a/lib/std/math/complex/log.zig b/lib/std/math/complex/log.zig index 7f8f649953..e59870d556 100644 --- a/lib/std/math/complex/log.zig +++ b/lib/std/math/complex/log.zig @@ -15,13 +15,13 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re)) { const r = cmath.abs(z); const phi = cmath.arg(z); - return Complex(T).new(math.ln(r), phi); + return Complex(T).init(math.ln(r), phi); } const epsilon = 0.0001; test "complex.clog" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = log(a); try testing.expect(math.approxEqAbs(f32, c.re, 1.763180, epsilon)); diff --git a/lib/std/math/complex/pow.zig b/lib/std/math/complex/pow.zig index 0d3a45e6d2..092c8c2422 100644 --- a/lib/std/math/complex/pow.zig +++ b/lib/std/math/complex/pow.zig @@ -19,8 +19,8 @@ pub fn pow(comptime T: type, z: T, c: T) T { const epsilon = 0.0001; test "complex.cpow" { - const a = Complex(f32).new(5, 3); - const b = Complex(f32).new(2.3, -1.3); + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2.3, -1.3); const c = pow(Complex(f32), a, b); try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon)); diff --git a/lib/std/math/complex/proj.zig b/lib/std/math/complex/proj.zig index 260816481b..8527be2293 100644 --- a/lib/std/math/complex/proj.zig +++ b/lib/std/math/complex/proj.zig @@ -14,16 +14,16 @@ pub fn proj(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); if (math.isInf(z.re) or math.isInf(z.im)) { - return Complex(T).new(math.inf(T), math.copysign(T, 0, z.re)); + return Complex(T).init(math.inf(T), math.copysign(T, 0, z.re)); } - return Complex(T).new(z.re, z.im); + return Complex(T).init(z.re, z.im); } const epsilon = 0.0001; test "complex.cproj" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = proj(a); try testing.expect(c.re == 5 and c.im == 3); diff --git a/lib/std/math/complex/sin.zig b/lib/std/math/complex/sin.zig index 68551b8596..39b5f584ac 100644 --- a/lib/std/math/complex/sin.zig +++ b/lib/std/math/complex/sin.zig @@ -12,15 +12,15 @@ const Complex = cmath.Complex; /// Returns the sine of z. pub fn sin(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); - const p = Complex(T).new(-z.im, z.re); + const p = Complex(T).init(-z.im, z.re); const q = cmath.sinh(p); - return Complex(T).new(q.im, -q.re); + return Complex(T).init(q.im, -q.re); } const epsilon = 0.0001; test "complex.csin" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = sin(a); try testing.expect(math.approxEqAbs(f32, c.re, -9.654126, epsilon)); diff --git a/lib/std/math/complex/sinh.zig b/lib/std/math/complex/sinh.zig index ea09f8e17d..b673da3d2f 100644 --- a/lib/std/math/complex/sinh.zig +++ b/lib/std/math/complex/sinh.zig @@ -40,55 +40,55 @@ fn sinh32(z: Complex(f32)) Complex(f32) { if (ix < 0x7f800000 and iy < 0x7f800000) { if (iy == 0) { - return Complex(f32).new(math.sinh(x), y); + return Complex(f32).init(math.sinh(x), y); } // small x: normal case if (ix < 0x41100000) { - return Complex(f32).new(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); + return Complex(f32).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); } // |x|>= 9, so cosh(x) ~= exp(|x|) if (ix < 0x42b17218) { // x < 88.7: exp(|x|) won't overflow const h = math.exp(math.fabs(x)) * 0.5; - return Complex(f32).new(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); + return Complex(f32).init(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); } // x < 192.7: scale to avoid overflow else if (ix < 0x4340b1e7) { - const v = Complex(f32).new(math.fabs(x), y); + const v = Complex(f32).init(math.fabs(x), y); const r = ldexp_cexp(v, -1); - return Complex(f32).new(r.re * math.copysign(f32, 1, x), r.im); + return Complex(f32).init(r.re * math.copysign(f32, 1, x), r.im); } // x >= 192.7: result always overflows else { const h = 0x1p127 * x; - return Complex(f32).new(h * math.cos(y), h * h * math.sin(y)); + return Complex(f32).init(h * math.cos(y), h * h * math.sin(y)); } } if (ix == 0 and iy >= 0x7f800000) { - return Complex(f32).new(math.copysign(f32, 0, x * (y - y)), y - y); + return Complex(f32).init(math.copysign(f32, 0, x * (y - y)), y - y); } if (iy == 0 and ix >= 0x7f800000) { if (hx & 0x7fffff == 0) { - return Complex(f32).new(x, y); + return Complex(f32).init(x, y); } - return Complex(f32).new(x, math.copysign(f32, 0, y)); + return Complex(f32).init(x, math.copysign(f32, 0, y)); } if (ix < 0x7f800000 and iy >= 0x7f800000) { - return Complex(f32).new(y - y, x * (y - y)); + return Complex(f32).init(y - y, x * (y - y)); } if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) { if (iy >= 0x7f800000) { - return Complex(f32).new(x * x, x * (y - y)); + return Complex(f32).init(x * x, x * (y - y)); } - return Complex(f32).new(x * math.cos(y), math.inf_f32 * math.sin(y)); + return Complex(f32).init(x * math.cos(y), math.inf_f32 * math.sin(y)); } - return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y)); + return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y)); } fn sinh64(z: Complex(f64)) Complex(f64) { @@ -107,61 +107,61 @@ fn sinh64(z: Complex(f64)) Complex(f64) { if (ix < 0x7ff00000 and iy < 0x7ff00000) { if (iy | ly == 0) { - return Complex(f64).new(math.sinh(x), y); + return Complex(f64).init(math.sinh(x), y); } // small x: normal case if (ix < 0x40360000) { - return Complex(f64).new(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); + return Complex(f64).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); } // |x|>= 22, so cosh(x) ~= exp(|x|) if (ix < 0x40862e42) { // x < 710: exp(|x|) won't overflow const h = math.exp(math.fabs(x)) * 0.5; - return Complex(f64).new(math.copysign(f64, h, x) * math.cos(y), h * math.sin(y)); + return Complex(f64).init(math.copysign(f64, h, x) * math.cos(y), h * math.sin(y)); } // x < 1455: scale to avoid overflow else if (ix < 0x4096bbaa) { - const v = Complex(f64).new(math.fabs(x), y); + const v = Complex(f64).init(math.fabs(x), y); const r = ldexp_cexp(v, -1); - return Complex(f64).new(r.re * math.copysign(f64, 1, x), r.im); + return Complex(f64).init(r.re * math.copysign(f64, 1, x), r.im); } // x >= 1455: result always overflows else { const h = 0x1p1023 * x; - return Complex(f64).new(h * math.cos(y), h * h * math.sin(y)); + return Complex(f64).init(h * math.cos(y), h * h * math.sin(y)); } } if (ix | lx == 0 and iy >= 0x7ff00000) { - return Complex(f64).new(math.copysign(f64, 0, x * (y - y)), y - y); + return Complex(f64).init(math.copysign(f64, 0, x * (y - y)), y - y); } if (iy | ly == 0 and ix >= 0x7ff00000) { if ((hx & 0xfffff) | lx == 0) { - return Complex(f64).new(x, y); + return Complex(f64).init(x, y); } - return Complex(f64).new(x, math.copysign(f64, 0, y)); + return Complex(f64).init(x, math.copysign(f64, 0, y)); } if (ix < 0x7ff00000 and iy >= 0x7ff00000) { - return Complex(f64).new(y - y, x * (y - y)); + return Complex(f64).init(y - y, x * (y - y)); } if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) { if (iy >= 0x7ff00000) { - return Complex(f64).new(x * x, x * (y - y)); + return Complex(f64).init(x * x, x * (y - y)); } - return Complex(f64).new(x * math.cos(y), math.inf_f64 * math.sin(y)); + return Complex(f64).init(x * math.cos(y), math.inf_f64 * math.sin(y)); } - return Complex(f64).new((x * x) * (y - y), (x + x) * (y - y)); + return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); } const epsilon = 0.0001; test "complex.csinh32" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = sinh(a); try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon)); @@ -169,7 +169,7 @@ test "complex.csinh32" { } test "complex.csinh64" { - const a = Complex(f64).new(5, 3); + const a = Complex(f64).init(5, 3); const c = sinh(a); try testing.expect(math.approxEqAbs(f64, c.re, -73.460617, epsilon)); diff --git a/lib/std/math/complex/sqrt.zig b/lib/std/math/complex/sqrt.zig index 25c486f838..07fed152fa 100644 --- a/lib/std/math/complex/sqrt.zig +++ b/lib/std/math/complex/sqrt.zig @@ -32,15 +32,15 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { const y = z.im; if (x == 0 and y == 0) { - return Complex(f32).new(0, y); + return Complex(f32).init(0, y); } if (math.isInf(y)) { - return Complex(f32).new(math.inf(f32), y); + return Complex(f32).init(math.inf(f32), y); } if (math.isNan(x)) { // raise invalid if y is not nan const t = (y - y) / (y - y); - return Complex(f32).new(x, t); + return Complex(f32).init(x, t); } if (math.isInf(x)) { // sqrt(inf + i nan) = inf + nan i @@ -48,9 +48,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { // sqrt(-inf + i nan) = nan +- inf i // sqrt(-inf + iy) = 0 + inf i if (math.signbit(x)) { - return Complex(f32).new(math.fabs(x - y), math.copysign(f32, x, y)); + return Complex(f32).init(math.fabs(x - y), math.copysign(f32, x, y)); } else { - return Complex(f32).new(x, math.copysign(f32, y - y, y)); + return Complex(f32).init(x, math.copysign(f32, y - y, y)); } } @@ -62,13 +62,13 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { if (dx >= 0) { const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5); - return Complex(f32).new( + return Complex(f32).init( @floatCast(f32, t), @floatCast(f32, dy / (2.0 * t)), ); } else { const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5); - return Complex(f32).new( + return Complex(f32).init( @floatCast(f32, math.fabs(y) / (2.0 * t)), @floatCast(f32, math.copysign(f64, t, y)), ); @@ -83,15 +83,15 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { var y = z.im; if (x == 0 and y == 0) { - return Complex(f64).new(0, y); + return Complex(f64).init(0, y); } if (math.isInf(y)) { - return Complex(f64).new(math.inf(f64), y); + return Complex(f64).init(math.inf(f64), y); } if (math.isNan(x)) { // raise invalid if y is not nan const t = (y - y) / (y - y); - return Complex(f64).new(x, t); + return Complex(f64).init(x, t); } if (math.isInf(x)) { // sqrt(inf + i nan) = inf + nan i @@ -99,9 +99,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { // sqrt(-inf + i nan) = nan +- inf i // sqrt(-inf + iy) = 0 + inf i if (math.signbit(x)) { - return Complex(f64).new(math.fabs(x - y), math.copysign(f64, x, y)); + return Complex(f64).init(math.fabs(x - y), math.copysign(f64, x, y)); } else { - return Complex(f64).new(x, math.copysign(f64, y - y, y)); + return Complex(f64).init(x, math.copysign(f64, y - y, y)); } } @@ -118,10 +118,10 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { var result: Complex(f64) = undefined; if (x >= 0) { const t = math.sqrt((x + math.hypot(f64, x, y)) * 0.5); - result = Complex(f64).new(t, y / (2.0 * t)); + result = Complex(f64).init(t, y / (2.0 * t)); } else { const t = math.sqrt((-x + math.hypot(f64, x, y)) * 0.5); - result = Complex(f64).new(math.fabs(y) / (2.0 * t), math.copysign(f64, t, y)); + result = Complex(f64).init(math.fabs(y) / (2.0 * t), math.copysign(f64, t, y)); } if (scale) { @@ -135,7 +135,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { const epsilon = 0.0001; test "complex.csqrt32" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = sqrt(a); try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon)); @@ -143,7 +143,7 @@ test "complex.csqrt32" { } test "complex.csqrt64" { - const a = Complex(f64).new(5, 3); + const a = Complex(f64).init(5, 3); const c = sqrt(a); try testing.expect(math.approxEqAbs(f64, c.re, 2.3271175190399496, epsilon)); diff --git a/lib/std/math/complex/tan.zig b/lib/std/math/complex/tan.zig index ca9d4ce7e9..0ee34dfcc2 100644 --- a/lib/std/math/complex/tan.zig +++ b/lib/std/math/complex/tan.zig @@ -12,15 +12,15 @@ const Complex = cmath.Complex; /// Returns the tanget of z. pub fn tan(z: anytype) Complex(@TypeOf(z.re)) { const T = @TypeOf(z.re); - const q = Complex(T).new(-z.im, z.re); + const q = Complex(T).init(-z.im, z.re); const r = cmath.tanh(q); - return Complex(T).new(r.im, -r.re); + return Complex(T).init(r.im, -r.re); } const epsilon = 0.0001; test "complex.ctan" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = tan(a); try testing.expect(math.approxEqAbs(f32, c.re, -0.002708233, epsilon)); diff --git a/lib/std/math/complex/tanh.zig b/lib/std/math/complex/tanh.zig index 096bdaac06..cb5e7b3f6d 100644 --- a/lib/std/math/complex/tanh.zig +++ b/lib/std/math/complex/tanh.zig @@ -36,22 +36,22 @@ fn tanh32(z: Complex(f32)) Complex(f32) { if (ix >= 0x7f800000) { if (ix & 0x7fffff != 0) { const r = if (y == 0) y else x * y; - return Complex(f32).new(x, r); + return Complex(f32).init(x, r); } const xx = @bitCast(f32, hx - 0x40000000); const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y); - return Complex(f32).new(xx, math.copysign(f32, 0, r)); + return Complex(f32).init(xx, math.copysign(f32, 0, r)); } if (!math.isFinite(y)) { const r = if (ix != 0) y - y else x; - return Complex(f32).new(r, y - y); + return Complex(f32).init(r, y - y); } // x >= 11 if (ix >= 0x41300000) { const exp_mx = math.exp(-math.fabs(x)); - return Complex(f32).new(math.copysign(f32, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); + return Complex(f32).init(math.copysign(f32, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); } // Kahan's algorithm @@ -61,7 +61,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { const rho = math.sqrt(1 + s * s); const den = 1 + beta * s * s; - return Complex(f32).new((beta * rho * s) / den, t / den); + return Complex(f32).init((beta * rho * s) / den, t / den); } fn tanh64(z: Complex(f64)) Complex(f64) { @@ -78,23 +78,23 @@ fn tanh64(z: Complex(f64)) Complex(f64) { if (ix >= 0x7ff00000) { if ((ix & 0x7fffff) | lx != 0) { const r = if (y == 0) y else x * y; - return Complex(f64).new(x, r); + return Complex(f64).init(x, r); } const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx); const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y); - return Complex(f64).new(xx, math.copysign(f64, 0, r)); + return Complex(f64).init(xx, math.copysign(f64, 0, r)); } if (!math.isFinite(y)) { const r = if (ix != 0) y - y else x; - return Complex(f64).new(r, y - y); + return Complex(f64).init(r, y - y); } // x >= 22 if (ix >= 0x40360000) { const exp_mx = math.exp(-math.fabs(x)); - return Complex(f64).new(math.copysign(f64, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); + return Complex(f64).init(math.copysign(f64, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); } // Kahan's algorithm @@ -104,13 +104,13 @@ fn tanh64(z: Complex(f64)) Complex(f64) { const rho = math.sqrt(1 + s * s); const den = 1 + beta * s * s; - return Complex(f64).new((beta * rho * s) / den, t / den); + return Complex(f64).init((beta * rho * s) / den, t / den); } const epsilon = 0.0001; test "complex.ctanh32" { - const a = Complex(f32).new(5, 3); + const a = Complex(f32).init(5, 3); const c = tanh(a); try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon)); @@ -118,7 +118,7 @@ test "complex.ctanh32" { } test "complex.ctanh64" { - const a = Complex(f64).new(5, 3); + const a = Complex(f64).init(5, 3); const c = tanh(a); try testing.expect(math.approxEqAbs(f64, c.re, 0.999913, epsilon)); diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig index 2bf2e6a0a5..b9bb6c332d 100644 --- a/lib/std/meta/trait.zig +++ b/lib/std/meta/trait.zig @@ -298,20 +298,6 @@ pub fn isNumber(comptime T: type) bool { }; } -pub fn isIntegerNumber(comptime T: type) bool { - return switch (@typeInfo(T)) { - .Int, .ComptimeInt => true, - else => false, - }; -} - -pub fn isFloatingNumber(comptime T: type) bool { - return switch (@typeInfo(T)) { - .Float, .ComptimeFloat => true, - else => false, - }; -} - test "std.meta.trait.isNumber" { const NotANumber = struct { number: u8, diff --git a/lib/std/os.zig b/lib/std/os.zig index 0ee20d7c8e..5df2c441cd 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1244,6 +1244,7 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: u32, mode: mode_t) EFAULT => unreachable, EINVAL => unreachable, + EBADF => unreachable, EACCES => return error.AccessDenied, EFBIG => return error.FileTooBig, EOVERFLOW => return error.FileTooBig, diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 03586bc777..b2ce8df73f 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -1608,13 +1608,13 @@ test "zig fmt: if-else with comment before else" { \\comptime { \\ // cexp(finite|nan +- i inf|nan) = nan + i nan \\ if ((hx & 0x7fffffff) != 0x7f800000) { - \\ return Complex(f32).new(y - y, y - y); + \\ return Complex(f32).init(y - y, y - y); \\ } // cexp(-inf +- i inf|nan) = 0 + i0 \\ else if (hx & 0x80000000 != 0) { - \\ return Complex(f32).new(0, 0); + \\ return Complex(f32).init(0, 0); \\ } // cexp(+inf +- i inf|nan) = inf + i nan \\ else { - \\ return Complex(f32).new(x, y - y); + \\ return Complex(f32).init(x, y - y); \\ } \\} \\ @@ -2267,16 +2267,16 @@ test "zig fmt: line comment between if block and else keyword" { \\test "aoeu" { \\ // cexp(finite|nan +- i inf|nan) = nan + i nan \\ if ((hx & 0x7fffffff) != 0x7f800000) { - \\ return Complex(f32).new(y - y, y - y); + \\ return Complex(f32).init(y - y, y - y); \\ } \\ // cexp(-inf +- i inf|nan) = 0 + i0 \\ else if (hx & 0x80000000 != 0) { - \\ return Complex(f32).new(0, 0); + \\ return Complex(f32).init(0, 0); \\ } \\ // cexp(+inf +- i inf|nan) = inf + i nan \\ // another comment \\ else { - \\ return Complex(f32).new(x, y - y); + \\ return Complex(f32).init(x, y - y); \\ } \\} \\ |
