hare

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

floats.ha (10526B)


      1 @test fn floatbits() void = {
      2 	const a: [_]f64 = [INF, -INF, 0.0, 1.0, -1.0, 123456789.0,
      3 		F64_MIN, F64_MIN_NORMAL, F64_MAX_NORMAL];
      4 	for (let i = 0z; i < len(a); i += 1) {
      5 		assert(f64frombits(f64bits(a[i])) == a[i]);
      6 	};
      7 	const a: [_]f32 = [INF, -INF, 0.0, 1.0, -1.0, -123456.0,
      8 		F32_MIN, F32_MIN_NORMAL, F32_MAX_NORMAL];
      9 	for (let i = 0z; i < len(a); i += 1) {
     10 		assert(f32frombits(f32bits(a[i])) == a[i]);
     11 	};
     12 };
     13 
     14 @test fn isnan() void = {
     15 	assert(isnan(NAN));
     16 	assert(isnan(-NAN));
     17 	assert(isnan(f64frombits(0xfffabcdef1234567)));
     18 	assert(!isnan(INF));
     19 	assert(!isnan(1.23f32));
     20 };
     21 
     22 @test fn float_normality() void = {
     23 	assert(isnormal(0.0));
     24 	assert(isnormal(1.0));
     25 	assert(!isnormal(NAN));
     26 	assert(!isnormal(INF));
     27 	assert(!isnormal(1.0e-310));
     28 	assert(!isnormal(1.0e-40f32));
     29 
     30 	assert(isnormalf32(1.0));
     31 	assert(isnormalf32(0.0));
     32 	assert(!isnormalf32(NAN));
     33 	assert(!isnormalf32(INF));
     34 	assert(!isnormalf32(-1.0e-40));
     35 	assert(isnormalf32(-1.0e-50));
     36 
     37 	assert(isnormalf64(1.0));
     38 	assert(isnormalf64(0.0));
     39 	assert(!isnormalf64(NAN));
     40 	assert(!isnormalf64(INF));
     41 	assert(!isnormalf64(-1.0e-320));
     42 	assert(isnormalf64(-1.0e-330));
     43 
     44 	assert(issubnormal(1.0e-320));
     45 	assert(issubnormal(1.0e-42f32));
     46 	assert(!issubnormal(NAN));
     47 	assert(!issubnormal(INF));
     48 	assert(!issubnormal(1.0));
     49 	assert(!issubnormal(0.0));
     50 
     51 	assert(issubnormalf32(1.0e-45));
     52 	assert(issubnormalf32(-1.0e-39));
     53 	assert(!issubnormalf32(-NAN));
     54 	assert(!issubnormalf32(-INF));
     55 	assert(!issubnormalf32(0.0));
     56 	assert(!issubnormalf32(-1.0e-49));
     57 
     58 	assert(issubnormalf64(5.0e-324));
     59 	assert(issubnormalf64(-2.0e-310));
     60 	assert(!issubnormalf64(-NAN));
     61 	assert(!issubnormalf64(-INF));
     62 	assert(!issubnormalf64(-1.0e-400));
     63 	assert(!issubnormalf64(0.0));
     64 };
     65 
     66 @test fn absf() void = {
     67 	for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) {
     68 		assert(absf(TEST_INPUTS[idx]) == TEST_ABSF[idx]);
     69 	};
     70 	assert(absf64(2f64) == 2f64);
     71 	assert(absf32(2.0f32) == 2.0f32);
     72 	assert(absf(2f64) == 2f64);
     73 	assert(absf(2.0f32) == 2f64);
     74 	assert(absf(-2f64) == 2f64);
     75 	assert(absf(-2.0f32) == 2.0f32);
     76 	assert(absf(0f32) == 0f32);
     77 	assert(absf(0f64) == 0f64);
     78 };
     79 
     80 @test fn copysign() void = {
     81 	assert(copysign(100f64, 1f64) == 100f64);
     82 	assert(copysign(100f64, -1f64) == -100f64);
     83 	assert(copysign(100.0f32, 1.0f32) == 100.0f32);
     84 	assert(copysign(100.0f32, -1.0f32) == -100.0f32);
     85 	assert(copysign(100f64, 0f64) == 100f64);
     86 	assert(copysign(100f64, -0f64) == -100f64);
     87 	assert(copysign(0f64, 100f64) == 0f64);
     88 	assert(signf(copysign(0f64, 100f64)) > 0);
     89 	assert(copysign(0f64, -100f64) == 0f64);
     90 	assert(signf(copysign(0f64, -100f64)) < 0);
     91 };
     92 
     93 @test fn signf() void = {
     94 	for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) {
     95 		assert(signf(TEST_INPUTS[idx]) == TEST_SIGNF[idx]);
     96 	};
     97 	assert(signf(0f64) == 1i64);
     98 	assert(signf(-0f64) == -1i64);
     99 	assert(signf(0.0f32) == 1i64);
    100 	assert(signf(-0.0f32) == -1i64);
    101 	assert(signf(1.5f64) == 1i64);
    102 	assert(signf(-1.5f64) == -1i64);
    103 	assert(ispositive(1f64));
    104 	assert(!ispositive(-1f64));
    105 	assert(isnegative(-1f64));
    106 	assert(!isnegative(1f64));
    107 };
    108 
    109 @test fn normalize() void = {
    110 	let res = normalizef64(5.0e-320);
    111 	assert(res.0 > F64_MIN_NORMAL);
    112 	assert(res.1 < 0i64);
    113 	res = normalizef64(5.0e-300);
    114 	assert(res.0 == 5.0e-300);
    115 	assert(res.1 == 0i64);
    116 };
    117 
    118 @test fn frexp() void = {
    119 	for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) {
    120 		let res = frexp(TEST_INPUTS[idx]);
    121 		let expected = TEST_FREXP[idx];
    122 		assert(res.0 == expected.0);
    123 		assert(res.1 == expected.1);
    124 	};
    125 	let res = frexp(3f64);
    126 	assert(res.0 == 0.75f64);
    127 	assert(res.1 == 2i64);
    128 	res = frexp(2.42f64);
    129 	assert(res.0 == 0.605f64);
    130 	assert(res.1 == 2i64);
    131 	res = frexp(NAN);
    132 	assert(res.1 == 0);
    133 	res = frexp(INF);
    134 	assert(res.1 == 0);
    135 };
    136 
    137 @test fn frexp_ldexp() void = {
    138 	const tests64: [_]f64 = [INF, -INF,
    139 		0.0, 1.0, -1.0, 2.42, 123456789.0,
    140 		F64_MIN_NORMAL, F64_MAX_NORMAL,
    141 		3.0e-310f64];
    142 	for (let i = 0z; i < len(tests64); i += 1) {
    143 		const parts = frexpf64(tests64[i]);
    144 		const res64 = ldexpf64(parts.0, parts.1);
    145 		assert(res64 == tests64[i]);
    146 	};
    147 	assert(ldexpf64(1f64, -1076i64) == 0f64);
    148 	assert(ldexpf64(-1f64, -1076i64) == -0f64);
    149 	assert(signf(ldexpf64(-1f64, -1076i64)) < 0);
    150 	assert(ldexpf64(2f64, 1024i64) == INF);
    151 	assert(ldexpf64(-2f64, 1024i64) == -INF);
    152 
    153 	const tests32: [_]f32 = [INF, -INF,
    154 		0.0, 1.0, -1.0, 2.42, 123456789.0,
    155 		F32_MIN_NORMAL, F32_MAX_NORMAL,
    156 		3.0e-39f32];
    157 	for (let i = 0z; i < len(tests32); i += 1) {
    158 		const parts = frexpf32(tests32[i]);
    159 		const res = ldexpf32(parts.0, parts.1);
    160 		assert(res == tests32[i]);
    161 	};
    162 	assert(ldexpf32(1.0f32, -1076i32) == 0.0f32);
    163 	assert(ldexpf32(-1.0f32, -1076i32) == -0.0f32);
    164 	assert(signf(ldexpf32(-1.0f32, -1076i32)) < 0);
    165 	assert(ldexpf32(2.0f32, 1024i32) == INF);
    166 	assert(ldexpf32(-2.0f32, 1024i32) == -INF);
    167 };
    168 
    169 @test fn modfrac() void = {
    170 	// 64
    171 	for (let idx = 0z; idx < len(TEST_INPUTS); idx += 1) {
    172 		let res = modfracf64(TEST_INPUTS[idx]);
    173 		assert(res.0 == TEST_MODFRAC[idx].0);
    174 		assert(isclose(res.1, TEST_MODFRAC[idx].1));
    175 	};
    176 	let res = modfracf64(1.75f64);
    177 	assert(res.0 == 1f64);
    178 	assert(res.1 == 0.75f64);
    179 	res = modfracf64(0.75f64);
    180 	assert(res.0 == 0f64);
    181 	assert(res.1 == 0.75f64);
    182 	res = modfracf64(-0.75f64);
    183 	assert(res.0 == -0f64);
    184 	assert(res.1 == -0.75f64);
    185 	res = modfracf64(0f64);
    186 	assert(res.0 == 0f64);
    187 	assert(res.1 == 0f64);
    188 	assert(signf(res.1) > 0);
    189 	res = modfracf64(-0f64);
    190 	assert(res.0 == -0f64);
    191 	assert(res.1 == -0f64);
    192 	assert(signf(res.1) < 0);
    193 	res = modfracf64(23.50f64);
    194 	assert(res.0 == 23f64);
    195 	assert(res.1 == 0.50f64);
    196 	res = modfracf64(F64_MAX_NORMAL);
    197 	assert(res.0 == F64_MAX_NORMAL);
    198 	assert(res.1 == 0f64);
    199 
    200 	// 32
    201 	let res = modfracf32(1.75f32);
    202 	assert(res.0 == 1f32);
    203 	assert(res.1 == 0.75f32);
    204 	res = modfracf32(0.75f32);
    205 	assert(res.0 == 0f32);
    206 	assert(res.1 == 0.75f32);
    207 	res = modfracf32(-0.75f32);
    208 	assert(res.0 == -0f32);
    209 	assert(res.1 == -0.75f32);
    210 	res = modfracf32(0.0f32);
    211 	assert(res.0 == 0f32);
    212 	assert(res.1 == 0.0f32);
    213 	assert(signf(res.1) > 0);
    214 	res = modfracf32(-0.0f32);
    215 	assert(res.0 == -0f32);
    216 	assert(res.1 == -0.0f32);
    217 	assert(signf(res.1) < 0);
    218 	res = modfracf32(23.50f32);
    219 	assert(res.0 == 23f32);
    220 	assert(res.1 == 0.50f32);
    221 	res = modfracf32(F32_MAX_NORMAL);
    222 	assert(res.0 == F32_MAX_NORMAL);
    223 	assert(res.1 == 0f32);
    224 };
    225 
    226 @test fn nextafter() void = {
    227 	let f = &f64frombits;
    228 	// from musl's testsuite
    229 	assert(nextafterf64(f(0xc0202239f3c6a8f1), f(0x40122484b9ef31f0)) == f(0xc0202239f3c6a8f0));
    230 	assert(nextafterf64(f(0x401161868e18bc67), f(0xc021c6a6cdce75e8)) == f(0x401161868e18bc66));
    231 	assert(nextafterf64(f(0xc020c34b3e01e6e7), f(0xc0061bde29e83f6d)) == f(0xc020c34b3e01e6e6));
    232 	assert(nextafterf64(f(0xc01a206f0a19dcc4), f(0x40124527f7b576ac)) == f(0xc01a206f0a19dcc3));
    233 	assert(nextafterf64(f(0x402288bbb0d6a1e6), f(0x40133edd910a3c01)) == f(0x402288bbb0d6a1e5));
    234 	assert(nextafterf64(f(0xc019ccd8be03f495), f(0x3fe52fb12ef638a1)) == f(0xc019ccd8be03f494));
    235 	assert(nextafterf64(f(0x401f6f80ed2eab44), f(0x3faab3ff8575b21d)) == f(0x401f6f80ed2eab43));
    236 	assert(nextafterf64(f(0xbfe95882b433fad3), f(0x401eb4a2e7ce0693)) == f(0xbfe95882b433fad2));
    237 	assert(nextafterf64(f(0x3fe3b3d617ae3c4a), f(0x40001860611d75e1)) == f(0x3fe3b3d617ae3c4b));
    238 	assert(nextafterf64(f(0xbfe1e159e36313ee), f(0x3fa081bd34224213)) == f(0xbfe1e159e36313ed));
    239 
    240 	assert(nextafterf64(f(0xbfe1e159e36313ee), f(0xbfe1e159e36313ee)) == f(0xbfe1e159e36313ee));
    241 	assert(nextafterf64(0.0f64, 1.0f64) == f(0x1));
    242 	assert(nextafterf64(0.0f64, -1.0f64) == f(0x8000000000000001));
    243 	assert(nextafterf64(-0.0f64, 1.0f64) == f(0x1));
    244 	assert(nextafterf64(-0.0f64, -1.0f64) == f(0x8000000000000001));
    245 	assert(nextafterf64(0.0f64, 0.0f64) == 0.0f64);
    246 	assert(nextafterf64(-0.0f64, 0.0f64) == 0.0f64);
    247 	assert(nextafterf64(0.0f64, -0.0f64) == -0.0f64);
    248 	assert(isnan(nextafterf64(1.0f64, NAN)));
    249 	assert(isnan(nextafterf64(NAN, -2f64)));
    250 	assert(isnan(nextafterf64(NAN, NAN)));
    251 
    252 	let f = &f32frombits;
    253 	assert(nextafterf32(f(0xc10111d0), f(0x40912426)) == f(0xc10111cf));
    254 	assert(nextafterf32(f(0x408b0c34), f(0xc10e3536)) == f(0x408b0c33));
    255 	assert(nextafterf32(f(0xc1061a5a), f(0xc030def1)) == f(0xc1061a59));
    256 	assert(nextafterf32(f(0xc0d10378), f(0x40922940)) == f(0xc0d10377));
    257 	assert(nextafterf32(f(0x411445de), f(0x4099f6ed)) == f(0x411445dd));
    258 	assert(nextafterf32(f(0xc0ce66c6), f(0x3f297d89)) == f(0xc0ce66c5));
    259 	assert(nextafterf32(f(0x40fb7c07), f(0x3d559ffc)) == f(0x40fb7c06));
    260 	assert(nextafterf32(f(0xbf4ac416), f(0x40f5a517)) == f(0xbf4ac415));
    261 	assert(nextafterf32(f(0x3f1d9eb1), f(0x4000c303)) == f(0x3f1d9eb2));
    262 	assert(nextafterf32(f(0xbf0f0acf), f(0x3d040dea)) == f(0xbf0f0ace));
    263 
    264 	assert(nextafterf32(f(0xbf0f0acf), f(0xbf0f0acf)) == f(0xbf0f0acf));
    265 	assert(nextafterf32(0.0f32, 1.0f32) == f(0x1));
    266 	assert(nextafterf32(0.0f32, -1.0f32) == f(0x80000001));
    267 	assert(nextafterf32(-0.0f32, 1.0f32) == f(0x1));
    268 	assert(nextafterf32(-0.0f32, -1.0f32) == f(0x80000001));
    269 	assert(nextafterf32(0.0f32, 0.0f32) == 0.0f32);
    270 	assert(nextafterf32(-0.0f32, 0.0f32) == 0.0f32);
    271 	assert(nextafterf32(0.0f32, -0.0f32) == -0.0f32);
    272 	assert(isnan(nextafterf32(1.0f32, NAN)));
    273 	assert(isnan(nextafterf32(NAN, -2f32)));
    274 	assert(isnan(nextafterf32(NAN, NAN)));
    275 };
    276 
    277 @test fn nearbyint() void = {
    278 	// from musl's testsuite
    279 	let f = &f64frombits;
    280 	assert(nearbyintf64(f(0xc0202239f3c6a8f1)) == f(0xc020000000000000));
    281 	assert(nearbyintf64(f(0x401161868e18bc67)) == f(0x4010000000000000));
    282 	assert(nearbyintf64(f(0xc020c34b3e01e6e7)) == f(0xc020000000000000));
    283 	assert(nearbyintf64(f(0xc01a206f0a19dcc4)) == f(0xc01c000000000000));
    284 	assert(nearbyintf64(f(0x402288bbb0d6a1e6)) == f(0x4022000000000000));
    285 	assert(nearbyintf64(f(0x3fe52efd0cd80497)) == f(0x3ff0000000000000));
    286 	assert(nearbyintf64(f(0xbfda05cc754481d1)) == f(0x8000000000000000));
    287 	assert(nearbyintf64(f(0x3fe1f9ef934745cb)) == f(0x3ff0000000000000));
    288 	assert(nearbyintf64(f(0x3fe8c5db097f7442)) == f(0x3ff0000000000000));
    289 	assert(nearbyintf64(f(0xbfe5b86ea8118a0e)) == f(0xbff0000000000000));
    290 
    291 	let f = &f32frombits;
    292 	assert(nearbyintf32(f(0xc10111d0)) == f(0xc1000000));
    293 	assert(nearbyintf32(f(0x408b0c34)) == f(0x40800000));
    294 	assert(nearbyintf32(f(0xc1061a5a)) == f(0xc1000000));
    295 	assert(nearbyintf32(f(0xc0d10378)) == f(0xc0e00000));
    296 	assert(nearbyintf32(f(0x411445de)) == f(0x41100000));
    297 	assert(nearbyintf32(f(0x3f2977e8)) == f(0x3f800000));
    298 	assert(nearbyintf32(f(0xbed02e64)) == f(0x80000000));
    299 	assert(nearbyintf32(f(0x3f0fcf7d)) == f(0x3f800000));
    300 	assert(nearbyintf32(f(0x3f462ed8)) == f(0x3f800000));
    301 	assert(nearbyintf32(f(0xbf2dc375)) == f(0xbf800000));
    302 };