Simple Math

In computer programming, seldom happens that a value is not being manipulated – we always do this.

But scripting languages for game development are rather different. All the necessary computation or manipulations to a value is being done by the script engine. Although, there are some instances that we still have to do such calculations.

As such, KonsolScript provides a way for either use.

function main() {
  Var:Number w, x, y, z;
 
  w = 8;
  x = 24;
  y = 99;
 
  z = w * x - y;
 
  Konsol:Log(z)
}

The usual way to compute a value in programming is sampled above.

Var:Number w, x, y, z;

First, we declare variables.

  w = 8;
  x = 24;
  y = 99;

We, then, assign values.

  z = w * x - y;

Then, we do the needed computation.

  Konsol:Log(z)

And finally, do some logging if the expected value is computed correctly.

That very convenient way of manipulating a value is adapted to what most modern programming languages usually do to compute.

But originally, KonsolScript has its own and only way to do the same computation as the one above. I say only because the style above was just adapted a little later.

function main() {
  Var:Number w, x, y, z;
 
  Var:SetNumber w = 8;
  Var:SetNumber x = 24;
  Var:SetNumber y = 99;
 
  Math:Mul(w, x, z)
  Math:Sub(z, y, z)
 
  Konsol:Log(z)
}

As weirder as it looks, the sample code above does actually work and computes the same as the first sample.

But this should not get any weirder to some old folks who once used Assembly.

There is a big similarity to Assembly and KonsolScript when it comes to computation since the design of computing in KonsolScript is inspired by Assembly.

And of course, whatever you picked to use in your computation, it should work on KonsolScript.

Operator Precedence

Be warned that KonsolScript does not know Operator Precedence.

//a sample in C++ demonstrating Operator Precedence
int main() {
  int x, y;
 
  x = 8*24-99;
  y = -99+8*24;
 
  if (x == y) {
    cout << "equal";
  } else {
    cout << "not equal";
  }
 
  return 0;
}

The sample code above is written in C++. And it is expected to print “equal”.

//a sample in KonsolScript demonstrating absence of Operator Precedence
function main() {
  Var:Number x, y;
 
  x = 8*24-99;
  y = -99+8*24;
 
  if (x == y) {
    Konsol:Log("equal")
  } else {
    Konsol:Log("not equal")
  }
}

The sample code above is written in KonsolScript. And it is expected to print “not equal”. Because x is going to have a value of 93 and y is going to have -2184.

Why? Because KonsolScript computes the first two values it has before proceeding. The code below shows how KonsolScript undestands the statement.

//given the code:
  y = -99+8*24;
 
//KonsolScript sees it as -99 plus 8 which is -91
  y = -99+8~~~;
 
//then -91 multiplied by 24 is -2184
  y = -91*24;
 
//so y is expected to have a value of -2184
  y = -2184;

A work around on this is to group the sample below with parenthesis. By then, KonsolScript will print equal. See the sample below.

function main() {
  Var:Number x, y;
 
  x = 8*24-99;
  y = -99+(8*24);
 
  if (x == y) {
    Konsol:Log("equal")
  } else {
    Konsol:Log("not equal")
  }
}

This way, KonsolScript sees it as y = (8*24);, then y = -99+192; which is then y = 93;.

Last Words

KonsolScript offers:

which is also available in the “* / + - %” symbols.

If you are going to do some computation, remember to place a parenthesis to the values that you want to be executed first!

www.konsolscript.org
© 2005-2011 KonsolScript Labs | All Rights Reversed | Licensed under GNU GPL | Designed by Mj Mendoza IV
http://www.sourceforge.net