Aug 13, 2022Kuina-chan


This is the reverse lookup dictionary 1 for exe execution environment of Kuin Programming Language, and is about how to handle numbers.

Handling Integers And Decimals

Performs Bitwise Operations

Handling Character

Handling Logical Value

Index

1Handling Integers And Decimals

1.1Handling Integers And Decimals



In Kuin, you use the int type for integers and the float type for decimals. They can perform basic arithmetic operations such as the four arithmetic operations (Figure 1-1).
  1. func main()
  2.   var a: int :: 7
  3.   var b: int :: 3
  4.   var c: int
  5.   do c :: a + b {c = 10}
  6.   do c :: a - b {c = 4}
  7.   do c :: a * b {c = 21}
  8.   do c :: a / b {c = 2}
  9.   do c :: a % b {c = 1}
  10.   do c :: a ^ b {c = 343}
  11.  
  12.   var x: float :: 7.0
  13.   var y: float :: 3.0
  14.   var z: float
  15.   do z :: x + y {z = 10.0}
  16.   do z :: x - y {z = 4.0}
  17.   do z :: x * y {z = 21.0}
  18.   do z :: x / y {z = 2.33333...}
  19.   do z :: x % y {z = 1.0}
  20.   do z :: x ^ y {z = 343.0}
  21. end func
Figure 1-1: int And float Types
Use the symbols "+" for addition, "-" for subtraction, "*" for multiplication, "/" for division, "%" for remainder of division, and "^" for power. You can write Kuin expressions in the same way as mathematical expressions, with multiplication being done before addition and parentheses changing the order of calculations.
The "/" operation on the int type will be truncated to the decimal point after the calculation.
When you write a number directly on the source code, if you write something like "5" or "-3", the number will be of type int, and if you write something with "." like "5.0" or "-3.0", the number will be of type float.

1.2Comparing Values



To compare the size of a value to a number of type int or float, use the comparison operators (Figure 1-2).
  1. func main()
  2.   var n: int :: 3
  3.   var m: int :: 5
  4.   var b: bool
  5.   do b :: n < m {true}
  6.   do b :: n <= m {true}
  7.   do b :: n > m {false}
  8.   do b :: n >= m {false}
  9.   do b :: n = m {false}
  10.   do b :: n <> m {true}
  11. end func
Figure 1-2: Comparison Operator
Each comparison operator is roughly equivalent to a mathematical symbol. The "<=" corresponds to "", ">=" corresponds to "", and "<>" corresponds to "".
However, the float type is subject to calculation errors, so even if the comparison matches mathematically, comparing with "=" may return false. Therefore, the lib@same function can be used to make comparisons with some allowance for calculation errors (Figure 1-3).
  1. func main()
  2.   var x: float :: lib@cos(lib@pi) {There is a slight calculation error compared to -1.0.}
  3.   var y: float :: -1.0
  4.   var b: bool
  5.   do b :: x = y {false}
  6.   do b :: lib@same(x, y) {true}
  7. end func
Figure 1-3: lib@same Function

1.3Convert Type



To convert between int and float types, use the cast operator "$" (Figure 1-4).
  1. func main()
  2.   var n: int :: -2
  3.  
  4.   var x: float
  5.   do x :: n $ float {x = -2.0}
  6.   var m: int
  7.   do m :: x $ int {m = -2}
  8. end func
Figure 1-4: Conversion Between int And float

1.4Obtain Absolute Value And Sign



For numbers of type int or float, use the .abs method for absolute values to make a negative number positive, and use the .sign method to get 1 if the sign is positive, -1 if negative, or 0 if zero (Figure 1-5).
  1. func main()
  2.   var a: int :: -3
  3.   var b: int :: 5
  4.   var c: int :: 0
  5.   var d: int
  6.   do d :: a.abs() {3}
  7.   do d :: b.abs() {5}
  8.   do d :: c.abs() {0}
  9.   do d :: a.sign() {-1}
  10.   do d :: b.sign() {1}
  11.   do d :: c.sign() {0}
  12. end func
Figure 1-5: Absolute Value And Sign

1.5Set Value To Within Upper And Lower Limit



For numbers of type int or float, use the lib@min, lib@max, and lib@clamp functions to make the value within the upper and lower limits.
lib@min is a function that sets a number greater than x to x, lib@max is a function that sets a number less than x to x, and lib@clamp is a function that does both (Figure 1-6).
  1. func main()
  2.   var a: int :: 2
  3.   var b: int :: 5
  4.   var c: int :: 9
  5.   var d: int
  6.   do d :: lib@max(a, 4) {4}
  7.   do d :: lib@max(b, 4) {5}
  8.   do d :: lib@max(c, 4) {9}
  9.   do d :: lib@min(a, 7) {2}
  10.   do d :: lib@min(b, 7) {5}
  11.   do d :: lib@min(c, 7) {7}
  12.   do d :: lib@clamp(a, 4, 7) {4}
  13.   do d :: lib@clamp(b, 4, 7) {5}
  14.   do d :: lib@clamp(c, 4, 7) {7}
  15. end func
Figure 1-6: Value Clamping

1.6Use Elementary Functions Such As Trigonometric Functions



To use elementary functions such as trigonometric functions, use the functions in lib@ (Figure 1-7).
  1. func main()
  2.   var x: float
  3.   do x :: lib@sin(lib@pi / 2.0) {sin(π / 2.0) = 1.0}
  4.   do x :: lib@ln(lib@e) {log_e(e) = 1.0}
  5.   do x :: lib@log(2.0, 8.0) {log_2.0(8.0) = 3.0}
  6.   do x :: lib@sqrt(9.0) {√(9.0) = 3.0}
  7. end func
Figure 1-7: Elementary Function

1.7Generate Random Number



To generate random numbers, use the lib@rnd and lib@rndFloat functions (Figure 1-8).
  1. func main()
  2.   var n: int :: lib@rnd(3, 5) {A random number between 3 and 5.}
  3.   var x: float :: lib@rndFloat(3.0, 5.0) {A random decimal number between 3.0 and 5.0.}
  4. end func
Figure 1-8: Generating Random Numbers
lib@rnd and lib@rndFloat will return a different random number each time the program is invoked. If you want to reproduce the same random number sequence, use the lib@Rnd class (Figure 1-9).
  1. func main()
  2.   var rnd: lib@Rnd :: lib@makeRnd(1234b32) {The argument is the seed of the random number.}
  3.   var a: int :: rnd.rnd(1, 10) {a = 2}
  4.   var b: int :: rnd.rnd(1, 10) {b = 5}
  5.   var c: int :: rnd.rnd(1, 10) {c = 4}
  6.   var d: int :: rnd.rnd(1, 10) {d = 7}
  7.  
  8.   do rnd :: lib@makeRnd(1234b32)
  9.   do a :: rnd.rnd(1, 10) {a = 2}
  10.   do b :: rnd.rnd(1, 10) {b = 5}
  11.   do c :: rnd.rnd(1, 10) {c = 4}
  12.   do d :: rnd.rnd(1, 10) {d = 7}
  13. end func
Figure 1-9: Generating Reproducible Random Numbers

1.8Perform Mathematical Calculations Such As Factorial And Prime Factorization



To perform mathematical calculations such as factorial and prime factorization, use the functions in math@ (Figure 1-10).
  1. func main()
  2.   var x: float :: math@fact(6.0) {6.0! = 720.0}
  3.   var n: int :: math@lcm(12, 16) {The least common multiple of 12 and 16 = 48}
  4.   var m: int :: math@gcd(12, 16) {The greatest common denominator of 12 and 16 = 4}
  5.   var b: bool :: math@prime(2147483647) {Whether 2147483647 is a prime number = true}
  6.   var ps: []int :: math@primeFactors(44444) {The prime factorization of 44444 = [2, 2, 41, 271]}
  7. end func
Figure 1-10: Mathematical Calculation

2Performs Bitwise Operations

To perform operations in bit units, use the bit8, bit16, bit32, and bit64 types (Figure 2-1).
  1. func main()
  2.   var a: bit8 :: 0x12b8 {a = 0x12}
  3.   var b: bit16 :: 0x1234b16 {b = 0x1234}
  4.   var c: bit32 :: 0x12345678b32 {c = 0x12345678}
  5.   var d: bit64 :: 0x123456789ABCDEF0b64 {d = 0x123456789ABCDEF0}
  6. end func
Figure 2-1: Bit Type
The bit8, bit16, bit32, and bit64 types can handle sizes of 8, 16, 32, and 64 bits, respectively, and cannot handle negative values.
When writing bit8, bit16, bit32, and bit64 numbers directly in the source code, they are represented by adding the suffixes "b8", "b16", "b32", and "b64" after the number notation in the int type, respectively.
These types can perform bitwise operations such as and, or, not, and xor (Figure 2-2).
  1. func main()
  2.   var a: bit16 :: 0xF333b16 {a = 0xF333}
  3.   var b: bit16 :: 0x1234b16 {b = 0x1234}
  4.   var c: bit16
  5.  
  6.   do c :: a.and(b) {c = 0x1230}
  7.   do c :: a.or(b) {c = 0xF337}
  8.   do c :: a.not() {c = 0x0CCC}
  9.   do c :: a.xor(b) {c = 0xE107}
  10.   do c :: a.shl(8) {c = 0x3300 (8-bit logical shift of a to the left.)}
  11.   do c :: a.shr(8) {c = 0x00F3 (8-bit logical shift of a to the right.)}
  12.   do c :: a.sar(8) {c = 0xFFF3 (8-bit arithmetic shift a to the right.)}
  13.   do c :: a.endian() {c = 0x33F3 (Endian conversion.)}
  14. end func
Figure 2-2: Bit Operation

3Handling Character

3.1Handling Character



To handle characters in Kuin, the char type is used. When writing characters directly in the source code, enclose them in "'" to represent them.(Figure 3-1)。
  1. func main()
  2.   var c1: char :: 'A'
  3.   var n: int :: c1 $ int {n = 65 (0x41)}
  4.   var c2: char :: n $ char {c2 = 'A'}
  5. end func
Figure 3-1: Character Type
The char type is internally an integer between 0x0000 and 0xFFFF, and characters are stored in UTF-16 character code. By using the "$" operator to convert to the int type, you can operate directly in character code.

3.2Writing Special Characters Such As Newline Characters



To represent special characters such as line feeds in the source code, use a combination of the "\" character and a single character. For example, a newline character is written as "'\n'" (Figure 3-2).
  1. func main()
  2.   var c: char
  3.   do c :: '\n' {Newline character.}
  4.   do c :: '\t' {Tab character.}
  5.   do c :: '\"' {Double quotation character.}
  6.   do c :: '\'' {Single quotation character.}
  7.   do c :: '\\' {\ character.}
  8.   do c :: '\u0041' {Character with the character code 0x0041 ('A').}
  9. end func
Figure 3-2: Escape Sequence

4Handling Logical Value

4.1Handling Logical Value



Use the bool type when you want to handle logical values, such as when expressing conditions such as "if ...". The bool type has only two values: "true" for true, and "false" for false (Figure 4-1).
  1. func main()
  2.   var b: bool
  3.   var n: int
  4.  
  5.   do b :: true
  6.   if(b)
  7.     do n :: 1 {This process is done.}
  8.   end if
  9.  
  10.   do b :: false
  11.   if(b)
  12.     do n :: 2 {This process is not done.}
  13.   end if
  14. end func
Figure 4-1: Boolean Type
In Kuin, the symbol "|" is used for logical disjunction, which corresponds to "... or ...", and the symbol "&" is used for logical conjunction, which corresponds to "... and ...". You can also write "conditional expression ?(value when true, value when false)", you can also get the value according to the logical value (Figure 4-2).
  1. func main()
  2.   var b1: bool :: true
  3.   var b2: bool :: false
  4.   var b3: bool
  5.   var n: int
  6.  
  7.   do b3 :: b1 | b2 {b3 = true}
  8.   do b3 :: b1 & b2 {b3 = false}
  9.   do n :: b1 ?(3, 5) {n = 3}
  10.   do n :: b2 ?(3, 5) {n = 5}
  11. end func
Figure 4-2: Logical Operation
The results of the "|" and "&" operators are shown in Table 4-1.
Table 4-1: Truth Table
Value of a Value of b Value of "a | b" Value of "a & b"
false false false false
false true true false
true false true false
true true true true
1660380149enf