hare

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

ints.ha (2637B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use types;
      5 
      6 // Returns the absolute value of signed integer n.
      7 export fn absi8(n: i8) u8 = if (n < 0) -n: u8 else n: u8;
      8 
      9 // Returns the absolute value of signed integer n.
     10 export fn absi16(n: i16) u16 = if (n < 0) -n: u16 else n: u16;
     11 
     12 // Returns the absolute value of signed integer n.
     13 export fn absi32(n: i32) u32 = if (n < 0) -n: u32 else n: u32;
     14 
     15 // Returns the absolute value of signed integer n.
     16 export fn absi64(n: i64) u64 = if (n < 0) -n: u64 else n: u64;
     17 
     18 // Returns the absolute value of signed integer n.
     19 export fn absi(n: int) uint = if (n < 0) -n: uint else n: uint;
     20 
     21 @test fn absi() void = {
     22 	assert(absi8(2) == 2);
     23 	assert(absi8(-2) == 2);
     24 	assert(absi8(types::I8_MIN) == types::I8_MIN: u8);
     25 	assert(absi16(2) == 2);
     26 	assert(absi16(-2) == 2);
     27 	assert(absi16(types::I16_MIN) == types::I16_MIN: u16);
     28 	assert(absi32(2) == 2);
     29 	assert(absi32(-2) == 2);
     30 	assert(absi32(types::I32_MIN) == types::I32_MIN: u32);
     31 	assert(absi64(2) == 2);
     32 	assert(absi64(-2) == 2);
     33 	assert(absi64(types::I64_MIN) == types::I64_MIN: u64);
     34 	assert(absi(2) == 2);
     35 	assert(absi(-2) == 2);
     36 	assert(absi(types::INT_MIN) == types::INT_MIN: uint);
     37 };
     38 
     39 // Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
     40 export fn signi8(n: i8) i8 = {
     41 	if (n > 0i8) {
     42 		return 1i8;
     43 	};
     44 	if (n < 0i8) {
     45 		return -1i8;
     46 	};
     47 	return 0i8;
     48 };
     49 
     50 // Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
     51 export fn signi16(n: i16) i16 = {
     52 	if (n > 0i16) {
     53 		return 1i16;
     54 	};
     55 	if (n < 0i16) {
     56 		return -1i16;
     57 	};
     58 	return 0i16;
     59 };
     60 
     61 // Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
     62 export fn signi32(n: i32) i32 = {
     63 	if (n > 0i32) {
     64 		return 1i32;
     65 	};
     66 	if (n < 0i32) {
     67 		return -1i32;
     68 	};
     69 	return 0i32;
     70 };
     71 
     72 // Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
     73 export fn signi64(n: i64) i64 = {
     74 	if (n > 0i64) {
     75 		return 1i64;
     76 	};
     77 	if (n < 0i64) {
     78 		return -1i64;
     79 	};
     80 	return 0i64;
     81 };
     82 
     83 // Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
     84 export fn signi(n: int) i64 = {
     85 	if (n > 0) {
     86 		return 1;
     87 	};
     88 	if (n < 0) {
     89 		return -1;
     90 	};
     91 	return 0;
     92 };
     93 
     94 @test fn signi() void = {
     95 	assert(signi8(2i8) == 1i8);
     96 	assert(signi8(-2i8) == -1i8);
     97 	assert(signi8(0i8) == 0i8);
     98 	assert(signi16(2i16) == 1i16);
     99 	assert(signi16(-2i16) == -1i16);
    100 	assert(signi16(0i16) == 0i16);
    101 	assert(signi32(2i32) == 1i32);
    102 	assert(signi32(-2i32) == -1i32);
    103 	assert(signi32(0i32) == 0i32);
    104 	assert(signi64(2i64) == 1i64);
    105 	assert(signi64(-2i64) == -1i64);
    106 	assert(signi64(0i64) == 0i64);
    107 	assert(signi(2) == 1);
    108 	assert(signi(-2) == -1);
    109 	assert(signi(0) == 0);
    110 };