math.ha (9080B)
1 @test fn eqwithin() void = { 2 assert(eqwithin(1f64, 2f64, 2f64)); 3 assert(eqwithin(1.0f32, 2.0f32, 2.0f32)); 4 assert(!eqwithin(1.0005f32, 1.0004f32, 0.00001f32)); 5 assert(isclose(1f64, 1.0000000000000000000000000001f64)); 6 assert(isclose(1.0f32, 1.0000000000000000000000000001f32)); 7 assert(!isclose(1.0005f32, 1.0004f32)); 8 }; 9 10 @test fn logf64() void = { 11 for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) { 12 assert(isclose( 13 logf64(absf64(TEST_INPUTS[idx])), 14 TEST_LOG[idx])); 15 }; 16 assert(logf64(E) == 1f64); 17 assert(logf64(54.598150033144239078110261202860878402790f64) == 4f64); 18 assert(isnan(logf64(-1f64))); 19 assert(logf64(INF) == INF); 20 assert(logf64(0f64) == -INF); 21 assert(isnan(logf64(NAN))); 22 }; 23 24 @test fn log10f64() void = { 25 for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) { 26 assert(isclose( 27 log10f64(absf64(TEST_INPUTS[idx])), 28 TEST_LOG10[idx])); 29 }; 30 }; 31 32 @test fn log2f64() void = { 33 for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) { 34 assert(isclose( 35 log2f64(absf64(TEST_INPUTS[idx])), 36 TEST_LOG2[idx])); 37 }; 38 }; 39 40 @test fn log1p() void = { 41 for (let idx = 0z; idx < 10; idx += 1) { 42 assert(isclose( 43 log1pf64(TEST_INPUTS[idx] / 100f64), 44 TEST_LOG1P[idx])); 45 }; 46 assert(isnan(log1pf64(-INF))); 47 assert(isnan(log1pf64(-PI))); 48 assert(log1pf64(-1f64) == -INF); 49 assert(log1pf64(-0f64) == -0f64); 50 assert(log1pf64(0f64) == 0f64); 51 assert(log1pf64(INF) == INF); 52 assert(isnan(log1pf64(NAN))); 53 }; 54 55 @test fn expf64() void = { 56 for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) { 57 assert(isclose(expf64(TEST_INPUTS[idx]), TEST_EXP[idx])); 58 }; 59 assert(expf64(1f64) == E); 60 assert(isnan(expf64(NAN))); 61 assert(isinf(expf64(INF))); 62 assert(expf64(-INF) == 0f64); 63 assert(isinf(expf64(99999f64))); 64 assert(expf64(-99999f64) == 0f64); 65 assert(expf64(0.5e-20) == 1f64); 66 }; 67 68 @test fn exp2f64() void = { 69 for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) { 70 assert(isclose(exp2f64(TEST_INPUTS[idx]), TEST_EXP2[idx])); 71 }; 72 assert(exp2f64(0f64) == 1f64); 73 assert(exp2f64(3f64) == 8f64); 74 assert(exp2f64(-2f64) == 0.25f64); 75 assert(!isinf(exp2f64(256f64))); 76 assert(isinf(exp2f64(99999f64))); 77 assert(exp2f64(-99999f64) == 0f64); 78 assert(isnan(exp2f64(NAN))); 79 assert(isinf(exp2f64(INF))); 80 assert(exp2f64(-INF) == 0f64); 81 }; 82 83 @test fn sqrt() void = { 84 for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) { 85 assert(isclose( 86 sqrtf64(absf64(TEST_INPUTS[idx])), 87 TEST_SQRT[idx])); 88 }; 89 assert(sqrtf64(2f64) == SQRT_2); 90 assert(sqrtf64(4f64) == 2f64); 91 assert(sqrtf64(16f64) == 4f64); 92 assert(sqrtf64(65536f64) == 256f64); 93 assert(sqrtf64(powf64(123f64, 2f64)) == 123f64); 94 assert(sqrtf64(0f64) == 0f64); 95 assert(isnan(sqrtf64(NAN))); 96 assert(sqrtf64(INF) == INF); 97 assert(isnan(sqrtf64(-2f64))); 98 }; 99 100 @test fn powf64() void = { 101 for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) { 102 assert(eqwithin( 103 powf64(10f64, TEST_INPUTS[idx]), 104 TEST_POW[idx], 105 1e-8f64)); 106 }; 107 // Positive integer 108 assert(powf64(2f64, 2f64) == 4f64); 109 assert(powf64(3f64, 3f64) == 27f64); 110 // Very large positive integer 111 assert(!isinf(powf64(2f64, 1020f64))); 112 assert(isinf(powf64(2f64, 1050f64))); 113 // Very very large positive integer 114 assert(isinf(powf64(2f64, F64_MAX_NORMAL))); 115 assert(powf64(0.5f64, F64_MAX_NORMAL) == 0f64); 116 // Negative integer 117 assert(powf64(2f64, -1f64) == 0.5f64); 118 assert(powf64(2f64, -2f64) == 0.25f64); 119 // Very small negative integer 120 assert(powf64(2f64, -1020f64) > 0f64); 121 assert(powf64(2f64, -1080f64) == 0f64); 122 // Very very small negative integer 123 assert(powf64(2f64, -F64_MAX_NORMAL) == 0f64); 124 assert(isinf(powf64(0.5f64, -F64_MAX_NORMAL))); 125 // Positive fractional powers 126 assert(eqwithin(powf64(2f64, 1.5f64), 127 2.8284271247461900976033774f64, 128 1e-10f64)); 129 assert(eqwithin(powf64(2f64, 5.5f64), 130 45.254833995939041561654039f64, 131 1e-10f64)); 132 // Negative fractional powers 133 assert(eqwithin(powf64(2f64, -1.5f64), 134 0.3535533905932737622004221f64, 135 1e-10f64)); 136 assert(eqwithin(powf64(2f64, -5.5f64), 137 0.0220970869120796101375263f64, 138 1e-10f64)); 139 140 // Special cases 141 // pow(x, ±0) = 1 for any x 142 assert(powf64(123f64, 0f64) == 1f64); 143 // pow(1, y) = 1 for any y 144 assert(powf64(1f64, 123f64) == 1f64); 145 // pow(x, 1) = x for any x 146 assert(powf64(123f64, 1f64) == 123f64); 147 // pow(NaN, y) = NaN 148 assert(isnan(powf64(NAN, 123f64))); 149 // pow(x, NaN) = NaN 150 assert(isnan(powf64(123f64, NAN))); 151 // pow(±0, y) = ±Inf for y an odd integer < 0 152 assert(powf64(0f64, -3f64) == INF); 153 assert(powf64(-0f64, -3f64) == -INF); 154 // pow(±0, -Inf) = +Inf 155 assert(powf64(0f64, -INF) == INF); 156 assert(powf64(-0f64, -INF) == INF); 157 // pow(±0, +Inf) = +0 158 assert(powf64(0f64, INF) == 0f64); 159 assert(powf64(-0f64, INF) == 0f64); 160 // pow(±0, y) = +Inf for finite y < 0 and not an odd integer 161 assert(powf64(0f64, -2f64) == INF); 162 assert(powf64(0f64, -2f64) == INF); 163 //pow(±0, y) = ±0 for y an odd integer > 0 164 assert(powf64(0f64, 123f64) == 0f64); 165 const neg_zero = powf64(-0f64, 123f64); 166 assert(neg_zero == 0f64); 167 assert(isnegative(neg_zero)); 168 // pow(±0, y) = +0 for finite y > 0 and not an odd integer 169 assert(powf64(0f64, 8f64) == 0f64); 170 // pow(-1, ±Inf) = 1 171 assert(powf64(-1f64, INF) == 1f64); 172 assert(powf64(-1f64, -INF) == 1f64); 173 // pow(x, +Inf) = +Inf for |x| > 1 174 assert(powf64(123f64, INF) == INF); 175 // pow(x, -Inf) = +0 for |x| > 1 176 assert(powf64(123f64, -INF) == 0f64); 177 // pow(x, +Inf) = +0 for |x| < 1 178 assert(powf64(0.5f64, INF) == 0f64); 179 assert(powf64(-0.5f64, INF) == 0f64); 180 // pow(x, -Inf) = +Inf for |x| < 1 181 assert(powf64(0.5f64, -INF) == INF); 182 assert(powf64(-0.5f64, -INF) == INF); 183 // pow(+Inf, y) = +Inf for y > 0 184 assert(powf64(INF, 123f64) == INF); 185 // pow(+Inf, y) = +0 for y < 0 186 assert(powf64(INF, -1f64) == 0f64); 187 // pow(-Inf, y) = pow(-0, -y) 188 assert(powf64(-INF, 123f64) == powf64(-0f64, -123f64)); 189 // pow(x, y) = NaN for finite x < 0 and finite non-integer y 190 assert(isnan(powf64(-2f64, 1.23f64))); 191 // sqrt 192 assert(powf64(4f64, 0.5f64) == sqrtf64(4f64)); 193 assert(powf64(4f64, 0.5f64) == 2f64); 194 assert(powf64(4f64, -0.5f64) == (1f64 / sqrtf64(4f64))); 195 assert(powf64(4f64, -0.5f64) == (1f64 / 2f64)); 196 }; 197 198 @test fn floor() void = { 199 for (let idx = 0z; idx < 10; idx += 1) { 200 assert(isclose( 201 floorf64(TEST_INPUTS[idx]), 202 TEST_FLOOR[idx])); 203 }; 204 assert(floorf64(-INF) == -INF); 205 assert(floorf64(-0f64) == -0f64); 206 assert(floorf64(0f64) == 0f64); 207 assert(floorf64(INF) == INF); 208 assert(isnan(floorf64(NAN))); 209 }; 210 211 @test fn ceil() void = { 212 for (let idx = 0z; idx < 10; idx += 1) { 213 assert(isclose( 214 ceilf64(TEST_INPUTS[idx]), 215 TEST_CEIL[idx])); 216 }; 217 assert(ceilf64(-INF) == -INF); 218 assert(ceilf64(-F64_MAX_NORMAL) == -F64_MAX_NORMAL); 219 assert(ceilf64(-0f64) == -0f64); 220 assert(ceilf64(0f64) == 0f64); 221 assert(ceilf64(F64_MAX_NORMAL) == F64_MAX_NORMAL); 222 assert(ceilf64(INF) == INF); 223 assert(isnan(ceilf64(NAN))); 224 }; 225 226 @test fn trunc() void = { 227 for (let idx = 0z; idx < 10; idx += 1) { 228 assert(isclose( 229 truncf64(TEST_INPUTS[idx]), 230 TEST_TRUNC[idx])); 231 }; 232 assert(truncf64(-INF) == -INF); 233 assert(truncf64(-F64_MAX_NORMAL) == -F64_MAX_NORMAL); 234 assert(truncf64(-0f64) == -0f64); 235 assert(truncf64(0f64) == 0f64); 236 assert(truncf64(F64_MAX_NORMAL) == F64_MAX_NORMAL); 237 assert(truncf64(INF) == INF); 238 assert(isnan(truncf64(NAN))); 239 }; 240 241 @test fn round() void = { 242 for (let idx = 0z; idx < 10; idx += 1) { 243 assert(isclose( 244 roundf64(TEST_INPUTS[idx]), 245 TEST_ROUND[idx])); 246 }; 247 assert(roundf64(-INF) == -INF); 248 assert(roundf64(-F64_MAX_NORMAL) == -F64_MAX_NORMAL); 249 assert(roundf64(-0f64) == -0f64); 250 assert(roundf64(0f64) == 0f64); 251 assert(roundf64(F64_MAX_NORMAL) == F64_MAX_NORMAL); 252 assert(roundf64(INF) == INF); 253 assert(isnan(roundf64(NAN))); 254 }; 255 256 @test fn modf64() void = { 257 for (let idx = 0z; idx < 10; idx += 1) { 258 assert(isclose( 259 modf64(10f64, TEST_INPUTS[idx]), 260 TEST_MODF[idx])); 261 }; 262 263 assert(isnan(modf64(-INF, -INF))); 264 assert(isnan(modf64(-INF, -PI))); 265 assert(isnan(modf64(-INF, 0f64))); 266 assert(isnan(modf64(-INF, PI))); 267 assert(isnan(modf64(-INF, INF))); 268 assert(isnan(modf64(-INF, NAN))); 269 assert(modf64(-PI, -INF) == -PI); 270 assert(isnan(modf64(-PI, 0f64))); 271 assert(modf64(-PI, INF) == -PI); 272 assert(isnan(modf64(-PI, NAN))); 273 assert(modf64(-0f64, -INF) == -0f64); 274 assert(isnan(modf64(-0f64, 0f64))); 275 assert(modf64(-0f64, INF) == -0f64); 276 assert(isnan(modf64(-0f64, NAN))); 277 assert(modf64(0f64, -INF) == 0f64); 278 assert(isnan(modf64(0f64, 0f64))); 279 assert(modf64(0f64, INF) == 0f64); 280 assert(isnan(modf64(0f64, NAN))); 281 assert(modf64(PI, -INF) == PI); 282 assert(isnan(modf64(PI, 0f64))); 283 assert(modf64(PI, INF) == PI); 284 assert(isnan(modf64(PI, NAN))); 285 assert(isnan(modf64(INF, -INF))); 286 assert(isnan(modf64(INF, -PI))); 287 assert(isnan(modf64(INF, 0f64))); 288 assert(isnan(modf64(INF, PI))); 289 assert(isnan(modf64(INF, INF))); 290 assert(isnan(modf64(INF, NAN))); 291 assert(isnan(modf64(NAN, -INF))); 292 assert(isnan(modf64(NAN, -PI))); 293 assert(isnan(modf64(NAN, 0f64))); 294 assert(isnan(modf64(NAN, PI))); 295 assert(isnan(modf64(NAN, INF))); 296 assert(isnan(modf64(NAN, NAN))); 297 assert(modf64(5.9790119248836734e+200, 1.1258465975523544) == 298 0.6447968302508578); 299 };