hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

trig.ha (5724B)


      1 @test fn trig_reduce() void = {
      2 	for (let idx = 0z; idx < 10; idx += 1) {
      3 		const reduced = trig_reduce(TEST_INPUTS[idx]);
      4 		const j = reduced.0;
      5 		const z = reduced.1;
      6 		const xred = (j: i64: f64) * (PI / 4f64) + z;
      7 		assert(isclose(
      8 			sinf64(TEST_INPUTS[idx]),
      9 			sinf64(xred)));
     10 	};
     11 };
     12 
     13 @test fn cos() void = {
     14 	for (let idx = 0z; idx < 10; idx += 1) {
     15 		assert(isclose(
     16 			cosf64(TEST_INPUTS[idx]), TEST_COS[idx]));
     17 	};
     18 	assert(isnan(cosf64(-INF)));
     19 	assert(isnan(cosf64(INF)));
     20 	assert(isnan(cosf64(NAN)));
     21 };
     22 
     23 @test fn sin() void = {
     24 	for (let idx = 0z; idx < 10; idx += 1) {
     25 		assert(isclose(
     26 			sinf64(TEST_INPUTS[idx]), TEST_SIN[idx]));
     27 	};
     28 	assert(isnan(sinf64(-INF)));
     29 	assert(sinf64(-0f64) == -0f64);
     30 	assert(sinf64(0f64) == 0f64);
     31 	assert(isnan(sinf64(INF)));
     32 	assert(isnan(sinf64(NAN)));
     33 };
     34 
     35 @test fn tan() void = {
     36 	for (let idx = 0z; idx < 10; idx += 1) {
     37 		assert(isclose(
     38 			tanf64(TEST_INPUTS[idx]), TEST_TAN[idx]));
     39 	};
     40 	assert(isnan(sinf64(-INF)));
     41 	assert(sinf64(-0f64) == -0f64);
     42 	assert(sinf64(0f64) == 0f64);
     43 	assert(isnan(sinf64(INF)));
     44 	assert(isnan(sinf64(NAN)));
     45 };
     46 
     47 @test fn asin() void = {
     48 	for (let idx = 0z; idx < 10; idx += 1) {
     49 		assert(isclose(
     50 			asinf64(TEST_INPUTS[idx] / 10f64),
     51 			TEST_ASIN[idx]));
     52 	};
     53 	assert(isnan(asinf64(-PI)));
     54 	assert(asinf64(-0f64) == -0f64);
     55 	assert(asinf64(0f64) == 0f64);
     56 	assert(isnan(asinf64(PI)));
     57 	assert(isnan(asinf64(NAN)));
     58 };
     59 
     60 @test fn acos() void = {
     61 	for (let idx = 0z; idx < 10; idx += 1) {
     62 		assert(isclose(
     63 			acosf64(TEST_INPUTS[idx] / 10f64),
     64 			TEST_ACOS[idx]));
     65 	};
     66 	assert(isnan(acosf64(-PI)));
     67 	assert(acosf64(1f64) == 0f64);
     68 	assert(isnan(acosf64(PI)));
     69 	assert(isnan(acosf64(NAN)));
     70 };
     71 
     72 @test fn atan() void = {
     73 	for (let idx = 0z; idx < 10; idx += 1) {
     74 		assert(isclose(
     75 			atanf64(TEST_INPUTS[idx]),
     76 			TEST_ATAN[idx]));
     77 	};
     78 	assert(atanf64(-INF) == -PI / 2f64);
     79 	assert(atanf64(-0f64) == -0f64);
     80 	assert(atanf64(0f64) == 0f64);
     81 	assert(atanf64(INF) == PI / 2f64);
     82 	assert(isnan(atanf64(NAN)));
     83 };
     84 
     85 @test fn sinh() void = {
     86 	for (let idx = 0z; idx < 10; idx += 1) {
     87 		assert(eqwithin(
     88 			sinhf64(TEST_INPUTS[idx]),
     89 			TEST_SINH[idx],
     90 			1e-6f64));
     91 	};
     92 	assert(sinhf64(-INF) == -INF);
     93 	assert(sinhf64(-0f64) == -0f64);
     94 	assert(sinhf64(0f64) == 0f64);
     95 	assert(sinhf64(INF) == INF);
     96 	assert(isnan(sinhf64(NAN)));
     97 };
     98 
     99 @test fn cosh() void = {
    100 	for (let idx = 0z; idx < 10; idx += 1) {
    101 		assert(isclose(
    102 			coshf64(TEST_INPUTS[idx]),
    103 			TEST_COSH[idx]));
    104 	};
    105 	assert(coshf64(-INF) == INF);
    106 	assert(coshf64(-0f64) == 1f64);
    107 	assert(coshf64(0f64) == 1f64);
    108 	assert(coshf64(INF) == INF);
    109 	assert(isnan(coshf64(NAN)));
    110 };
    111 
    112 @test fn tanh() void = {
    113 	for (let idx = 0z; idx < 10; idx += 1) {
    114 		assert(isclose(
    115 			tanhf64(TEST_INPUTS[idx]),
    116 			TEST_TANH[idx]));
    117 	};
    118 	assert(tanhf64(-INF) == -1f64);
    119 	assert(tanhf64(-0f64) == -0f64);
    120 	assert(tanhf64(0f64) == 0f64);
    121 	assert(tanhf64(INF) == 1f64);
    122 	assert(isnan(tanhf64(NAN)));
    123 };
    124 
    125 @test fn asinh() void = {
    126 	for (let idx = 0z; idx < 10; idx += 1) {
    127 		assert(isclose(
    128 			asinhf64(TEST_INPUTS[idx]),
    129 			TEST_ASINH[idx]));
    130 	};
    131 	assert(asinhf64(-INF) == -INF);
    132 	assert(asinhf64(-0f64) == -0f64);
    133 	assert(asinhf64(0f64) == 0f64);
    134 	assert(asinhf64(INF) == INF);
    135 	assert(isnan(asinhf64(NAN)));
    136 };
    137 
    138 @test fn acosh() void = {
    139 	for (let idx = 0z; idx < 10; idx += 1) {
    140 		assert(isclose(
    141 			acoshf64(1f64 + absf64(TEST_INPUTS[idx])),
    142 			TEST_ACOSH[idx]));
    143 	};
    144 	assert(isnan(acoshf64(-INF)));
    145 	assert(isnan(acoshf64(0.5f64)));
    146 	assert(acoshf64(1f64) == 0f64);
    147 	assert(acoshf64(INF) == INF);
    148 	assert(isnan(acoshf64(NAN)));
    149 };
    150 
    151 @test fn atanh() void = {
    152 	for (let idx = 0z; idx < 10; idx += 1) {
    153 		assert(isclose(
    154 			atanhf64(TEST_INPUTS[idx] / 10f64),
    155 			TEST_ATANH[idx]));
    156 	};
    157 	assert(isnan(atanhf64(-INF)));
    158 	assert(isnan(atanhf64(-PI)));
    159 	assert(atanhf64(-1f64) == -INF);
    160 	assert(atanhf64(-0f64) == -0f64);
    161 	assert(atanhf64(0f64) == 0f64);
    162 	assert(atanhf64(1f64) == INF);
    163 	assert(isnan(atanhf64(PI)));
    164 	assert(isnan(atanhf64(INF)));
    165 	assert(isnan(atanhf64(NAN)));
    166 };
    167 
    168 @test fn atan2() void = {
    169 	for (let idx = 0z; idx < 10; idx += 1) {
    170 		assert(isclose(
    171 			atan2f64(10f64, TEST_INPUTS[idx]),
    172 			TEST_ATAN2[idx]));
    173 	};
    174 	assert(isnan(atan2f64(-INF, NAN)));
    175 	assert(atan2f64(-PI, INF) == -0f64);
    176 	assert(isnan(atan2f64(-PI, NAN)));
    177 	assert(atan2f64(-0f64, 0f64) == -0f64);
    178 	assert(atan2f64(-0f64, PI) == -0f64);
    179 	assert(atan2f64(-0f64, INF) == -0f64);
    180 	assert(isnan(atan2f64(-0f64, NAN)));
    181 	assert(atan2f64(0f64, 0f64) == 0f64);
    182 	assert(atan2f64(0f64, PI) == 0f64);
    183 	assert(atan2f64(0f64, INF) == 0f64);
    184 	assert(isnan(atan2f64(0f64, NAN)));
    185 	assert(atan2f64(PI, INF) == 0f64);
    186 	assert(atan2f64(1f64, INF) == 0f64);
    187 	assert(atan2f64(-1f64, INF) == -0f64);
    188 	assert(isnan(atan2f64(PI, NAN)));
    189 	assert(isnan(atan2f64(INF, NAN)));
    190 	assert(isnan(atan2f64(NAN, NAN)));
    191 };
    192 
    193 @test fn hypot() void = {
    194 	for (let idx = 0z; idx < 10; idx += 1) {
    195 		const a = absf64(1e200f64 * TEST_TANH[idx] * SQRT_2);
    196 		assert(isclose(
    197 			hypotf64(1e200f64 * TEST_TANH[idx],
    198 				1e200f64 * TEST_TANH[idx]),
    199 			a));
    200 	};
    201 	assert(hypotf64(-INF, -INF) == INF);
    202 	assert(hypotf64(-INF, 0f64) == INF);
    203 	assert(hypotf64(-INF, INF) == INF);
    204 	assert(hypotf64(-INF, NAN) == INF);
    205 	assert(hypotf64(-0f64, -0f64) == 0f64);
    206 	assert(hypotf64(-0f64, 0f64) == 0f64);
    207 	assert(hypotf64(0f64, -0f64) == 0f64);
    208 	assert(hypotf64(0f64, 0f64) == 0f64);
    209 	assert(hypotf64(0f64, -INF) == INF);
    210 	assert(hypotf64(0f64, INF) == INF);
    211 	assert(isnan(hypotf64(0f64, NAN)));
    212 	assert(hypotf64(INF, -INF) == INF);
    213 	assert(hypotf64(INF, 0f64) == INF);
    214 	assert(hypotf64(INF, INF) == INF);
    215 	assert(hypotf64(INF, NAN) == INF);
    216 	assert(hypotf64(NAN, -INF) == INF);
    217 	assert(isnan(hypotf64(NAN, 0f64)));
    218 	assert(hypotf64(NAN, INF) == INF);
    219 	assert(isnan(hypotf64(NAN, NAN)));
    220 };