home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Scripting Manual.doc / Scripting Manual.doc.rsrc / TEXT_150.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  7.2 KB  |  154 lines

  1. Numbers
  2.  
  3.     Numbers represent numerical quantities.  There are two kinds of numbers: standard and extended.  Standard numbers have six digits of precision and a range of ¬±10¬±30 and are very efficient in both time and memory.  Integers in the range ¬±32,000 are handled very efficiently and quickly.  Standard numbers are what you get by default.
  4.  
  5.     Extended numbers have fifteen digits of accuracy and a range of ¬±10¬±32,000.  However, they are about two to three times slower than standard numbers and take much more space.
  6.  
  7.     Both kinds of number represent base-ten quantities exactly with no rounding errors.
  8.  
  9.     In this chapter‚Äôs examples, x, y, and z represent any numeric values, n and b integer values, and s a string.
  10.  
  11.  
  12. Arithmetic
  13.     Numbers can perform the standard arithmetic operations and mathematical functions:
  14.  
  15. x + y    add
  16. x - y    subtract
  17. - x        negate
  18. x * y    multiply
  19. x / y    divide, dividing by zero results in ¬±infinity
  20. x % y    remainder
  21. x div y    integer division, integer result, rounded towards zero
  22. x.abs    absolute value
  23. x.sqrt    square root
  24. x.sin    trigonometric sine
  25. x.cos    trigonometric cosine
  26. x.tan    trigonometric tangent
  27. x min y    minimum of x and y
  28. x max y    maximum of x and y
  29. n.fib    the n-th Fibonacci number
  30.  
  31.  
  32. Comparison
  33.     Numbers can be compared with the normal comparison operations.  These comparisons all return either true or false.
  34.  
  35. x == y    equality
  36. x != y    inequality
  37. x < y    x less than y
  38. x <= y    x less than or equal to y
  39. x > y    x greater than y
  40. x >= y    x greater than or equal to y
  41. x between y and z    y <= x, and x <= z
  42.  
  43.  
  44. Precedence
  45.     The mathematical operators have precedence.  This means that a sequence of operations are evaluated as you would evaluate them yourself.  The precise rule is based on the concept of precedence.  Each operator has a precedence.  Operators with high precedence are evaluated before any operators of lower precedence.  Operators of the same precedence are evaluated left to right.  Of course, you can use parentheses to override this: expressions in parentheses are always evaluated first.
  46.   - x   <-- Highest Precedence
  47.     x * y   x / y   x % y
  48.     x + y   x - y
  49.     x < y   x <= y   x >= y   x > y
  50.   x == y   x != y   <-- Lowest Precedence
  51.  
  52.  
  53. Entering and Displaying
  54.     Numbers can be entered in scripts.  They follow the standard convention for entering numbers in computers.  In addition you can enter integers in base 2, 8 or 16.  Some examples are:
  55.  
  56.     165    an integer
  57.     3.14159    a number with a decimal part
  58.     0.2    just a decimal part
  59.     .2    the leading zero is optional (but recommended)
  60.     -10    a negative number
  61.     5e2    exponent notation: this equals 5 x 102, or 500
  62.     0.625e-9    this equals 0.625 x 10-9, or 0.000000000625
  63.     0xA5    hexadecimal (base 16) number, this equals 165
  64.     0o245    octal (base 8) number, this equals 165
  65.     0b10100101    binary (base 2) number, this equal 165
  66.  
  67.     When numbers are displayed, they are always displayed in base 10.  In general they are limited to three decimal places and only used exponent notation if needed.
  68.  
  69. n.name    a string that is the default display format of a number
  70.  format n using s    a string of the number formatted according to s
  71.  
  72.     The format string follows rules similar used in most spread sheets:  ‚Äò9‚Äôs indicated required digit positions, ‚Äò#‚Äôs indicate optional.  Most other characters are simply copied.  Note that it is possible to format numbers in ways that they can‚Äôt be entered.  This is the same message that number fields use to format their values for display.  The number format option of number fields lets you specify the format string.
  73.  
  74.     Some example formats and how they format the numbers: .314159, -23.4, and 181282:
  75.  
  76. "$#,##9.99"    $0.31    the dollar sign is put in front
  77.     -$23.40    forced to two decimal places
  78.     $181,282.00    commas added
  79.  
  80. "9.9###"    0.3142    rounded to four places
  81.     -23.4    only needed one decimal place
  82.     181282.0    always adds places to the left if needed
  83.  
  84. ".999e"    .314e0    forced exponent notation
  85.     -.234e2
  86.     .181e6
  87.  
  88. "#9.9e?"    0.3    exponent notation only if needed
  89.     -23.4
  90.     1.8e5    too big to fit, so uses exponent
  91.  
  92. "9%"    31%    percent multiplies the value by 100
  93.     -2340%
  94.     18128200%
  95.  
  96. "#,##9.99 cr; #,##9.99 db"    semicolon separates two formats:
  97.     0.31 cr    positive numbers use first 
  98.     23.40 db    negative numbers use second
  99.     181,282.00 cr
  100.  
  101.  
  102.  
  103. number scan s                convert a string to a number
  104. number scan-integer s base b        convert a string to n integer
  105.     
  106.     These two messages allow you to convert a string to a number.  Scan converts decimal numbers with optional decimal points, exponent notation, percent signs and minus signs.  It will also interpret a number in parentheses as a negative number (accounting style).  Scan-integer will convert only integer strings, but will do so in a number of bases:  If the base argument is supplied, then it is used as the base of the number.  The base can be anywhere from 2 to 16.  If the base isn‚Äôt specified, then it is assumed to be decimal, unless the string starts with ‚Äú0x‚Äù, ‚Äú0o‚Äù, or ‚Äú0b‚Äù, in which case it is hexadecimal, octal or binary respectively.
  107.  
  108. !!    Notice that these to messages are sent to the object number, not to any particular number.
  109.  
  110.  
  111.  
  112. Rounding and Truncation
  113.     Numbers can be rounded and truncated.
  114.  
  115. round x to n    round x to the n-th power of 10
  116. truncate x to n    truncate x to the n-th power of 10
  117.     
  118.     N defaults to 0, which rounds or truncates to integers.  If n is -2, then this gives two decimal places, if n is 3 then this rounds or truncates to thousands:
  119.  
  120.  round 2.345 to -2        Ô¨Å 2.35
  121.  truncate 2.345 to -2    Ô¨Å 2.34
  122.  round 4590 to 3        Ô¨Å 5000
  123.     truncate 4590 to 3        Ô¨Å 4000
  124.  
  125.  
  126. x.integer    integer portion of x (truncates toward zero)
  127. x.fraction    fractional part of x
  128.         x is equal to x.integer plus x.fraction
  129. x.ceiling    nearest integer >= to x (truncates toward +‚àû)
  130. x.floor    nearest integer <= to x (truncates toward -‚àû)
  131. x.mantissa    mantissa of x, always a number between -1 and 1
  132. x.exponent    exponent of x
  133.         x is equal to x.mantissa times 10 raised to x.exponent
  134.  
  135.  
  136. Extended Numbers & Conversion
  137.     Any numeric operation involving an extended number, results in an extended number.  Since extended numbers are less efficient in both time and memory, you will want to use them only when you need the precision, and convert back to standard when you can.  (Integer, ceiling, floor, and exponent are exceptions: they produce standard numbers if possible even if their argument is extended).
  138.  
  139. x.extended    returns an extended number equal to x
  140. x.standard    returns a standard number equal to x
  141.     These return an extended or standard version of x respectively.  If the number is already in the right format, it just returns it immediately.  When converting to standard from extended, the value is rounded as needed.
  142.  
  143. x.precision    returns the number of decimal digits of precision.
  144.     If x is a standard number, then 6.  If x is an extended number, then 16.  [ This is a bug, it should return 15. ]
  145.  
  146.  
  147. Random Numbers
  148.     
  149. choose n    returns a random integer between 1 and n, inclusive.
  150. choose n to m    returns a random integer between n and m, inclusive.
  151.  
  152. number.seed := x.    reset the random seed to x
  153.     This resets the random number generator to a particular point, x.  X should be a number between zero and one.  If you reset the random number generator to the same value, then the same numbers will be returned from the choose message.  This is useful if you are debugging a program that uses random numbers, because you can force the system to repeat exactly the same set of choices again.
  154.