SimpleMath Script Tutorial

 

SimpleMath Script Tutorial

Welcome to the SimpleMath Script tutorial! This guide will help you learn the basics of our programmable calculator script, which has a syntax similar to JavaScript but is simplified to support only number and boolean types.

Variables

Declare variables using the 'var' keyword:
var x;
var y = 5;
Assign values to variables:
x = y;
x = 3 + y;

Data Types

SimpleMath supports two data types:
  • Numbers: Any numeric value
  • Booleans: true or false
Types are inferred, not specified.

Operators

SimpleMath supports the following operators:

+ : 2 + 1 -> 3
- : 2 - 1 -> 1
* : 2 * 3 -> 6 (multiplication)
/ : 4 / 2 -> 2 (division)
^ : 2 ^ 3 -> 8 (exponentiation)
% : 3 % 2 -> 1 (remainder)
= : var num1 = 1; var num2 = 2; (assignment)
num1 + num2 -> 3
== : 3 == 3 (3 equals 3) -> true
3 == 2 (3 equals 2) -> false
!= : 3 != 2 (3 is not equal to 2) -> true
3 != 3 (3 is not equal to 3)-> false
< : 2 < 3 (2 is less than 3) -> true
> : 3 > 2 (3 is greater than 2) -> true
<= : 2 <= 3 (2 is less than or equal to 3) -> true
>= : 3 >= 2 (3 is greater than or equal to 2) -> true
|| : true || false (it is true or false) -> true
&& : true && false (it is true and false) -> false

Control Structures

All control structures must use curly braces {} for their body:

if (x > 0) { **code block here } else if (x < 0) { **code block here } else { **code block here } while (condition) { ** code block here } for (var i = 0; i < 10; i = i + 1) { ** code block here } switch (expression) { case value1: ** code block here break; case value2: ** code block here break; default: ** code block here } do { ** code block here } while (condition);

Functions

Define functions using the 'def' keyword. There are two main types of function definitions:
  1. Expression-style:

def add(a, b) = a + b;
  1. Block-style:

def multiply(a, b) { return a * b; }
For recursive functions, you can define base cases:

def factorial(n) = n * factorial(n - 1); factorial(0) = 1;

Examples

Here are some examples to help you get started:
**Calculate the sum of numbers from 1 to n
def sum(n) { var total = 0; for (var i = 1; i <= n; i = i + 1) { total = total + i; } return total; } **Recursive Fibonacci function def fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2); fibonacci(0) = 0; fibonacci(1) = 1; **Calculate the Greatest Common Divisor (GCD) def GCD(a, b) { while (b != 0) { var temp = b; b = a % b; a = temp; } return a; }
Remember, every code line should end with a semicolon (;). 

Be careful!!

Comment with "//" or "/**/" or "#" is not supported.

SimpleMath provides math functions such as sin, cos, tan, toRadians, toDegrees, abs, floor, ceil, round, log(a,b), log10, ln(x) for log(e,x), sqrt, cbrt etc. 
But SimpleMath does not have type integer. Only number type. So when integer value is necessary make sure that floor or ceil or round should be applied to the value. Otherwise infinite loop or endless recursion might happen.

Happy coding with SimpleMath!

Comments

Popular posts from this blog

Prompt for AI

How Deep Is the Well?

Hanoi Tower - How many to move disks.