Skip to content

Simple Maths

zenonet edited this page May 19, 2022 · 6 revisions

Addition

You can add to values (a, b) together by increasing a b times in a loop.
This is very simple in code:
Works in: v0.4+

//Set addend 1 to 10
lime killed

//Set addend 2 to 3
cyan vented
cyan vented
cyan vented

//Select the counting variable for the loop
sus cyan

[
    //Increase addend 1
    lime vented

    //Decrease addend 2
    cyan wasWithMe

    //When addend 2 becomes 0, the loop will exit and the addition is done
]

//Output the result
sus lime
report

Subtraction

Subtraction is very easy too, in fact, it's the same except this time, we need to decrease a b times:
Works in: v0.4+

//Set addend 1 to 10
lime killed

//Set addend 2 to 3
cyan vented
cyan vented
cyan vented

//Select the counting variable for the loop
sus cyan

[
    //Increase addend 1
    lime vented

    //Decrease addend 2
    cyan wasWithMe

    //When addend 2 becomes 0, the loop will exit and the addition is done
]

//Output the result
sus lime
report

Multiplication

To multiply to values we just need to repeatedly add one factor to the other.
We can do this with 2 nested loops like this:

Works in: v0.4+

//Factor1: (3)
sus red
he vented
he vented
he vented

//Factor2: (2)
sus blue
he vented
he vented


//It's important that something is selected at this point, that is not 0 because otherwise the loop won't even start
[
    orange wasWith blue //Buffer blue because factor2 will change in this loop
    sus orange //Select orange as the counter for the coming loop
    [
        
        green vented //Increase the output
        
        orange wasWithMe //Decrease the counter
    ]
    
    sus red //Reselect the counter of the first loop
    red wasWithMe //Decrease the counter of the first loop
]

//Print the output variable as a number
sus green 
report //Outputs 6

Division

Division is a bit more complex.
This example was provided by SandanSpitzhacke

Works in v0.4+

//Set dividend to 10:
sus red
he killed

//Set divisor to 2:
sus blue
he vented
he vented

// red / blue = purple

orange wasWith red
yellow wasWith blue
sus orange
[
    [
        white wasWith black 
        orange wasWithMe yellow wasWithMe sus yellow 
        [
            sus orange 
            [
                sus white
            ]
        ] 
        white vented
    ]
    purple vented
    yellow wasWith blue
    sus orange
]
sus purple
report //Outputs 5
Clone this wiki locally