;;; math.mhtml: -*- Meta-HTML -*-  A few math helpers.
;;; 
;;;  Copyright (c) 1998 Brian J. Fox
;;;  Author: Brian J. Fox (bfox@ai.mit.edu) Sun Apr 19 13:21:39 1998.
<defsubst float whitespace=delete>
  ;;; DOC_SECTION (ARITHMETIC-OPERATORS)
  ;;; Returns the floating point representation of <var x>.
  ;;; <var x> may be an integer, a floating point number, or a variable
  ;;; containing an integer or floating point number.
  ;;; <complete-example>
  ;;; <set-var foo=7>
  ;;; <float foo>
  ;;; <float 3>
  ;;; <float 3.0>
  ;;; </complete-example>
  <mul 1.0 %0>
</defsubst>

<defun number? arg &key base>
  ;;; DOC_SECTION (ARITHMETIC-OPERATORS)
  ;;; Returns "true" if <var arg> is the string representation of an integer
  ;;; in base <var base> (default <code>10</code>), or the string
  ;;; representation of a floating point number in base 10.
  ;;;
  ;;; For integer checks, the special value of zero (<code>0</code>) for
  ;;; <var base> allows the common radixes of decimal, octal, and hexadecimal
  ;;; to be understood.  That is to say:
  ;;; <example>
  ;;; <number? <get-var x> base=0>
  ;;; </example>
  ;;; is equivalent to:
  ;;; <example>
  ;;; <or <integer? <get-var x> base=8>
  ;;;     <integer? <get-var x> base=10>
  ;;;     <integer? <get-var x> base=16>
  ;;;     <real?    <get-var x>>>
  ;;; </example>
  ;;;
  ;;; Some examples:
  ;;; <example>
  ;;; <number? 10>          --> true
  ;;; <number? .9>          --> true
  ;;; <number 0xEF base=16> --> true
  ;;; </example>
  <defvar base 10>
  <or <integer? <get-var arg> base=<get-var base>> <real? <get-var arg>>>
</defun>

<defun avg &rest nums[]>
  ;;; DOC_SECTION (ARITHMETIC-OPERATORS)
  ;;; Returns the average of all of the arguments passed.
  ;;; Examples:
  ;;; <example>
  ;;; <avg 3 4 5>    --> 4
  ;;; <avg 4 9 3.2>  --> 5.40
  ;;; </example>
  <set-var divisor = <array-size nums>>
  <set-var dividend = <%%backquote-eval <add ,<get-var nums[]>>>>
  <div dividend divisor>
</defun>

<defun factorial x>
  ;;; DOC_SECTIONS (ARITHMETIC-OPERATORS)
  ;;; Computes the factorial of <var x>.
  ;;; Example:
  ;;; <complete-example>
  ;;; <factorial 12>
  ;;; </complete-example>
  <if <lt x 2>
      1
    <mul <float x> <factorial <sub x 1>>>>
</defun>

<defun percent num total>
  ;;; DOC_SECTION (ARITMETIC-OPERATORS)
  ;;; Return the percentage of <var total> that <var num> is.
  ;;; <example>
  ;;; <percent 20 200> --> 10.00
  ;;; </example>
  <if <not <number? <get-var-once num>>>
      <concat
       <parser::canonicalize-var num>
       <set-var num = <get-var <get-var num>>>>>
  <if <not <number? <get-var-once total>>>
      <concat
       <parser::canonicalize-var total>
       <set-var total = <get-var <get-var total>>>>>
  <div <mul num 100.00> total>
</defun>

<defun percentage percent total>
  ;;; DOC_SECTION (ARITMETIC-OPERATORS)
  ;;; Return the number which is <var percentage> of <var total>.
  ;;; <example>
  ;;; <percentage 10 200> --> 20.00
  ;;; </example>
  <if <not <number? <get-var-once percent>>>
      <concat
       <parser::canonicalize-var percent>
       <set-var percent = <get-var <get-var percent>>>>>
  <if <not <number? <get-var-once total>>>
      <concat
       <parser::canonicalize-var total>
       <set-var total = <get-var <get-var total>>>>>
  <div total <div 100.00 percent>>
</defun>

<defun med &rest nums[]>
  ;;; DOC_SECTION (ARITHMETIC-OPERATORS)
  ;;; Returns the median of all of the arguments passed.
  ;;; The median is defined as the number for which half of the rest of the
  ;;; numbers is greater, and half of the rest of the numbers is less.
  ;;; Example:
  ;;; <example>
  ;;; <med 3 4 5>       --> 4
  ;;; <med 2 3 7 8 10>  --> 5.40
  ;;; </example>
  <defvar nums[] 0>
  <sort nums numeric=true>
  <set-var center=<div <array-size nums> 2>>
  <get-var nums[center]>
</defun>

  