authorgravatar for wink@saville.comWink Saville <wink@saville.com> 2018-11-25 10:10:44-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-28 23:13:12-05:00
log078a0a6999a916def004211a4c35a9e1b32ae355
treecd25153d0431236a7e6aeedd5439518dcf78d0a4
parentc54fe0d3ae867744bf1c0b27202f779fffc5ac7f

Add math min/max for Float and Value


1 files changed, 89 insertions(+), 0 deletions(-)

std/math/index.zig+89
...@@ -777,3 +777,92 @@ test "max value type" {...@@ -777,3 +777,92 @@ test "max value type" {
777 const x: u32 = maxInt(i32);777 const x: u32 = maxInt(i32);
778 assert(x == 2147483647);778 assert(x == 2147483647);
779}779}
780
781pub fn maxFloat(comptime T: type) T {
782 return switch (T) {
783 f16 => f16_max,
784 f32 => f32_max,
785 f64 => f64_max,
786 else => @compileError("Expecting type to be a float"),
787 };
788}
789
790test "math.maxFloat" {
791 assert(maxFloat(f16) == f16_max);
792 assert(maxFloat(f32) == f32_max);
793 assert(maxFloat(f64) == f64_max);
794}
795
796pub fn minFloat(comptime T: type) T {
797 return switch (T) {
798 f16 => f16_min,
799 f32 => f32_min,
800 f64 => f64_min,
801 else => @compileError("Expecting type to be a float"),
802 };
803}
804
805test "math.minFloat" {
806 assert(minFloat(f16) == f16_min);
807 assert(minFloat(f32) == f32_min);
808 assert(minFloat(f64) == f64_min);
809}
810
811pub fn maxValue(comptime T: type) T {
812 return switch (@typeId(T)) {
813 TypeId.Int => maxInt(T),
814 TypeId.Float => maxFloat(T),
815 else => @compileError("Expecting type to be a float or int"),
816 };
817}
818
819test "math.maxValue" {
820 assert(maxValue(u0) == 0);
821 assert(maxValue(u1) == 1);
822 assert(maxValue(u8) == 255);
823 assert(maxValue(u16) == 65535);
824 assert(maxValue(u32) == 4294967295);
825 assert(maxValue(u64) == 18446744073709551615);
826
827 assert(maxValue(i0) == 0);
828 assert(maxValue(i1) == 0);
829 assert(maxValue(i8) == 127);
830 assert(maxValue(i16) == 32767);
831 assert(maxValue(i32) == 2147483647);
832 assert(maxValue(i63) == 4611686018427387903);
833 assert(maxValue(i64) == 9223372036854775807);
834
835 assert(maxValue(f16) == f16_max);
836 assert(maxValue(f32) == f32_max);
837 assert(maxValue(f64) == f64_max);
838}
839
840pub fn minValue(comptime T: type) T {
841 return switch (@typeId(T)) {
842 TypeId.Int => minInt(T),
843 TypeId.Float => minFloat(T),
844 else => @compileError("Expecting type to be a float or int"),
845 };
846}
847
848test "math.minValue" {
849 assert(minValue(u0) == 0);
850 assert(minValue(u1) == 0);
851 assert(minValue(u8) == 0);
852 assert(minValue(u16) == 0);
853 assert(minValue(u32) == 0);
854 assert(minValue(u63) == 0);
855 assert(minValue(u64) == 0);
856
857 assert(minValue(i0) == 0);
858 assert(minValue(i1) == -1);
859 assert(minValue(i8) == -128);
860 assert(minValue(i16) == -32768);
861 assert(minValue(i32) == -2147483648);
862 assert(minValue(i63) == -4611686018427387904);
863 assert(minValue(i64) == -9223372036854775808);
864
865 assert(minValue(f16) == f16_min);
866 assert(minValue(f32) == f32_min);
867 assert(minValue(f64) == f64_min);
868}