04.03.2013 Views

Basic Micro Studio Syntax Manual

Basic Micro Studio Syntax Manual

Basic Micro Studio Syntax Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Fractional Multiplication (*/)<br />

Mod (//)<br />

Structure - Math<br />

Fractional multiplication will multiply a number with a fractional part. The multiplier must be a long<br />

value which is handled by a special method. The high 16 bits are the integer portion of the multiplier,<br />

the low 16 bits are the fractional part (expressed as a fraction of 65536). The result will be an integer<br />

with any fractional remainder discarded (not rounded).<br />

Example<br />

Let us say we want to multiply the number 346 x 2.5. The multiplier must be constructed as follows:<br />

The high 16 bits will have a value of 2. We can do this with:<br />

mult.highword = 2<br />

The low 16 bits will have a value that is half of 65535 or 32782 so:<br />

mult.lowword = 32782<br />

Then we do the fractional multiply:<br />

a = 346 */ mult<br />

The example will give “a” the value 865. A similar procedure will let you multiply by any fraction by<br />

expressing that fraction with a denominator of 65535 as closely as possible.<br />

Notice that half of 65535 is actually 32782.5; a number we can not enter as the fractional part. This<br />

means that multiplication by exactly half is not possible. However, the difference is so small that it has<br />

no effect on the actual outcome of the integer result.<br />

The mod function (short for “modulo”) returns the remainder after an integer division. So, for example,<br />

13 modulo 5 is 3 (the remainder after dividing 13 by 5).<br />

The mod function can be used to determine if a number is odd or even, as shown here:<br />

x var word<br />

y var word<br />

(code that sets the value of x)<br />

y = x//2<br />

if y=0 goto even ;zero indicates an even number<br />

if y=1 goto odd ;one indicates an odd number<br />

even<br />

(more code)<br />

odd<br />

(more code)<br />

Similarly, the mod function can be used to determine if a number is divisible by any other number.<br />

54

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!