spec

Software for Diffraction

1.3.1. - spec as a Calculator



In some respects, the spec user interface behaves like a BASIC language interpreter that uses the C language syntax. For example, you can easily print strings and the results of arithmetic expressions:
1.FOURC> p 2+2, sqrt(3), "2^16 =", 1<<16
4 1.73205 2^16 = 65536 2.FOURC>

(The p macro is defined as print, a built-in command.) You do not need to search for your calculator, as all the standard operators and functions are available.

The arithmetic operators (= , * , / , % , + , - , ++ , --, += , -= , *= , /= , %= ), the relational operators (> , < , <= , >= , == , != ), the boolean operators (! , && , || ), the bitwise operators (>> , << , ~ , & , ^ , | , >>= , <<= , &= , ^= , |= ) and the ternary operator (?:) are all available. Parentheses can be used for grouping within expressions. See the Reference Manual for a description of all the operators and their rules of precedence.

The most useful standard C math functions are included, such as sin(), cos(), tan(), asin(), acos(), atan(), exp(), log(), log10(), pow(), sqrt(), and fabs(). Conversions functions such as deg() and rad() convert between degrees and radians, while bcd() and dcb() convert between decimal and binary-coded decimal. A rand() function to return random numbers is also provided.

Numbers can be entered in decimal, octal or hexadecimal notation, just as in C.
1.FOURC> p 100, 0100, 0x100
100 64 256 2.FOURC>



Special string functions also exist. The date() function provides the current date and time as a string:
1.FOURC> p date()
Mon Feb 15 02:13:13 1994 2.FOURC>

The date() function can also take an argument that is the number of seconds from the UNIX epoch.
1.FOURC> p date(1e9)
Sat Sep 8 21:46:40 2001 2.FOURC> p date(0)
Wed Dec 31 19:00:00 1969 3.FOURC> p int(time()), date(time())
729760917 Mon Feb 15 02:21:57 1994 4.FOURC>

The second example shows the (Eastern Standard Time) moment of the UNIX epoch. The function time() returns the number of seconds since that moment, including a fractional part with a resolution determined by the system clock. The difference of subsequent calls to time() can, for example, give a reasonable elapsed time for each point in a scan.

The function input() reads a string from the keyboard. An optional argument will be printed first. For example, a macro or command file might prompt you for information:
1.FOURC> TITLE = input("Please enter a title:  ")
Please enter a title: Au (001) Sample #1 2.FOURC>



Other string functions such as index(), substr(), length(s) and sprintf(format, [args]) are also available. See the Reference Manual for details.