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
- Handling Integers And Decimals
- Comparing Values
- Convert Type
- Obtain Absolute Value And Sign
- Set Value To Within Upper And Lower Limit
- Use Elementary Functions Such As Trigonometric Functions
- Generate Random Number
- Perform Mathematical Calculations Such As Factorial And Prime Factorization
Performs Bitwise Operations
Handling Character
Handling Logical Value
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).
- func main()
- var a: int :: 7
- var b: int :: 3
- var c: int
- do c :: a + b {c = 10}
- do c :: a - b {c = 4}
- do c :: a * b {c = 21}
- do c :: a / b {c = 2}
- do c :: a % b {c = 1}
- do c :: a ^ b {c = 343}
-
- var x: float :: 7.0
- var y: float :: 3.0
- var z: float
- do z :: x + y {z = 10.0}
- do z :: x - y {z = 4.0}
- do z :: x * y {z = 21.0}
- do z :: x / y {z = 2.33333...}
- do z :: x % y {z = 1.0}
- do z :: x ^ y {z = 343.0}
- end func
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).
- func main()
- var n: int :: 3
- var m: int :: 5
- var b: bool
- do b :: n < m {true}
- do b :: n <= m {true}
- do b :: n > m {false}
- do b :: n >= m {false}
- do b :: n = m {false}
- do b :: n <> m {true}
- end func
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).
- func main()
- var x: float :: lib@cos(lib@pi) {There is a slight calculation error compared to -1.0.}
- var y: float :: -1.0
- var b: bool
- do b :: x = y {false}
- do b :: lib@same(x, y) {true}
- end func
1.3Convert Type
To convert between int and float types, use the cast operator "$" (Figure 1-4).
- func main()
- var n: int :: -2
-
- var x: float
- do x :: n $ float {x = -2.0}
- var m: int
- do m :: x $ int {m = -2}
- end func
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).
- func main()
- var a: int :: -3
- var b: int :: 5
- var c: int :: 0
- var d: int
- do d :: a.abs() {3}
- do d :: b.abs() {5}
- do d :: c.abs() {0}
- do d :: a.sign() {-1}
- do d :: b.sign() {1}
- do d :: c.sign() {0}
- end func
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).
- func main()
- var a: int :: 2
- var b: int :: 5
- var c: int :: 9
- var d: int
- do d :: lib@max(a, 4) {4}
- do d :: lib@max(b, 4) {5}
- do d :: lib@max(c, 4) {9}
- do d :: lib@min(a, 7) {2}
- do d :: lib@min(b, 7) {5}
- do d :: lib@min(c, 7) {7}
- do d :: lib@clamp(a, 4, 7) {4}
- do d :: lib@clamp(b, 4, 7) {5}
- do d :: lib@clamp(c, 4, 7) {7}
- end func
1.6Use Elementary Functions Such As Trigonometric Functions
To use elementary functions such as trigonometric functions, use the functions in lib@ (Figure 1-7).
- func main()
- var x: float
- do x :: lib@sin(lib@pi / 2.0) {sin(π / 2.0) = 1.0}
- do x :: lib@ln(lib@e) {log_e(e) = 1.0}
- do x :: lib@log(2.0, 8.0) {log_2.0(8.0) = 3.0}
- do x :: lib@sqrt(9.0) {√(9.0) = 3.0}
- end func
1.7Generate Random Number
To generate random numbers, use the lib@rnd and lib@rndFloat functions (Figure 1-8).
- func main()
- var n: int :: lib@rnd(3, 5) {A random number between 3 and 5.}
- var x: float :: lib@rndFloat(3.0, 5.0) {A random decimal number between 3.0 and 5.0.}
- end func
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).
- func main()
- var rnd: lib@Rnd :: lib@makeRnd(1234b32) {The argument is the seed of the random number.}
- var a: int :: rnd.rnd(1, 10) {a = 2}
- var b: int :: rnd.rnd(1, 10) {b = 5}
- var c: int :: rnd.rnd(1, 10) {c = 4}
- var d: int :: rnd.rnd(1, 10) {d = 7}
-
- do rnd :: lib@makeRnd(1234b32)
- do a :: rnd.rnd(1, 10) {a = 2}
- do b :: rnd.rnd(1, 10) {b = 5}
- do c :: rnd.rnd(1, 10) {c = 4}
- do d :: rnd.rnd(1, 10) {d = 7}
- end func
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).
- func main()
- var x: float :: math@fact(6.0) {6.0! = 720.0}
- var n: int :: math@lcm(12, 16) {The least common multiple of 12 and 16 = 48}
- var m: int :: math@gcd(12, 16) {The greatest common denominator of 12 and 16 = 4}
- var b: bool :: math@prime(2147483647) {Whether 2147483647 is a prime number = true}
- var ps: []int :: math@primeFactors(44444) {The prime factorization of 44444 = [2, 2, 41, 271]}
- end func
2Performs Bitwise Operations
To perform operations in bit units, use the bit8, bit16, bit32, and bit64 types (Figure 2-1).
- func main()
- var a: bit8 :: 0x12b8 {a = 0x12}
- var b: bit16 :: 0x1234b16 {b = 0x1234}
- var c: bit32 :: 0x12345678b32 {c = 0x12345678}
- var d: bit64 :: 0x123456789ABCDEF0b64 {d = 0x123456789ABCDEF0}
- end func
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).
- func main()
- var a: bit16 :: 0xF333b16 {a = 0xF333}
- var b: bit16 :: 0x1234b16 {b = 0x1234}
- var c: bit16
-
- do c :: a.and(b) {c = 0x1230}
- do c :: a.or(b) {c = 0xF337}
- do c :: a.not() {c = 0x0CCC}
- do c :: a.xor(b) {c = 0xE107}
- do c :: a.shl(8) {c = 0x3300 (8-bit logical shift of a to the left.)}
- do c :: a.shr(8) {c = 0x00F3 (8-bit logical shift of a to the right.)}
- do c :: a.sar(8) {c = 0xFFF3 (8-bit arithmetic shift a to the right.)}
- do c :: a.endian() {c = 0x33F3 (Endian conversion.)}
- end func
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)。
- func main()
- var c1: char :: 'A'
- var n: int :: c1 $ int {n = 65 (0x41)}
- var c2: char :: n $ char {c2 = 'A'}
- end func
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).
- func main()
- var c: char
- do c :: '\n' {Newline character.}
- do c :: '\t' {Tab character.}
- do c :: '\"' {Double quotation character.}
- do c :: '\'' {Single quotation character.}
- do c :: '\\' {\ character.}
- do c :: '\u0041' {Character with the character code 0x0041 ('A').}
- end func
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).
- func main()
- var b: bool
- var n: int
-
- do b :: true
- if(b)
- do n :: 1 {This process is done.}
- end if
-
- do b :: false
- if(b)
- do n :: 2 {This process is not done.}
- end if
- end func
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).
- func main()
- var b1: bool :: true
- var b2: bool :: false
- var b3: bool
- var n: int
-
- do b3 :: b1 | b2 {b3 = true}
- do b3 :: b1 & b2 {b3 = false}
- do n :: b1 ?(3, 5) {n = 3}
- do n :: b2 ?(3, 5) {n = 5}
- end func
The results of the "|" and "&" operators are shown in Table 4-1.
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 |