FluxSharp Standard Library and Math Functions
Built-in functions for common operations.
Math Functions
Mathematical functions for numerical operations.
Absolute Value (abs)
Purpose: Returns the absolute value (magnitude) of a number, removing the sign.
int result = abs(-42); // 42
int positive = abs(10); // 10
Power (pow)
Purpose: Raises a number to a power. Returns base raised to the exponent.
int squared = pow(5, 2); // 25 (5²)
int cubed = pow(2, 3); // 8 (2³)
double result = pow(2.5, 2); // 6.25
Square Root (sqrt)
Purpose: Returns the square root of a number.
double result = sqrt(16.0); // 4.0
double root = sqrt(2.0); // 1.414...
Floor (floor)
Purpose: Returns the largest integer less than or equal to the number.
double value = 3.7;
int floored = floor(value); // 3
double negative = -2.3;
int negfloor = floor(negative); // -3
Ceiling (ceil)
Purpose: Returns the smallest integer greater than or equal to the number.
double value = 3.2;
int ceiled = ceil(value); // 4
double negative = -2.7;
int negceil = ceil(negative); // -2
Rounding (round)
Purpose: Rounds a number to the nearest integer.
double value = 3.5;
int rounded = round(value); // 4
double smaller = 3.2;
int rounded2 = round(smaller); // 3
Minimum (min)
Purpose: Returns the smaller of two numbers.
int smallest = min(5, 3); // 3
int negative = min(-10, -5); // -10
double smaller = min(2.5, 2.3); // 2.3
Maximum (max)
Purpose: Returns the larger of two numbers.
int largest = max(5, 3); // 5
int bigger = max(-10, -5); // -5
double larger = max(2.5, 2.3); // 2.5
Absolute Difference (absdiff)
Purpose: Returns the absolute difference between two numbers.
int diff = absdiff(5, 2); // 3
int reverse = absdiff(2, 5); // 3 (order doesn't matter)
Modulo (mod or %)
Purpose: Returns the remainder after division.
int remainder = 17 % 5; // 2
int evencheck = 10 % 2; // 0 (divisible)
String Functions
String manipulation functions.
Length
Purpose: Returns the number of characters in a string.
int len = "hello".length(); // 5
int emptylen = "".length(); // 0
Concatenation (+)
Purpose: Combines two strings together.
string greeting = "Hello" + " " + "World"; // "Hello World"
string message = "Count: " + "42";
Substring
Purpose: Extracts a portion of a string from start to end position.
string text = "Hello World";
string sub = substring(text, 0, 5); // "Hello"
string world = substring(text, 6, 11); // "World"
Character At
Purpose: Returns the character at a specific position in a string.
string text = "Hello";
char ch = charat(text, 0); // 'H'
char last = charat(text, 4); // 'o'
To Uppercase
Purpose: Converts all characters to uppercase.
string upper = "hello".toupper(); // "HELLO"
string mixed = "HeLLo".toupper(); // "HELLO"
To Lowercase
Purpose: Converts all characters to lowercase.
string lower = "HELLO".tolower(); // "hello"
string mixed = "HeLLo".tolower(); // "hello"
String Contains
Purpose: Checks if a string contains another string.
bool hasworld = contains("Hello World", "World"); // true
bool hastest = contains("Hello World", "test"); // false
String Index Of
Purpose: Finds the position of a substring in a string (or -1 if not found).
int pos = indexof("Hello World", "World"); // 6
int notfound = indexof("Hello World", "xyz"); // -1
Array Functions
Array manipulation functions.
Array Length
Purpose: Returns the size of an array (number of elements).
int[10] arr;
int size = length(arr); // 10
byte[256] buffer;
int bufsize = length(buffer); // 256
Array Clear
Purpose: Sets all elements of an array to zero/default.
int[100] data;
clear(data); // All elements become 0
string[10] names;
clear(names); // All elements become empty string
Array Copy
Purpose: Copies elements from one array to another.
int[10] source;
int[10] destination;
// Initialize source
for (int i = 0; i < 10; i++) {
source[i] = i * 10;
}
// Copy to destination
copy(source, destination);
Array Fill
Purpose: Fills an array with a specific value.
int[100] numbers;
fill(numbers, 42); // All elements become 42
string[10] names;
fill(names, "empty"); // All elements become "empty"
Type Conversion Functions
Int to String
Purpose: Converts an integer to its string representation.
int number = 42;
string text = tostring(number); // "42"
string negative = tostring(-100); // "-100"
String to Int
Purpose: Converts a string to an integer value.
string text = "42";
int value = toint(text); // 42
string negative = "-100";
int negvalue = toint(negative); // -100
Float to Int
Purpose: Converts a floating-point number to an integer (truncates decimal).
double value = 3.7;
int truncated = toint(value); // 3
double pi = 3.14159;
int piint = toint(pi); // 3
Int to Float
Purpose: Converts an integer to a floating-point number.
int number = 42;
double decimal = todouble(number); // 42.0
int five = 5;
float fivefloat = tofloat(five); // 5.0
Memory and System Functions
Memory Allocation (malloc)
Purpose: Allocates a block of memory of a given size.
byte[1024] buffer = malloc(1024);
Memory Free (free)
Purpose: Frees allocated memory.
free(buffer);
Sleep
Purpose: Pauses program execution for a specified number of milliseconds.
sleep(1000); // Sleep for 1 second
sleep(500); // Sleep for half a second
I/O Functions
Purpose: Outputs text to the console/standard output.
print("Hello");
print("Count: ");
Print Line
Purpose: Outputs text followed by a newline.
printline("Hello World"); // Adds newline after
printline("Line 2");
Print Integer
Purpose: Outputs an integer value to the console.
int value = 42;
printint(value); // Prints "42"
Print Float
Purpose: Outputs a floating-point number to the console.
double pi = 3.14159;
printfloat(pi); // Prints "3.14159"
Read Line
Purpose: Reads a line of text from standard input.
string input = readline(); // Waits for user input
Read Integer
Purpose: Reads an integer from standard input.
int value = readint(); // Reads integer from user
Tip: These built-in functions are essential for writing practical programs. Use them to manipulate data, perform calculations, and interact with users.