mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
05915b85dd
These are the standard complex multiplication/division functions required by the C standard (Annex G). Don't get me started on the standard's handling of complex-infinity...
78 lines
2.3 KiB
Zig
78 lines
2.3 KiB
Zig
const std = @import("std");
|
|
const math = std.math;
|
|
const expect = std.testing.expect;
|
|
|
|
const Complex = @import("./mulc3.zig").Complex;
|
|
const __divhc3 = @import("./divhc3.zig").__divhc3;
|
|
const __divsc3 = @import("./divsc3.zig").__divsc3;
|
|
const __divdc3 = @import("./divdc3.zig").__divdc3;
|
|
const __divxc3 = @import("./divxc3.zig").__divxc3;
|
|
const __divtc3 = @import("./divtc3.zig").__divtc3;
|
|
|
|
test {
|
|
try testDiv(f16, __divhc3);
|
|
try testDiv(f32, __divsc3);
|
|
try testDiv(f64, __divdc3);
|
|
try testDiv(f80, __divxc3);
|
|
try testDiv(f128, __divtc3);
|
|
}
|
|
|
|
fn testDiv(comptime T: type, comptime f: fn (T, T, T, T) callconv(.C) Complex(T)) !void {
|
|
{
|
|
var a: T = 1.0;
|
|
var b: T = 0.0;
|
|
var c: T = -1.0;
|
|
var d: T = 0.0;
|
|
|
|
const result = f(a, b, c, d);
|
|
try expect(result.real == -1.0);
|
|
try expect(result.imag == 0.0);
|
|
}
|
|
{
|
|
var a: T = 1.0;
|
|
var b: T = 0.0;
|
|
var c: T = -4.0;
|
|
var d: T = 0.0;
|
|
|
|
const result = f(a, b, c, d);
|
|
try expect(result.real == -0.25);
|
|
try expect(result.imag == 0.0);
|
|
}
|
|
{
|
|
// if the first operand is an infinity and the second operand is a finite number, then the
|
|
// result of the / operator is an infinity;
|
|
var a: T = -math.inf(T);
|
|
var b: T = 0.0;
|
|
var c: T = -4.0;
|
|
var d: T = 1.0;
|
|
|
|
const result = f(a, b, c, d);
|
|
try expect(result.real == math.inf(T));
|
|
try expect(result.imag == math.inf(T));
|
|
}
|
|
{
|
|
// if the first operand is a finite number and the second operand is an infinity, then the
|
|
// result of the / operator is a zero;
|
|
var a: T = 17.2;
|
|
var b: T = 0.0;
|
|
var c: T = -math.inf(T);
|
|
var d: T = 0.0;
|
|
|
|
const result = f(a, b, c, d);
|
|
try expect(result.real == -0.0);
|
|
try expect(result.imag == 0.0);
|
|
}
|
|
{
|
|
// if the first operand is a nonzero finite number or an infinity and the second operand is
|
|
// a zero, then the result of the / operator is an infinity
|
|
var a: T = 1.1;
|
|
var b: T = 0.1;
|
|
var c: T = 0.0;
|
|
var d: T = 0.0;
|
|
|
|
const result = f(a, b, c, d);
|
|
try expect(result.real == math.inf(T));
|
|
try expect(result.imag == math.inf(T));
|
|
}
|
|
}
|