OSL ‐ Operators and Expressions - Mistium/Origin-OS GitHub Wiki
Mathematical Equations in OSL
In originOS scripting, mathematical equations play a crucial role in creating dynamic systems and performing calculations. These equations involve operators and comparisons that allow you to manipulate data and make decisions based on conditions. Here's a comprehensive guide to mathematical equations in OSL:
How They Function
Mathematical expressions are evaluated based on the given operators and data. For example:
if true and false (
)
When parsed, it becomes:
if false (
)
The and
statement replaces true and false
with false
, demonstrating how expressions can be dynamically altered.
Syntax
The general syntax for mathematical equations in OSL is:
data1 (operator) data2
Operators
Addition
- (If both data are integers) Adds the number on the right and left together.
- (If either data is not an integer) Joins the right and left with a space in-between.
- (If data on the left is an array) Appends the right data on the right to the array.
- (If data on the right is an array) Appends the left data on the left to the array. (v4.6.9 or later)
Example:
result = 5 + 3
// result is 8
text_result = "hello" + "world"
// text_result is "hello world"
array_result = ["apple"] + "orange"
// array_result is ["apple", "orange"]
array_result = "orange" + ["apple"]
// array_result is ["orange","apple"]
Concatenate
- (If both data are arrays) Concatenates the two arrays together.
- (Else) Joins the two sides together with no spaces.
Example:
array_result = ["hello"] ++ ["world"]
// array_result is ["hello", "world"]
text_result = "hello" ++ "world"
// text_result is "helloworld"
Minus
- Takes the number on the right away from the number on the left.
- (If the data on the left is an array and the right is a string) Removes all instances of the specified string from the array.
- (If the data on the left is an array and the right is a number) Removes the data at the specified index of the array.
- (If both sides are strings) Replaces all instances of the right string in the left string with ""
Example:
result = 8 - 3
// result is 5
array_result1 = ["hello"] - "hello"
// array_result1 is []
array_result2 = ["hello"] - 1
// array_result2 is []
text_result = "hello" - "llo"
// text_result is "he"
Divide
- Divides the number on the left by the number on the right.
Example:
result = 10 / 2
// result is 5
Multiply
- (If the data on the left is a number) Multiplies the number on the left and right together.
- (If the data on the left is text) Repeats the text by the number on the right.
Example:
result = 5 * 3
// result is 15
text_result = "hello" * 3
// text_result is "hellohellohello"
Modulo
- Runs modulus on the left number using the right number.
Example:
result = 10 % 3
// result is 1
Exponential
- Runs an exponential operation on the left number.
Example:
result = 2 ^ 3
// result is 8
Floor Divide
- Divides the number on the left by the number on the right and then rounds it down to the biggest whole number.
Example:
(floor divide):
result = 10 // 3
// result is 3
(normal divide):
result = 10 / 3
// result is 3.3333333
Comparisons
Equals (Case Insensitive)
- True if both sides are the same (non-case-sensitive).
Example:
if "apple" == "Apple" (
// This block will be executed because the comparison is not case-sensitive.
)
Not Equals (Case Insensitive)
- True if both sides aren't the same (non-case-sensitive).
Example:
if "apple" != "orange" (
// This block will be executed because the strings are not the same.
)
Equals (Case Sensitive)
- True if both sides are the same (case-sensitive).
Example:
if "apple" === "apple" (
// This block will be executed because the strings are the same in a case-sensitive manner.
)
Not Equal (Case Sensitive)
- True if both sides aren't the same (case-sensitive).
Example:
if "apple" !== "apple" (
// This block will not be executed because the strings are the same in a case-sensitive manner.
)
Greater Than
- True if the number on the left is bigger than the one on the right.
Example:
if 10 > 5 (
// This block will be executed because 10 is greater than 5.
)
Less Than
- True if the number on the right is bigger than the one on the left.
Example:
if 5 < 10 (
// This block will be executed because 5 is less than 10.
)
Not Greater Than
- True if the number on the left is not bigger than the one on the left.
Example:
if 5 !> 10 (
// This block will be executed because 5 is not greater than 10.
)
Not Less Than
- True if the number on the left is not less than the number on the right.
Example:
if 5 !< 10 (
// This block will not be executed because 5 is less than 10.
)
Greater Than Or Equal
- True if the number on the left is bigger than or equal to the one on the right.
Example:
if 10 >= 10 (
// This block will be executed because 10 is equal to 10.
)
Less Than Or Equal
- True
if the number on the right is bigger than or equal to the one on the left.
Example:
if 5 <= 10 (
// This block will be executed because 5 is less that 10.
)
In
- True if the right data contains the left data
Example:
if "apple" in "apples are great" (
// This block will be executed because the string "apple" is in the string "apples are great"
)
Boolean Expressions
And
- True if both sides are true.
Example:
if true and false (
// This block won't be executed because the condition is false.
)
Or
- True if at least one side is true.
Example:
if true or false (
// This block will be executed because the condition is true.
)
Xor
- True if exactly one side is true.
Example:
if true xor false (
// This block will be executed because only one side is true.
)
Nor
- True if both sides are false.
Example:
if false nor false (
// This block will be executed because both sides are false.
)
Nand
- True if both sides aren't true.
Example:
if false nand false (
// This block will be executed because both sides aren't true.
)
Advanced
Terniary
- If the boolean before the ? is true it returns the first string on the right else it returns the second string
Example:
say false ? "hello" "world"
// this will output "world"
say true ? "hello" "world"
// this will output "hello"
Example Use Cases
Use Case 1: Mathematical Operation.
number_result = 5 + 3
// number_result is 8
text_result = "hello" * 3
// text_result is "hellohellohello"
Use Case 2: Data Manipulation
array_result = ["apple"] + "orange"
// array_result is ["apple", "orange"]
text_result = "hello" - "llo"
// text_result is "he"
Use Case 3: String Comparison
if "apple" == "Apple" (
// This block will be executed because the comparison is not case-sensitive.
)
Use Case 4: Boolean Expressions
if true and false (
// This block won't be executed because the condition is false.
)
These examples showcase the versatility of mathematical equations and boolean expressions in originOS scripting, providing you with powerful tools to manipulate data and make logical decisions in your applications.