Hello!
I’ve made simple math functions to emulate math methods from other languages into CS. Keep in mind, these functions are- simply put- primitive. There is no use of *script here, only pure CS, hence you might encounter performance issues.
Installation Guide
In your startup file, you will need a couple of variables.
startup.txt
*create n 0
*create pi "3."
*create/set implicit_control_flow true
Now you can rename n or pi, just remind yourself to also rename the variables in the math.txt file.
Speaking of math.txt. Create a math.txt file in the folder with your IF with these contents:
*comment ------------ Constants ------------
*comment ------------ pi ------------
*comment Return the number of pi. Precision included (up to 10 places)
*comment NOTE: You can increase the precision by increasing the length of the array and adding the following digit.
*comment Keep in mind that the digits SHOULD be in string format, else this function will fail.
*comment And yes, math is still possible in CS combining both int and str.
*comment gosub_scene math pi [precision]
*label pi
*temp i 0
*params x
*temp_array list 10 "1" "4" "1" "5" "9" "2" "6" "5" "3" "5"
*set pi "3."
*if x = 0
*set n 3
${n}
*return
*label pi_loop
*if (i = x) and (i != 0)
${pi}
*return
*else
*set i + 1
*set pi & {list[i]}
*goto pi_loop
*comment ------------ Basic Functions ------------
*comment ------------ pow ------------
*comment Returns the number x raised to the power y
*comment gosub_scene math ex [x] [y]
*comment NOTE: USE WHOLE NUMBERS ONLY
*label pow
*temp i 1
*params x y
*temp z 0
*set z x
*label pow_loop
*if i = y
*set n x
${n}
*return
*elseif y = 0
*set n 1
${n}
*return
*elseif y = 1
${x}
*return
*else
*set x * z
*set i + 1
*goto pow_loop
*comment ------------ sqrt ------------
*comment Returns the sqrt of a number
*comment gosub_scene math sqrt [x]
*comment NOTE: PRIMATIVE!
*comment Will only work for perfect squares.
*comment Might take longer for larger numbers.
*comment Will rework this in the future.
*label sqrt
*params x
*temp i 0
*label sqrt_loop
*if (i * i) <= x
*if (i * i) = x
*set n i
${n}
*return
*else
*set i + 1
*goto sqrt_loop
*else
Irrational number returned.
*return
*comment ------------ fact ------------
*comment Returns the factorial of a number
*comment Iterates and descends through the number, until 1, and returns the product
*comment gosub_scene math fact [x]
*label fact
*params x
*temp i 0
*temp z 0
*set z x
*if x = 0
1
*return
*else
*label fact_loop
*if x > 1
*set z * (x - 1)
*set x - 1
*goto fact_loop
*else
*set n z
${n}
*return
*comment ------------ ceil ------------
*comment Rounds UP to the nearest integer
*comment gosub_scene math ceil [x]
*label ceil
*params x
*set n 0
*label ceil_loop
*if x <= 0
${n}
*return
*if x > 0
*set n + 1
*set x - 1
*goto ceil_loop
*comment ------------ floor ------------
*comment Rounds DOWN to the nearest integer
*comment gosub_scene math floor [x]
*label floor
*params x
*set n 0
*label floor_loop
*if x <= 0
${n - 1}
*return
*if x > 0
*set n + 1
*set x - 1
*goto floor_loop
Make sure that the file is in the same folder your IF is in. There’s no need to put the file in your scene_list.
I’m not sure why would this be useful. I suppose it would be great for IF largely focused on gameplay elements.
So, without further a-do. Let me introduce all the current functions available.
Pi
Return the constant pi. You can return a certain precision of pi, currently up to 10 decimal digits, but you can add more digits. And yes, you can perform math equations with strings in CS.
*label pi
*temp i 0
*params x
*temp_array list 10 "1" "4" "1" "5" "9" "2" "6" "5" "3" "5"
*set pi "3."
*if x = 0
*set n 3
${n}
*return
*label pi_loop
*if (i = x) and (i != 0)
${pi}
*return
*else
*set i + 1
*set pi & {list[i]}
*goto pi_loop
To add another digit, increase the length of the array then add the digit.
Example code:
*subscene math pi 4
Will return with:
3.1415
Exponents
Returns a value of x to the power of y.
*label pow
*temp i 1
*params x y
*temp z 0
*set z x
*label pow_loop
*if i = y
*set n x
${n}
*return
*elseif y = 0
*set n 1
${n}
*return
*elseif y = 1
${x}
*return
*else
*set x * z
*set i + 1
*goto pow_loop
Keep in mind that this won’t work for rational numbers (i.e: 1.1, 2.3, 4.5).
Example code:
*subscene math pow 3 2
Will return with:
9
Square Root
Returns the value of the square root of x.
*label sqrt
*params x
*temp i 0
*label sqrt_loop
*if (i * i) <= x
*if (i * i) = x
*set n i
${n}
*return
*else
*set i + 1
*goto sqrt_loop
*else
Irrational number returned.
*return
Note that this function is a brute force method. It only works for perfect squares (products of numbers multiplied by itself. i.e: 4, 9, 16), otherwise will return “Irrational number returned” meaning it simply cannot calculate the expression. For example, √2. It could be possible for me to revise this to return an irrational value, but I simply don’t want to, as of now.
Example code:
*subscene math sqrt 16
Will return with:
4
Factorials
Returns the product of all whole numbers of x down to 1. Will be gradually slower as you approach larger numbers.
*label fact
*params x
*temp i 0
*temp z 0
*set z x
*if x = 0
1
*return
*else
*label fact_loop
*if x > 1
*set z * (x - 1)
*set x - 1
*goto fact_loop
*else
*set n z
${n}
*return
Example code:
*subscene math fact 5
Will return with:
120
Ceil
Rounds UP to the nearest integer. Only possible for non-negative integers, as of now.
*label ceil
*params x
*set n 0
*label ceil_loop
*if x <= 0
${n}
*return
*if x > 0
*set n + 1
*set x - 1
*goto ceil_loop
Example code:
*subscene math fact 4.3
Will return with:
5
Floor
Rounds DOWN to the nearest integer. Only possible for non-negative integers, as of now.
*label floor
*params x
*set n 0
*label floor_loop
*if x <= 0
${n - 1}
*return
*if x > 0
*set n + 1
*set x - 1
*goto floor_loop
Example code:
*subscene math fact 4.3
Will return with:
4
As an example, here is a Demo for all the math functions.
This project isn’t tested well enough. If you run into problems, please provide your encounters.
If you also have any feedback/suggestions, also put them here.

