General HydroStatics
Ship Stability Software
Command of the Week
(New or interesting aspects of GHS that you may not know about)

MACRO /FUNction
(Requires GHS version 17.36 or later)

Variable arithmetic can be performed anywhere in GHS since version 17.30 using symbolic expressions inside braces (as seen in COW185). So you can show the distance between locations L1 and L2 using:

\ Distance: {((L1 - L2) ^2) ^(1/2)}

But squaring then taking the square root is kind of overkill given that all you want is the absolute value of L1 minus L2. It sure would be nice to be able to build your own functions like that into braced expressions!

That's now possible using MACROs defined with the /FUNction parameter. Such a function works the same as a regular macro except it has a function value that can be SET using its name before the equal sign. For example:

MACRO ABSVAL /FUN
    SET ABSVAL = ABS %1
/

Functions can be executed and their values used within braced expressions by following their names with parentheses around any parameters. So here's how this newly-minted ABSVAL function shows distance between L1 and L2:

\ Distance: {ABSVAL(L1 - L2)}

And you don't even need braces around the function when setting DISTANCE using the SET-free assignments introduced in COW186:

DISTANCE := ABSVAL(L1 - L2)

That all said... there's no need to define your own ABSVAL macro function, because all the useful SET operator keywords are available as predefined functions out of the box! That includes ABS, SIN, COS, TAN, LOG, all the string operators like SLICE, etc., etc. So this works without defining any functions:

DISTANCE := ABS(L1 - L2)

So how about defining a macro function that's actually useful? The LOG function that's predefined is base 10, but you can build on that to create exponential and natural logarithm functions for base e:

MACRO EXP /FUN
    EXP := 2.718281828459 ^ %1
/
MACRO LN /FUN
    LN := LOG(%1) / LOG(EXP(1))
/

There's lots more fun to be had with functions... stay tuned for future COWs!

Questions, comments, or requests?
Contact Creative Systems, Inc.

support@ghsport.com

USA phone: 360-385-6212 Fax: 360-385-6213
Office hours: 7:00 am - 4:00 pm Pacific Time, Monday - Friday

Mailing address:
PO Box 1910
Port Townsend, WA 98368 USA

www.ghsport.com

Click here for an index to this and previous COWs