hare

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

floats_test.ha (10606B)


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