home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume24 / gnucalc / part41 < prev    next >
Encoding:
Text File  |  1991-11-01  |  55.5 KB  |  1,281 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@synaptics.com (David Gillespie)
  3. Subject:  v24i089:  gnucalc - GNU Emacs Calculator, v2.00, Part41/56
  4. Message-ID: <1991Nov1.183645.20812@sparky.imd.sterling.com>
  5. X-Md4-Signature: 87c4d2c362e0f34785231a6a84a2e74d
  6. Date: Fri, 1 Nov 1991 18:36:45 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: daveg@synaptics.com (David Gillespie)
  10. Posting-number: Volume 24, Issue 89
  11. Archive-name: gnucalc/part41
  12. Environment: Emacs
  13. Supersedes: gmcalc: Volume 13, Issue 27-45
  14.  
  15. ---- Cut Here and unpack ----
  16. #!/bin/sh
  17. # do not concatenate these parts, unpack them in order with /bin/sh
  18. # file calc.texinfo continued
  19. #
  20. if test ! -r _shar_seq_.tmp; then
  21.     echo 'Please unpack part 1 first!'
  22.     exit 1
  23. fi
  24. (read Scheck
  25.  if test "$Scheck" != 41; then
  26.     echo Please unpack part "$Scheck" next!
  27.     exit 1
  28.  else
  29.     exit 0
  30.  fi
  31. ) < _shar_seq_.tmp || exit 1
  32. if test ! -f _shar_wnt_.tmp; then
  33.     echo 'x - still skipping calc.texinfo'
  34. else
  35. echo 'x - continuing file calc.texinfo'
  36. sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
  37. (This is entirely equivalent to @kbd{125 RET 1:3 ^}.)
  38. X
  39. @kindex %
  40. @pindex calc-mod
  41. @tindex %
  42. The @kbd{%} (@code{calc-mod}) command performs a ``modulo'' (or ``remainder'')
  43. operation.  Mathematically, @samp{a%b = a - floor(a/b)*b}, and is defined
  44. for all real numbers @cite{a} and @cite{b} (except @cite{b=0}).  For
  45. positive @cite{b}, the result will always be between 0 (inclusive) and
  46. @cite{b} (exclusive).  Modulo does not work for HMS forms and error forms.
  47. If @cite{a} is a modulo form, its modulo is changed to @cite{b}, which
  48. must be positive real number.
  49. X
  50. @kindex \
  51. @pindex calc-idiv
  52. @tindex idiv
  53. @tindex \
  54. The @kbd{\} (@code{calc-idiv}) command divides two numbers on the stack
  55. to produce an integer result.  It is equivalent to dividing with
  56. @key{/}, then rounding down with @kbd{F} (@code{calc-floor}), only a bit
  57. more convenient and efficient.  Also, since it is an all-integer
  58. operation when the arguments are integers, it avoids problems that
  59. @kbd{/ F} would have with floating-point roundoff.
  60. X
  61. @kindex :
  62. @pindex calc-fdiv
  63. @tindex fdiv
  64. The @kbd{:} (@code{calc-fdiv}) command [@code{fdiv} function in a formula]
  65. divides the two integers on the top of the stack to produce a fractional
  66. result.  This is a convenient shorthand for enabling Fraction Mode (with
  67. @kbd{m f}) temporarily and using @samp{/}.  Note that during numeric entry
  68. the @kbd{:} key is interpreted as a fraction separator, so to divide 8 by 6
  69. you would have to type @kbd{8 @key{RET} 6 @key{RET} :}.  (Of course, in
  70. this case, it would be much easier simply to enter the fraction directly
  71. as @kbd{8:6 @key{RET}}!)
  72. X
  73. @kindex n
  74. @pindex calc-change-sign
  75. The @kbd{n} (@code{calc-change-sign}) command negates the number on the top
  76. of the stack.  It works on numbers, vectors and matrices, HMS forms, date
  77. forms, error forms, intervals, and modulo forms.
  78. X
  79. @kindex A
  80. @pindex calc-abs
  81. @tindex abs
  82. The @kbd{A} (@code{calc-abs}) [@code{abs}] command computes the absolute
  83. value of a number.  The result of @code{abs} is always a nonnegative
  84. real number:  With a complex argument, it computes the complex magnitude.
  85. With a vector or matrix argument, it computes the Frobenius norm, i.e.,
  86. the square root of the sum of the squares of the absolute values of the
  87. elements.  The absolute value of an error form is defined by replacing
  88. the mean part with its absolute value and leaving the error part the same.
  89. The absolute value of a modulo form is undefined.  The absolute value of
  90. an interval is defined in the obvious way.
  91. X
  92. @kindex f A
  93. @pindex calc-abssqr
  94. @tindex abssqr
  95. The @kbd{f A} (@code{calc-abssqr}) [@code{abssqr}] command computes the
  96. absolute value squared of a number, vector or matrix, or error form.
  97. X
  98. @kindex f s
  99. @pindex calc-sign
  100. @tindex sign
  101. The @kbd{f s} (@code{calc-sign}) [@code{sign}] command returns 1 if its
  102. argument is positive, @i{-1} if its argument is negative, or 0 if its
  103. argument is zero.  In algebraic form, you can also write @samp{sign(a,x)}
  104. which evaluates to @samp{x * sign(a)}, i.e., either @samp{x}, @samp{-x}, or
  105. zero depending on the sign of @samp{a}.
  106. X
  107. @kindex &
  108. @pindex calc-inv
  109. @tindex inv
  110. @cindex Reciprocal
  111. The @kbd{&} (@code{calc-inv}) [@code{inv}] command computes the
  112. reciprocal of a number, i.e., @cite{1 / x}.  Operating on a square
  113. matrix, it computes the inverse of that matrix.
  114. X
  115. @kindex Q
  116. @pindex calc-sqrt
  117. @tindex sqrt
  118. The @kbd{Q} (@code{calc-sqrt}) [@code{sqrt}] command computes the square
  119. root of a number.  For a negative real argument, the result will be a
  120. complex number whose form is determined by the current Polar Mode.
  121. X
  122. @kindex f h
  123. @pindex calc-hypot
  124. @tindex hypot
  125. The @kbd{f h} (@code{calc-hypot}) [@code{hypot}] command computes the square
  126. root of the sum of the squares of two numbers.  That is, @samp{hypot(a,b)}
  127. is the length of the hypotenuse of a right triangle with sides @cite{a}
  128. and @cite{b}.  If the arguments are complex numbers, their squared
  129. magnitudes are used.
  130. X
  131. @kindex f Q
  132. @pindex calc-isqrt
  133. @tindex isqrt
  134. The @kbd{f Q} (@code{calc-isqrt}) [@code{isqrt}] command computes the
  135. integer square root of an integer.  This is the true square root of the
  136. number, rounded down to an integer.  For example, @samp{isqrt(10)}
  137. produces 3.  Note that, like @kbd{\} [@code{idiv}], this uses exact
  138. integer arithmetic throughout to avoid roundoff problems.  If the input
  139. is a floating-point number or other non-integer value, this is exactly
  140. the same as @samp{floor(sqrt(x))}.
  141. X
  142. @kindex f n
  143. @kindex f x
  144. @pindex calc-min
  145. @tindex min
  146. @pindex calc-max
  147. @tindex max
  148. The @kbd{f n} (@code{calc-min}) [@code{min}] and @kbd{f x} (@code{calc-max})
  149. [@code{max}] commands take the minimum or maximum of two real numbers,
  150. respectively.  These commands also work on HMS forms, date forms,
  151. intervals, and infinities.  (In algebraic expressions, these functions
  152. take any number of arguments and return the maximum or minimum among
  153. all the arguments.)@refill
  154. X
  155. @kindex f M
  156. @kindex f X
  157. @pindex calc-mant-part
  158. @tindex mant
  159. @pindex calc-xpon-part
  160. @tindex xpon
  161. The @kbd{f M} (@code{calc-mant-part}) [@code{mant}] function extracts
  162. the ``mantissa'' part @cite{m} of its floating-point argument; @kbd{f X}
  163. (@code{calc-xpon-part}) [@code{xpon}] extracts the ``exponent'' part
  164. @cite{e}.  The original number is equal to @c{$m \times 10^e$}
  165. @cite{m * 10^e},
  166. where @cite{m} is in the interval @samp{[1.0 ..@: 10.0)} except that
  167. @cite{m=e=0} if the original number is zero.  For integers
  168. and fractions, @code{mant} returns the number unchanged and @code{xpon}
  169. returns zero.  The @kbd{v u} (@code{calc-unpack}) command can also be
  170. used to ``unpack'' a floating-point number; this produces an integer
  171. mantissa and exponent, with the constraint that the mantissa is not a
  172. a multiple of ten (again except for the @cite{m=e=0} case).@refill
  173. X
  174. @kindex f S
  175. @pindex calc-scale-float
  176. @tindex scf
  177. The @kbd{f S} (@code{calc-scale-float}) [@code{scf}] function scales a number
  178. by a given power of ten.  Thus, @samp{scf(mant(x), xpon(x)) = x} for any
  179. real @samp{x}.  The second argument must be an integer, but the first
  180. may actually be any numeric value.  For example, @samp{scf(5,-2) = 0.05}
  181. or @samp{1:20} depending on the current Fraction Mode.@refill
  182. X
  183. @kindex f [
  184. @kindex f ]
  185. @pindex calc-decrement
  186. @pindex calc-increment
  187. @tindex decr
  188. @tindex incr
  189. The @kbd{f [} (@code{calc-decrement}) [@code{decr}] and @kbd{f ]}
  190. (@code{calc-increment}) [@code{incr}] functions decrease or increase
  191. a number by one unit.  For integers, the effect is obvious.  For
  192. floating-point numbers, the change is by one unit in the last place.
  193. For example, incrementing @samp{12.3456} when the current precision
  194. is 6 digits yields @samp{12.3457}.  If the current precision had been
  195. 8 digits, the result would have been @samp{12.345601}.  Incrementing
  196. @samp{0.0} produces @c{$10^{-p}$}
  197. @cite{10^-p}, where @cite{p} is the current
  198. precision.  These operations are defined only on integers and floats.
  199. With numeric prefix arguments, they change the number by @cite{n} units.
  200. X
  201. Note that incrementing followed by decrementing, or vice-versa, will
  202. almost but not quite always cancel out.  Suppose the precision is
  203. 6 digits and the number @samp{9.99999} is on the stack.  Incrementing
  204. will produce @samp{10.0000}; decrementing will produce @samp{9.9999}.
  205. One digit has been dropped.  This is an unavoidable consequence of the
  206. way floating-point numbers work.
  207. X
  208. Incrementing a date/time form adjusts it by a certain number of seconds.
  209. Incrementing a pure date form adjusts it by a certain number of days.
  210. X
  211. @node Integer Truncation, Complex Number Functions, Basic Arithmetic, Arithmetic
  212. @section Integer Truncation
  213. X
  214. @noindent
  215. There are four commands for truncating a real number to an integer,
  216. differing mainly in their treatment of negative numbers.  All of these
  217. commands have the property that if the argument is an integer, the result
  218. is the same integer.  An integer-valued floating-point argument is converted
  219. to integer form.
  220. X
  221. If you press @kbd{H} (@code{calc-hyperbolic}) first, the result will be
  222. expressed as an integer-valued floating-point number.
  223. X
  224. @cindex Integer part of a number
  225. @kindex F
  226. @kindex H F
  227. @pindex calc-floor
  228. @tindex floor
  229. @tindex ffloor
  230. The @kbd{F} (@code{calc-floor}) [@code{floor} or @code{ffloor}] command
  231. truncates a real number to the next lower integer, i.e., toward minus
  232. infinity.  Thus @kbd{3.6 F} produces 3, but @kbd{_3.6 F} produces
  233. -4.@refill
  234. X
  235. @kindex I F
  236. @kindex H I F
  237. @pindex calc-ceiling
  238. @tindex ceil
  239. @tindex fceil
  240. The @kbd{I F} (@code{calc-ceiling}) [@code{ceil} or @code{fceil}]
  241. command truncates toward positive infinity.  Thus @kbd{3.6 I F} produces
  242. 4, and @kbd{_3.6 I F} produces @i{-3}.@refill
  243. X
  244. @kindex R
  245. @kindex H R
  246. @pindex calc-round
  247. @tindex round
  248. @tindex fround
  249. The @kbd{R} (@code{calc-round}) [@code{round} or @code{fround}] command
  250. rounds to the nearest integer.  When the fractional part is .5 exactly,
  251. this command rounds away from zero.  (All other rounding in the
  252. Calculator uses this convention as well.)  Thus @kbd{3.5 R} produces 4
  253. but @kbd{3.4 R} produces 3; @kbd{_3.5 R} produces @i{-4}.@refill
  254. X
  255. @kindex I R
  256. @kindex H I R
  257. @pindex calc-trunc
  258. @tindex trunc
  259. @tindex ftrunc
  260. The @kbd{I R} (@code{calc-trunc}) [@code{trunc} or @code{ftrunc}]
  261. command truncates toward zero.  In other words, it ``chops off''
  262. everything after the decimal point.  Thus @kbd{3.6 I R} produces 3 and
  263. @kbd{_3.6 I R} produces @i{-3}.@refill
  264. X
  265. These functions may not be applied meaningfully to error forms, but they
  266. do work for intervals.  As a convenience, applying @code{floor} to a
  267. modulo form floors the value part of the form.  Applied to a vector,
  268. these functions operate on all elements of the vector one by one.
  269. Applied to a date form, they operate on the internal numerical
  270. representation of dates, converting a date/time form into a pure date.
  271. X
  272. @tindex rounde
  273. @tindex roundu
  274. @tindex frounde
  275. @tindex froundu
  276. There are two more rounding functions which can only be entered in
  277. algebraic notation.  The @code{roundu} function is like @code{round}
  278. except that it rounds up, toward plus infinity, when the fractional
  279. part is .5.  This distinction matters only for negative arguments.
  280. Also, @code{rounde} rounds to an even number in the case of a tie,
  281. rounding up or down as necessary.  For example, @samp{rounde(3.5)} and
  282. @samp{rounde(4.5)} both return 4, but @samp{rounde(5.5)} returns 6.
  283. The advantage of round-to-even is that the net error due to rounding
  284. after a long calculation tends to cancel out to zero.  An important
  285. subtle point here is that the number being fed to @code{rounde} will
  286. already have been rounded to the current precision before @code{rounde}
  287. begins.  For example, @samp{rounde(2.500001)} with a current precision
  288. of 6 will incorrectly, or at least surprisingly, yield 2 because the
  289. argument will first have been rounded down to @cite{2.5} (which
  290. @code{rounde} sees as an exact tie between 2 and 3).
  291. X
  292. Each of these functions, when written in algebraic formulas, allows
  293. a second argument which specifies the number of digits after the
  294. decimal point to keep.  For example, @samp{round(123.4567, 2)} will
  295. produce the answer 123.46, and @samp{round(123.4567, -1)} will
  296. produce 120 (i.e., the cutoff is one digit to the @emph{left} of
  297. the decimal point).  A second argument of zero is equivalent to
  298. no second argument at all.
  299. X
  300. @cindex Fractional part of a number
  301. To compute the fractional part of a number (i.e., the amount which, when
  302. added to `@t{floor(}@i{N}@t{)}', will produce @cite{N}) just take @cite{N}
  303. modulo 1 using the @code{%} command.@refill
  304. X
  305. Note also the @kbd{\} (integer quotient), @kbd{f I} (integer logarithm),
  306. and @kbd{f Q} (integer square root) commands, which are analogous to
  307. @kbd{\}, @kbd{B}, and @kbd{Q}, respectively, except that they take integer
  308. arguments and return the result rounded down to an integer.
  309. X
  310. @node Complex Number Functions, Conversions, Integer Truncation, Arithmetic
  311. @section Complex Number Functions
  312. X
  313. @noindent
  314. @kindex J
  315. @pindex calc-conj
  316. @tindex conj
  317. The @kbd{J} (@code{calc-conj}) [@code{conj}] command computes the
  318. complex conjugate of a number.  For complex number @cite{a+bi}, the
  319. complex conjugate is @cite{a-bi}.  If the argument is a real number,
  320. this command leaves it the same.  If the argument is a vector or matrix,
  321. this command replaces each element by its complex conjugate.
  322. X
  323. @kindex G
  324. @pindex calc-argument
  325. @tindex arg
  326. The @kbd{G} (@code{calc-argument}) [@code{arg}] command computes the
  327. ``argument'' or polar angle of a complex number.  For a number in polar
  328. notation, this is simply the second component of the pair
  329. `@t{(}@i{r}@t{;}@c{$\theta$}
  330. @i{theta}@t{)}'.
  331. The result is expressed according to the current angular mode and will
  332. be in the range @i{-180} degrees (exclusive) to @i{+180} degrees
  333. (inclusive), or the equivalent range in radians.@refill
  334. X
  335. @pindex calc-imaginary
  336. The @code{calc-imaginary} command multiplies the number on the
  337. top of the stack by the imaginary number @cite{i = (0,1)}.  This
  338. command is not normally bound to a key in Calc, but it is available
  339. on the @key{IMAG} button in Keypad Mode.
  340. X
  341. @kindex f r
  342. @pindex calc-re
  343. @tindex re
  344. The @kbd{f r} (@code{calc-re}) [@code{re}] command replaces a complex number
  345. by its real part.  This command has no effect on real numbers.  (As an
  346. added convenience, @code{re} applied to a modulo form extracts
  347. the value part.)@refill
  348. X
  349. @kindex f i
  350. @pindex calc-im
  351. @tindex im
  352. The @kbd{f i} (@code{calc-im}) [@code{im}] command replaces a complex number
  353. by its imaginary part; real numbers are converted to zero.  With a vector
  354. or matrix argument, these functions operate element-wise.@refill
  355. X
  356. @kindex v p (complex)
  357. @pindex calc-pack
  358. The @kbd{v p} (@code{calc-pack}) command can pack the top two numbers on
  359. the the stack into a composite object such as a complex number.  With
  360. a prefix argument of @i{-1}, it produces a rectangular complex number;
  361. with an argument of @i{-2}, it produces a polar complex number.
  362. (Also, @pxref{Building Vectors}.)
  363. X
  364. @kindex v u (complex)
  365. @pindex calc-unpack
  366. The @kbd{v u} (@code{calc-unpack}) command takes the complex number
  367. (or other composite object) on the top of the stack and unpacks it
  368. into its separate components.
  369. X
  370. @node Conversions, Date Arithmetic, Complex Number Functions, Arithmetic
  371. @section Conversions
  372. X
  373. @noindent
  374. The commands described in this section are two-key sequences beginning with
  375. the letter @kbd{c}.
  376. X
  377. @kindex c f
  378. @pindex calc-float
  379. @tindex pfloat
  380. The @kbd{c f} (@code{calc-float}) [@code{pfloat}] command converts the
  381. number on the top of the stack to floating-point form.  For example,
  382. @cite{23} is converted to @cite{23.0}, @cite{3:2} is converted to
  383. @cite{1.5}, and @cite{2.3} is left the same.  If the value is a composite
  384. object such as a complex number or vector, each of the components is
  385. converted to floating-point.  If the value is a formula, all numbers
  386. in the formula are converted to floating-point.  Note that depending
  387. on the current floating-point precision, conversion to floating-point
  388. format may lose information.@refill
  389. X
  390. As a special exception, integers which appear as powers or subscripts
  391. are not floated by @code{pfloat}.  If you really want to float a power,
  392. you can use a @kbd{j s} command to select the power, then use @kbd{c f}.
  393. Because @kbd{c f} cannot examine the formula outside of the selection,
  394. it does not notice that the thing being floated is a power.
  395. @xref{Selecting Subformulas}.
  396. X
  397. The normal @kbd{c f} command is ``pervasive'' in the sense that it
  398. applies to all numbers throughout the formula.  The @code{pfloat}
  399. algebraic function never stays around in a formula; @samp{pfloat(a + 1)}
  400. changes to @samp{a + 1.0} as soon as it is evaluated.
  401. X
  402. @kindex H c f
  403. @tindex float
  404. With the Hyperbolic flag, @kbd{H c f} [@code{float}] operates
  405. only on the number or vector of numbers at the top level of its
  406. argument.  Thus, @samp{float(1)} is 1.0, but @samp{float(a + 1)}
  407. is left unevaluated because its argument is not a number.
  408. X
  409. You should use @kbd{H c f} if you wish to guarantee that the final
  410. value, once all the variables have been assigned, is a float; you
  411. would use @kbd{c f} if you wish to do the conversion on the numbers
  412. that appear right now.
  413. X
  414. @kindex c F
  415. @pindex calc-fraction
  416. @tindex pfrac
  417. The @kbd{c F} (@code{calc-fraction}) [@code{pfrac}] command converts a
  418. floating-point number into a fractional approximation.  By default, it
  419. produces a fraction whose decimal representation is the same as the
  420. input number, to within the current precision.  You can also give a
  421. numeric prefix argument to specify a tolerance, either directly, or,
  422. if the prefix argument is zero, by using the number on top of the stack
  423. as the tolerance.  If the tolerance is a positive integer, the fraction
  424. is correct to within that many significant figures.  If the tolerance is
  425. a non-positive integer, it specifies how many digits fewer than the current
  426. precision to use.  If the tolerance is a floating-point number, the
  427. fraction is correct to within that absolute amount.
  428. X
  429. @kindex H c F
  430. @tindex frac
  431. The @code{pfrac} function is pervasive, like @code{pfloat}.
  432. There is also a non-pervasive version, @kbd{H c F} [@code{frac}],
  433. which is analogous to @kbd{H c f} discussed above.
  434. X
  435. @kindex c d
  436. @pindex calc-to-degrees
  437. @tindex deg
  438. The @kbd{c d} (@code{calc-to-degrees}) [@code{deg}] command converts a
  439. number into degrees form.  The value on the top of the stack may be an
  440. HMS form (interpreted as degrees-minutes-seconds), or a real number which
  441. will be interpreted in radians regardless of the current angular mode.@refill
  442. X
  443. @kindex c r
  444. @pindex calc-to-radians
  445. @tindex rad
  446. The @kbd{c r} (@code{calc-to-radians}) [@code{rad}] command converts an
  447. HMS form or angle in degrees into an angle in radians.
  448. X
  449. @kindex c h
  450. @pindex calc-to-hms
  451. @tindex hms
  452. The @kbd{c h} (@code{calc-to-hms}) [@code{hms}] command converts a real
  453. number, interpreted according to the current angular mode, to an HMS
  454. form describing the same angle.  In algebraic notation, the @code{hms}
  455. function also accepts three arguments: @samp{hms(@var{h}, @var{m}, @var{s})}.
  456. (The three-argument version is independent of the current angular mode.)
  457. X
  458. @pindex calc-from-hms
  459. The @code{calc-from-hms} command converts the HMS form on the top of the
  460. stack into a real number according to the current angular mode.
  461. X
  462. @kindex c p
  463. @kindex I c p
  464. @pindex calc-polar
  465. @tindex polar
  466. @tindex rect
  467. The @kbd{c p} (@code{calc-polar}) command converts the complex number on
  468. the top of the stack from polar to rectangular form, or from rectangular
  469. to polar form, whichever is appropriate.  Real numbers are left the same.
  470. This command is equivalent to the @code{rect} or @code{polar}
  471. functions in algebraic formulas, depending on the direction of
  472. conversion.  (It uses @code{polar}, except that if the argument is
  473. already a polar complex number, it uses @code{rect} instead.  The
  474. @kbd{I c p} command always uses @code{rect}.)@refill
  475. X
  476. @kindex c c
  477. @pindex calc-clean
  478. @tindex pclean
  479. The @kbd{c c} (@code{calc-clean}) [@code{pclean}] command ``cleans'' the
  480. number on the top of the stack.  Floating point numbers are re-rounded
  481. according to the current precision.  Polar numbers whose angular
  482. components have strayed from the @i{-180} to @i{+180} degree range
  483. are normalized.  (Note that results will be undesirable if the current
  484. angular mode is different from the one under which the number was
  485. produced!)  Integers and fractions are generally unaffected by this
  486. operation.  Vectors and formulas are cleaned by cleaning each component
  487. number (i.e., pervasively).@refill
  488. X
  489. If the simplification mode is set below the default level, it is raised
  490. to the default level for the purposes of this command.  Thus, @kbd{c c}
  491. applies the default simplifications even if their automatic application
  492. is disabled.  @xref{Simplification Modes}.
  493. X
  494. @cindex Roundoff errors, correcting
  495. A numeric prefix argument to @kbd{c c} sets the floating-point precision
  496. to that value for the duration of the command.  A positive prefix (of at
  497. least 3) sets the precision to the specified value; a negative or zero
  498. prefix decreases the precision by the specified amount.
  499. X
  500. @kindex c 0-9
  501. @pindex calc-clean-num
  502. The keystroke sequences @kbd{c 0} through @kbd{c 9} are equivalent
  503. to @kbd{c c} with the corresponding negative prefix argument.  If roundoff
  504. errors have changed 2.0 into 1.999999, typing @kbd{c 1} to clip off one
  505. decimal place often conveniently does the trick.
  506. X
  507. The @kbd{c c} command with a numeric prefix argument, and the @kbd{c 0}
  508. through @kbd{c 9} commands, also ``clip'' very small floating-point
  509. numbers to zero.  If the exponent is less than or equal to the negative
  510. of the specified precision, the number is changed to 0.0.  For example,
  511. if the current precision is 12, then @kbd{c 2} changes the vector
  512. @samp{[1e-8, 1e-9, 1e-10, 1e-11]} to @samp{[1e-8, 1e-9, 0., 0.]}.
  513. Numbers this small generally arise from roundoff noise.
  514. X
  515. If the numbers you are using really are legitimately this small,
  516. you should avoid using the @kbd{c 0} through @kbd{c 9} commands.
  517. (The plain @kbd{c c} command rounds to the current precision but
  518. does not clip small numbers.)
  519. X
  520. One more property of @kbd{c 0} through @kbd{c 9}, and of @kbd{c c} with
  521. a prefix argument, is that integer-valued floats are converted to
  522. plain integers, so that @kbd{c 1} on @samp{[1., 1.5, 2., 2.5, 3.]}
  523. produces @samp{[1, 1.5, 2, 2.5, 3]}.  This is not done for huge
  524. numbers (@samp{1e100} is techinically an integer-valued float, but
  525. you wouldn't want it automatically converted to a 100-digit integer).
  526. X
  527. @kindex H c 0-9
  528. @kindex H c c
  529. @tindex clean
  530. With the Hyperbolic flag, @kbd{H c c} and @kbd{H c 0} through @kbd{H c 9}
  531. operate non-pervasively [@code{clean}].
  532. X
  533. @node Date Arithmetic, Financial Functions, Conversions, Arithmetic
  534. @section Date Arithmetic
  535. X
  536. @noindent
  537. @cindex Date arithmetic, additional functions
  538. The commands described in this section perform various conversions
  539. and calculations involving date forms (@pxref{Date Forms}).  They
  540. use the @kbd{t} (for time/date) prefix key followed by shifted
  541. letters.
  542. X
  543. The simplest date arithmetic is done using the regular @kbd{+} and @kbd{-}
  544. commands.  In particular, adding a number to a date form advances the
  545. date form by a certain number of days; adding an HMS form to a date
  546. form advances the date by an amount of time; and subtracting two
  547. date forms produces a difference measured in days.  The commands
  548. described here provide additional, more specialized operations on dates.
  549. X
  550. Many of these commands accept a numeric prefix argument; if you give
  551. plain @kbd{C-u} as the prefix, these commands will instead take the
  552. additional argument from the top of the stack.
  553. X
  554. @menu
  555. * Date Conversions::
  556. * Date Functions::
  557. * Time Zones::
  558. @end menu
  559. X
  560. @node Date Conversions, Date Functions, Date Arithmetic, Date Arithmetic
  561. @subsection Date Conversions
  562. X
  563. @noindent
  564. @kindex t D
  565. @pindex calc-date
  566. @tindex date
  567. The @kbd{t D} (@code{calc-date}) [@code{date}] command converts a
  568. date form into a number, measured in days since Jan 1, 1 AD.  The
  569. result will be an integer if @var{date} is a pure date form, or a
  570. fraction or float if @var{date} is a date/time form.  Or, if its
  571. argument is a number, it converts this number into a date form.
  572. X
  573. With a numeric prefix argument, @kbd{t D} takes that many objects
  574. (up to six) from the top of the stack and interprets them in one
  575. of the following ways:
  576. X
  577. The @samp{date(@var{year}, @var{month}, @var{day})} function
  578. builds a pure date form out of the specified year, month, and
  579. day, which must all be integers.  @var{Year} is a year number,
  580. such as 1991 (@emph{not} the same as 91!).  @var{Month} must be
  581. an integer in the range 1 to 12; @var{day} must be in the range
  582. 1 to 31.  If the specified month has fewer than 31 days and
  583. @var{day} is too large, the equivalent day in the following
  584. month will be used.
  585. X
  586. The @samp{date(@var{month}, @var{day})} function builds a
  587. pure date form using the current year, as determined by the
  588. real-time clock.
  589. X
  590. The @samp{date(@var{year}, @var{month}, @var{day}, @var{hms})}
  591. function builds a date/time form using an @var{hms} form.
  592. X
  593. The @samp{date(@var{year}, @var{month}, @var{day}, @var{hour},
  594. @var{minute}, @var{second})} function builds a date/time form.
  595. @var{hour} should be an integer in the range 0 to 23;
  596. @var{minute} should be an integer in the range 0 to 59;
  597. @var{second} should be any real number in the range @samp{[0 .. 60)}.
  598. The last two arguments default to zero if omitted.
  599. X
  600. @kindex t J
  601. @pindex calc-julian
  602. @tindex julian
  603. @cindex Julian day counts, conversions
  604. The @kbd{t J} (@code{calc-julian}) [@code{julian}] command converts
  605. a date form into a Julian day count, which is the number of days
  606. since noon on Jan 1, 4713 BC.  A pure date is converted to an integer
  607. Julian count representing noon of that day.  A date/time form is
  608. converted to an exact floating-point Julian count, adjusted to
  609. interpret the date form in the current time zone but the Julian
  610. day count in Greenwich Mean Time.  A numeric prefix argument allows
  611. you to specify the time zone; @pxref{Time Zones}.  Use a prefix of
  612. zero to suppress the time zone adjustment.  Note that pure date forms
  613. are never time-zone adjusted.
  614. X
  615. This command can also do the opposite conversion, from a Julian day
  616. count (either an integer day, or a floating-point day and time in
  617. the GMT zone), into a pure date form or a date/time form in the
  618. current or specified time zone.
  619. X
  620. @kindex t U
  621. @pindex calc-unix-time
  622. @tindex unixtime
  623. @cindex Unix time format, conversions
  624. The @kbd{t U} (@code{calc-unix-time}) [@code{unixtime}] command
  625. converts a date form into a Unix time value, which is the number of
  626. seconds since midnight on Jan 1, 1970, or vice-versa.  The numeric result
  627. will be an integer if the current precision is 12 or less; for higher
  628. precisions, the result may be a float with (@var{precision} - 12)
  629. digits after the decimal.  Just as for @kbd{t J}, the numeric time
  630. is interpreted in the GMT time zone and the date form is interpreted
  631. in the current or specified zone.  Some systems use Unix-like
  632. numbering but with the local time zone; give a prefix of zero to
  633. suppress the adjustment if so.
  634. X
  635. @kindex t C
  636. @pindex calc-convert-time-zones
  637. @tindex tzconv
  638. @cindex Time Zones, converting between
  639. The @kbd{t C} (@code{calc-convert-time-zones}) [@code{tzconv}]
  640. command converts a date form from one time zone to another.  You
  641. are prompted for each time zone name in turn; you can answer with
  642. any suitable Calc time zone expression (@pxref{Time Zones}).
  643. If you answer either prompt with a blank line, the local time
  644. zone is used for that prompt.  You can also answer the first
  645. prompt with @kbd{$} to take the two time zone names from the
  646. stack (and the date to be converted from the third stack level).
  647. X
  648. @node Date Functions, Time Zones, Date Conversions, Date Arithmetic
  649. @subsection Date Functions
  650. X
  651. @noindent
  652. @kindex t N
  653. @pindex calc-now
  654. @tindex now
  655. The @kbd{t N} (@code{calc-now}) [@code{now}] command pushes the
  656. current date and time on the stack as a date form.  The time is
  657. reported in terms of the specified time zone; with no numeric prefix
  658. argument, @kbd{t N} reports for the current time zone.
  659. X
  660. @kindex t P
  661. @pindex calc-date-part
  662. The @kbd{t P} (@code{calc-date-part}) command extracts one part
  663. of a date form.  The prefix argument specifies the part; with no
  664. argument, this command prompts for a part code from 1 to 9.
  665. The various part codes are described in the following paragraphs.
  666. X
  667. @tindex year
  668. The @kbd{M-1 t P} [@code{year}] function extracts the year number
  669. from a date form as an integer, e.g., 1991.  This and the
  670. following functions will also accept a real number for an
  671. argument, which is interpreted as a standard Calc day number.
  672. Note that this function will never return zero, since the year
  673. 1 BC immediately precedes the year 1 AD.
  674. X
  675. @tindex month
  676. The @kbd{M-2 t P} [@code{month}] function extracts the month number
  677. from a date form as an integer in the range 1 to 12.
  678. X
  679. @tindex day
  680. The @kbd{M-3 t P} [@code{day}] function extracts the day number
  681. from a date form as an integer in the range 1 to 31.
  682. X
  683. @tindex hour
  684. The @kbd{M-4 t P} [@code{hour}] function extracts the hour from
  685. a date form as an integer in the range 0 (midnight) to 23.  Note
  686. that 24-hour time is always used.  This returns zero for a pure
  687. date form.  This function (and the following two) also accept
  688. HMS forms as input.
  689. X
  690. @tindex minute
  691. The @kbd{M-5 t P} [@code{minute}] function extracts the minute
  692. from a date form as an integer in the range 0 to 59.
  693. X
  694. @tindex second
  695. The @kbd{M-6 t P} [@code{second}] function extracts the second
  696. from a date form.  If the current precision is 12 or less,
  697. the result is an integer in the range 0 to 59.  For higher
  698. precisions, the result may instead be a floating-point number.
  699. X
  700. @tindex weekday
  701. The @kbd{M-7 t P} [@code{weekday}] function extracts the weekday
  702. number from a date form as an integer in the range 0 (Sunday)
  703. to 6 (Saturday).
  704. X
  705. @tindex yearday
  706. The @kbd{M-8 t P} [@code{yearday}] function extracts the day-of-year
  707. number from a date form as an integer in the range 1 (January 1)
  708. to 366 (December 31 of a leap year).
  709. X
  710. @tindex time
  711. The @kbd{M-9 t P} [@code{time}] function extracts the time portion
  712. of a date form as an HMS form.  This returns @samp{0@@ 0' 0"}
  713. for a pure date form.
  714. X
  715. @kindex t M
  716. @pindex calc-new-month
  717. @tindex newmonth
  718. The @kbd{t M} (@code{calc-new-month}) [@code{newmonth}] command
  719. computes a new date form that represents the first day of the month
  720. specified by the input date.  The result is always a pure date
  721. form; only the year and month numbers of the input are retained.
  722. With a numeric prefix argument @var{n} in the range from 1 to 31,
  723. @kbd{t M} computes the @var{n}th day of the month.  (If @var{n}
  724. is greater than the actual number of days in the month, or if
  725. @var{n} is zero, the last day of the month is used.)
  726. X
  727. @kindex t Y
  728. @pindex calc-new-year
  729. @tindex newyear
  730. The @kbd{t Y} (@code{calc-new-year}) [@code{newyear}] command
  731. computes a new pure date form that represents the first day of
  732. the year specified by the input.  The month, day, and time
  733. of the input date form are lost.  With a numeric prefix argument
  734. @var{n} in the range from 1 to 366, @kbd{t Y} computes the
  735. @var{n}th day of the year (366 is treated as 365 in non-leap
  736. years).  A prefix argument of 0 computes the last day of the
  737. year (December 31).  A negative prefix argument from @i{-1} to
  738. @i{-12} computes the first day of the @var{n}th month of the year.
  739. X
  740. @kindex t W
  741. @pindex calc-new-week
  742. @tindex newweek
  743. The @kbd{t W} (@code{calc-new-week}) [@code{newweek}] command
  744. computes a new pure date form that represents the Sunday on or before
  745. the input date.  With a numeric prefix argument, it can be made to
  746. use any day of the week as the starting day; the argument must be in
  747. the range from 0 (Sunday) to 6 (Saturday).  This function always
  748. subtracts between 0 and 6 days from the input date.
  749. X
  750. Here's an example use of @code{newweek}:  Find the date of the next
  751. Wednesday after a given date.  Using @kbd{M-3 t W} or @samp{newweek(d, 3)}
  752. will give you the @emph{preceding} Wednesday, so @samp{newweek(d+7, 3)}
  753. will give you the following Wednesday.  A further look at the definition
  754. of @code{newweek} shows that if the input date is itself a Wednesday,
  755. this formula will return the Wednesday one week in the future.  An
  756. exercise for the reader is to modify this formula to yield the same day
  757. if the input is already a Wednesday.  Another interesting exercise is
  758. to preserve the time-of-day portion of the input (@code{newweek} resets
  759. the time to midnight; hint: how can @code{newweek} be defined in terms
  760. of the @code{weekday} function?).
  761. X
  762. @tindex pwday
  763. The @samp{pwday(@var{date})} function (not on any key) computes the
  764. day-of-month number of the Sunday on or before @var{date}.  With
  765. two arguments, @samp{pwday(@var{date}, @var{day})} computes the day
  766. number of the Sunday on or before day number @var{day} of the month
  767. specified by @var{date}.  The @var{day} must be in the range from
  768. 7 to 31; if the day number is greater than the actual number of days
  769. in the month, the true number of days is used instead.  Thus
  770. @samp{pwday(@var{date}, 7)} finds the first Sunday of the month, and
  771. @samp{pwday(@var{date}, 31)} finds the last Sunday of the month.
  772. With a third @var{weekday} argument, @code{pwday} can be made to look
  773. for any day of the week instead of Sunday.
  774. X
  775. @kindex t I
  776. @pindex calc-inc-month
  777. @tindex incmonth
  778. The @kbd{t I} (@code{calc-inc-month}) [@code{incmonth}] command
  779. increases a date form by one month, or by an arbitrary number of
  780. months specified by a numeric prefix argument.  The time portion,
  781. if any, of the date form stays the same.  The day also stays the
  782. same, except that if the new month has fewer days the day
  783. number may be reduced to lie in the valid range.  For example,
  784. @samp{incmonth(<Jan 31, 1991>)} produces @samp{<Feb 28, 1991>}.
  785. Because of this, @kbd{t I t I} and @kbd{M-2 t I} do not always give
  786. the same results (@samp{<Mar 28, 1991>} versus @samp{<Mar 31, 1991>}
  787. in this case).
  788. X
  789. @tindex incyear
  790. The @samp{incyear(@var{date}, @var{step})} function increases
  791. a date form by the specified number of years, which may be
  792. any positive or negative integer.  Note that @samp{incyear(d, n)}
  793. is equivalent to @samp{incmonth(d, 12*n)}, but these do not have
  794. simple equivalents in terms of day arithmetic because
  795. months and years have varying lengths.  If the @var{step}
  796. argument is omitted, 1 year is assumed.  There is no keyboard
  797. command for this function; use @kbd{C-u 12 t I} instead.
  798. X
  799. There is no @code{newday} function at all because @kbd{F} [@code{floor}]
  800. serves this purpose.  Similarly, instead of @code{incday} and
  801. @code{incweek} simply use @cite{d + n} or @cite{d + 7 n}.
  802. X
  803. @xref{Basic Arithmetic}, for the @kbd{f ]} [@code{incr}] command
  804. which can adjust a date/time form by a certain number of seconds.
  805. X
  806. @node Time Zones, , Date Functions, Date Arithmetic
  807. @subsection Time Zones
  808. X
  809. @noindent
  810. @cindex Time zones
  811. @cindex Daylight savings time
  812. Time zones and daylight savings time are a complicated business.
  813. The conversions to and from Julian and Unix-style dates automatically
  814. compute the correct time zone and daylight savings adjustment to use,
  815. provided they can figure out this information.  This section describes
  816. Calc's time zone adjustment algorithm in detail, in case you want to
  817. do conversions in different time zones or in case Calc's algorithms
  818. can't determine the right correction to use.
  819. X
  820. Adjustments for time zones and daylight savings time are done by
  821. @kbd{t U}, @kbd{t J}, @kbd{t N}, and @kbd{t C}, but not by any other
  822. commands.  In particular, @samp{<may 1 1991> - <apr 1 1991>} evaluates
  823. to exactly 30 days even though there is a daylight-savings
  824. transition in between.  This is also true for Julian pure dates:
  825. @samp{julian(<may 1 1991>) - julian(<apr 1 1991>)}.  But Julian
  826. and Unix date/times will adjust for daylight savings time:
  827. @samp{julian(<12am may 1 1991>) - julian(<12am apr 1 1991>)}
  828. evaluates to @samp{29.95834} (that's 29 days and 23 hours)
  829. because one hour was lost when daylight savings commenced on
  830. April 7, 1991.
  831. X
  832. In brief, the idiom @samp{julian(@var{date1}) - julian(@var{date2})}
  833. computes the actual number of 24-hour periods between two dates, whereas
  834. @samp{@var{date1} - @var{date2}} computes the number of calendar
  835. days between two dates without taking daylight savings into account.
  836. X
  837. @pindex calc-time-zone
  838. @tindex tzone
  839. The @code{calc-time-zone} [@code{tzone}] command converts the time
  840. zone specified by its numeric prefix argument into a number of
  841. seconds difference from Greenwich mean time (GMT).  If the argument
  842. is a number, the result is simply that value multiplied by 3600.
  843. Typical arguments for North America are 5 (Eastern) or 8 (Pacific).  If
  844. Daylight Savings time is in effect, one hour should be subtracted from
  845. the normal difference.
  846. X
  847. If you give a prefix of plain @kbd{C-u}, @code{calc-time-zone} (like other
  848. date arithmetic commands that include a time zone argument) takes the
  849. zone argument from the top of the stack.  (In the case of @kbd{t J}
  850. and @kbd{t U}, the normal argument is then taken from the second-to-top
  851. stack position.)  This allows you to give a non-integer time zone
  852. adjustment.  The time-zone argument can also be an HMS form, or
  853. it can be a variable which is a time zone name in upper- or lower-case.
  854. For example @samp{tzone(PST) = tzone(8)} and @samp{tzone(pdt) = tzone(7)}
  855. (for Pacific standard and daylight savings times, respectively).
  856. X
  857. North American and European time zone names are defined as follows;
  858. note that for each time zone there is one name for standard time,
  859. another for daylight savings time, and a third for ``generalized'' time
  860. in which the daylight savings adjustment is computed from context.
  861. X
  862. @group
  863. @smallexample
  864. YST  PST  MST  CST  EST  AST    NST    GMT   WET     MET    MEZ
  865. X 9    8    7    6    5    4     3.5     0     -1      -2     -2
  866. X
  867. YDT  PDT  MDT  CDT  EDT  ADT    NDT    BST  WETDST  METDST  MESZ
  868. X 8    7    6    5    4    3     2.5     -1    -2      -3     -3
  869. X
  870. YGT  PGT  MGT  CGT  EGT  AGT    NGT    BGT   WEGT    MEGT   MEGZ
  871. 9/8  8/7  7/6  6/5  5/4  4/3  3.5/2.5  0/-1 -1/-2   -2/-3  -2/-3
  872. @end smallexample
  873. @end group
  874. X
  875. @vindex math-tzone-names
  876. To define time zone names that do not appear in the above table,
  877. you must modify the Lisp variable @code{math-tzone-names}.  This
  878. is a list of lists describing the different time zone names; its
  879. structure is best explained by an example.  The three entries for
  880. Pacific Time look like this:
  881. X
  882. @group
  883. @smallexample
  884. ( ( "PST" 8 0 )    ; Name as an upper-case string, then standard
  885. X  ( "PDT" 8 -1 )   ; adjustment, then daylight savings adjustment.
  886. X  ( "PGT" 8 "PST" "PDT" ) )   ; Generalized time zone.
  887. @end smallexample
  888. @end group
  889. X
  890. @cindex @code{TimeZone} variable.
  891. @vindex TimeZone
  892. With no arguments, @code{calc-time-zone} or @samp{tzone()} obtains an
  893. argument from the Calc variable @code{TimeZone} if a value has been
  894. stored for that variable.  If not, Calc runs the Unix @samp{date}
  895. command and looks for one of the above time zone names in the output;
  896. if this does not succeed, @samp{tzone()} leaves itself unevaluated.
  897. The time zone name in the @samp{date} output may be followed by a signed
  898. adjustment, e.g., @samp{GMT+5} or @samp{GMT+0500} which specifies a
  899. number of hours and minutes to be added to the base time zone.
  900. Calc stores the time zone it finds into @code{TimeZone} to speed
  901. later calls to @samp{tzone()}.
  902. X
  903. The special time zone name @code{local} is equivalent to no argument,
  904. i.e., it uses the local time zone as obtained from the @code{date}
  905. command.
  906. X
  907. If the time zone name found is one of the standard or daylight
  908. savings zone names from the above table, and Calc's internal
  909. daylight savings algorithm says that time and zone are consistent
  910. (e.g., @code{PDT} accompanies a date that Calc's algorithm would also
  911. consider to be daylight savings, or @code{PST} accompanies a date
  912. that Calc would consider to be standard time), then Calc substitutes
  913. the corresponding generalized time zone (like @code{PGT}).
  914. X
  915. If your system does not have a suitable @samp{date} command, you
  916. may wish to put a @samp{(setq var-TimeZone ...)} in your Emacs
  917. initialization file to set the time zone.  The easiest way to do
  918. this is to edit the @code{TimeZone} variable using Calc's @kbd{s T}
  919. command, then use the @kbd{s p} (@code{calc-permanent-variable})
  920. command to save the value of @code{TimeZone} permanently.
  921. X
  922. The @kbd{t J} and @code{t U} commands with no numeric prefix
  923. arguments do the same thing as @samp{tzone()}.  If the current
  924. time zone is a generalized time zone, e.g., @code{EGT}, Calc
  925. examines the date being converted to tell whether to use standard
  926. or daylight savings time.  But if the current time zone is explicit,
  927. e.g., @code{EST} or @code{EDT}, then that adjustment is used exactly
  928. and Calc's daylight savings algorithm is not consulted.
  929. X
  930. Some places don't follow the usual rules for daylight savings time.
  931. The state of Arizona, for example, does not observe daylight savings
  932. time.  If you run Calc during the winter season in Arizona, the
  933. Unix @code{date} command will report @code{MST} time zone, which
  934. Calc will change to @code{MGT}.  If you then convert a time that
  935. lies in the summer months, Calc will apply an incorrect daylight
  936. savings time adjustment.  To avoid this, set your @code{TimeZone}
  937. variable explicitly to @code{MST} to force the use of standard,
  938. non-daylight-savings time.
  939. X
  940. @vindex math-daylight-savings-hook
  941. @findex math-std-daylight-savings
  942. By default Calc always considers daylight savings time to begin at
  943. 2 a.m.@: on the first Sunday of April, and to end at 2 a.m.@: on the
  944. last Sunday of October.  This is the rule that has been in effect
  945. in North America since 1987.  If you are in a country that uses
  946. different rules for computing daylight savings time, you have two
  947. choices:  Write your own daylight savings hook, or control time
  948. zones explicitly by setting the @code{TimeZone} variable and/or
  949. always giving a time-zone argument for the conversion functions.
  950. X
  951. The Lisp variable @code{math-daylight-savings-hook} holds the
  952. name of a function that is used to compute the daylight savings
  953. adjustment for a given date.  The default is
  954. @code{math-std-daylight-savings}, which computes an adjustment
  955. (either 0 or @i{-1}) using the North American rules given above.
  956. X
  957. The daylight savings hook function is called with four arguments:
  958. The date, as a floating-point number in standard Calc format;
  959. a six-element list of the date decomposed into year, month, day,
  960. hour, minute, and second, respectively; a string which contains
  961. the generalized time zone name in upper-case, e.g., @code{"WEGT"};
  962. and a special adjustment to be applied to the hour value when
  963. converting into a generalized time zone (see below).
  964. X
  965. @findex math-prev-weekday-in-month
  966. The Lisp function @code{math-prev-weekday-in-month} is useful for
  967. daylight savings computations.  This is an internal version of
  968. the user-level @code{pwday} function described in the previous
  969. section. It takes four arguments:  The floating-point date value,
  970. the corresponding six-element date list, the day-of-month number,
  971. and the weekday number (0-6).
  972. X
  973. The default daylight savings hook ignores the time zone name, but a
  974. more sophisticated hook could use different algorithms for different
  975. time zones.  It would also be possible to use different algorithms
  976. depending on the year number, but the default hook always uses the
  977. algorithm for 1987 and later.  Here is a listing of the default
  978. daylight savings hook:
  979. X
  980. @smallexample
  981. (defun math-std-daylight-savings (date dt zone bump)
  982. X  (cond ((< (nth 1 dt) 4) 0)
  983. X        ((= (nth 1 dt) 4)
  984. X         (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
  985. X           (cond ((< (nth 2 dt) sunday) 0)
  986. X                 ((= (nth 2 dt) sunday)
  987. X                  (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
  988. X                 (t -1))))
  989. X        ((< (nth 1 dt) 10) -1)
  990. X        ((= (nth 1 dt) 10)
  991. X         (let ((sunday (math-prev-weekday-in-month date dt 31 0)))
  992. X           (cond ((< (nth 2 dt) sunday) -1)
  993. X                 ((= (nth 2 dt) sunday)
  994. X                  (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
  995. X                 (t 0))))
  996. X        (t 0))
  997. )
  998. @end smallexample
  999. X
  1000. @noindent
  1001. The @code{bump} parameter is equal to zero when Calc is converting
  1002. from a date form in a generalized time zone into a GMT date value.
  1003. It is @i{-1} when Calc is converting in the other direction.  The
  1004. adjustments shown above ensure that the conversion behaves correctly
  1005. and reasonably around the 2 a.m.@: transition in each direction.
  1006. X
  1007. There is a ``missing'' hour between 2 a.m.@: and 3 a.m.@: at the
  1008. beginning of daylight savings time; converting a date/time form that
  1009. falls in this hour results in a time value for the following hour,
  1010. from 3 a.m.@: to 4 a.m.  At the end of daylight savings time, the
  1011. hour from 1 a.m.@: to 2 a.m.@: repeats itself; converting a date/time
  1012. form that falls in in this hour results in a time value for the first
  1013. manifestion of that time (@emph{not} the one that occurs one hour later).
  1014. X
  1015. If @code{math-daylight-savings-hook} is @code{nil}, then the
  1016. daylight savings adjustment is always taken to be zero.
  1017. X
  1018. In algebraic formulas, @samp{tzone(@var{zone}, @var{date})}
  1019. computes the time zone adjustment for a given zone name at a
  1020. given date.  The @var{date} is ignored unless @var{zone} is a
  1021. generalized time zone.  If @var{date} is a date form, the
  1022. daylight savings computation is applied to it as it appears.
  1023. If @var{date} is a numeric date value, it is adjusted for the
  1024. daylight-savings version of @var{zone} before being given to
  1025. the daylight savings hook.  This odd-sounding rule ensures
  1026. that the daylight-savings computation is always done in
  1027. local time, not in the GMT time that a numeric @var{date}
  1028. is typically represented in.
  1029. X
  1030. @tindex dsadj
  1031. The @samp{dsadj(@var{date}, @var{zone})} function computes the
  1032. daylight savings adjustment that is appropriate for @var{date} in
  1033. time zone @var{zone}.  If @var{zone} is explicitly in or not in
  1034. daylight savings time (e.g., @code{PDT} or @code{PST}) the
  1035. @var{date} is ignored.  If @var{zone} is a generalized time zone,
  1036. the algorithms described above are used.  If @var{zone} is omitted,
  1037. the computation is done for the current time zone.
  1038. X
  1039. @xref{Reporting Bugs}, for the address of Calc's author, if you
  1040. should wish to contribute your improved versions of
  1041. @code{math-tzone-names} and @code{math-daylight-savings-hook}
  1042. to the Calc distribution.
  1043. X
  1044. @node Financial Functions, Binary Functions, Date Arithmetic, Arithmetic
  1045. @section Financial Functions
  1046. X
  1047. @noindent
  1048. Calc's financial or business functions use the @kbd{b} prefix
  1049. key followed by a shifted letter.  (The @kbd{b} prefix followed by
  1050. a lower-case letter is used for operations on binary numbers.)
  1051. X
  1052. @tindex %
  1053. @tindex percent
  1054. Since many of these functions have @var{rate} arguments which would
  1055. normally be written as percentages, Calc accepts the notation
  1056. @samp{@var{x}%} during algebraic entry.  This is equivalent to
  1057. @samp{@var{x} * .01}.  In other words, the @var{rate} argument
  1058. to these functions wants something like @samp{8%} or @samp{.08}
  1059. as a value, @emph{not} 8 (which would correspond to a rate of
  1060. 800%!).
  1061. X
  1062. Also note that the rate and the number of intervals must be on the
  1063. same time scale, e.g., months or years.  Mixing an annual interest
  1064. rate with a time expressed in months will give you very wrong
  1065. answers!
  1066. X
  1067. Finally, it is wise to compute these functions to a higher precision
  1068. than you really need, just to make sure your answer is correct to
  1069. the last penny; also, you may wish to check the definitions at the end
  1070. of this section to make sure the functions have the meaning you expect.
  1071. X
  1072. @menu
  1073. * Future Value::
  1074. * Present Value::
  1075. * Related Financial Functions::
  1076. * Depreciation Functions::
  1077. * Definitions of Financial Functions::
  1078. @end menu
  1079. X
  1080. @node Future Value, Present Value, Financial Functions, Financial Functions
  1081. @subsection Future Value
  1082. X
  1083. @noindent
  1084. @kindex b F
  1085. @pindex calc-fin-fv
  1086. @tindex fv
  1087. The @kbd{b F} (@code{calc-fin-fv}) [@code{fv}] command computes
  1088. the future value of an investment.  It takes three arguments
  1089. from the stack:  @samp{fv(@var{rate}, @var{n}, @var{payment})}.
  1090. If you give payments of @var{payment} every year for @var{n}
  1091. years, and the money you have paid earns interest at @var{rate} per
  1092. year, then this function tells you what your investment would be
  1093. worth at the end of the period.  (The actual interval doesn't
  1094. have to be years, as long as @var{n} and @var{rate} are expressed
  1095. in terms of the same intervals.)  This function assumes payments
  1096. occur at the @emph{end} of each interval.
  1097. X
  1098. @kindex I b F
  1099. @tindex fvb
  1100. The @kbd{I b F} [@code{fvb}] command does the same computation,
  1101. but assuming your payments are at the beginning of each interval.
  1102. Suppose you plan to deposit $1000 per year in a savings account
  1103. earning 5.4% interest, starting right now.  How much will be
  1104. in the account after five years?  @code{fvb(5.4%, 5, 1000) = 5870.73}.
  1105. Thus you will have earned $870 worth of interest over the years.
  1106. Using the stack, this calculation would have been
  1107. @kbd{.054 RET 5 RET 1000 I b F}.  Note that the rate is expressed
  1108. as a number between 0 and 1, @emph{not} as a percentage.
  1109. X
  1110. @kindex H b F
  1111. @tindex fvl
  1112. The @kbd{H b F} [@code{fvl}] command computes the future value
  1113. of an initial lump sum investment.  Suppose you could deposit
  1114. those five thousand dollars in the bank right now; how much would
  1115. they be worth in five years?  @code{fvl(5.4%, 5, 5000) = 6503.89}.
  1116. X
  1117. The algebraic functions @code{fv} and @code{fvb} accept an optional
  1118. fourth argument, which is used as an initial lump sum in the sense
  1119. of @code{fvl}.  In other words, @code{fv(@var{rate}, @var{n},
  1120. @var{payment}, @var{initial}) = fv(@var{rate}, @var{n}, @var{payment})
  1121. + fvl(@var{rate}, @var{n}, @var{initial})}.@refill
  1122. X
  1123. To illustrate the relationships between these functions, we could
  1124. do the @code{fvb} calculation ``by hand'' using @code{fvl}.  The
  1125. final balance will be the sum of the contributions of our five
  1126. deposits at various times.  The first deposit earns interest for
  1127. five years:  @code{fvl(5.4%, 5, 1000) = 1300.78}.  The second
  1128. deposit only earns interest for four years:  @code{fvl(5.4%, 4, 1000) =
  1129. 1234.13}.  And so on down to the last deposit, which earns one
  1130. year's interest:  @code{fvl(5.4%, 1, 1000) = 1054.00}.  The sum of
  1131. these five values is, sure enough, $5870.73, just as was computed
  1132. by @code{fvb} directly.
  1133. X
  1134. What does @code{fv(5.4%, 5, 1000) = 5569.96} mean?  The payments
  1135. are now at the ends of the periods.  The end of one year is the same
  1136. as the beginning of the next, so what this really means is that we've
  1137. lost the payment at year zero (which contributed $1300.78), but we're
  1138. now counting the payment at year five (which, since it didn't have
  1139. a chance to earn interest, counts as $1000).  Indeed, @cite{5569.96 =
  1140. 5870.73 - 1300.78 + 1000} (give or take a bit of roundoff error).
  1141. X
  1142. @node Present Value, Related Financial Functions, Future Value, Financial Functions
  1143. @subsection Present Value
  1144. X
  1145. @noindent
  1146. @kindex b P
  1147. @pindex calc-fin-pv
  1148. @tindex pv
  1149. The @kbd{b P} (@code{calc-fin-pv}) [@code{pv}] command computes
  1150. the present value of an investment.  Like @code{fv}, it takes
  1151. three arguments:  @code{pv(@var{rate}, @var{n}, @var{payment})}.
  1152. It computes the present value of a series of regular payments.
  1153. Suppose you have the chance to make an investment that will
  1154. pay $2000 per year over the next four years; as you receive
  1155. these payments you can put them in the bank at 9% interest.
  1156. You want to know whether it is better to make the investment, or
  1157. to keep the money in the bank where it earns 9% interest right
  1158. from the start.  The calculation @code{pv(9%, 4, 2000)} gives the
  1159. result 6479.44.  If your initial investment must be less than this,
  1160. say, $6000, then the investment is worthwhile.  But if you had to
  1161. put up $7000, then it would be better just to leave it in the bank.
  1162. X
  1163. Here is the interpretation of the result of @code{pv}:  You are
  1164. trying to compare the return from the investment you are
  1165. considering, which is @code{fv(9%, 4, 2000) = 9146.26}, with
  1166. the return from leaving the money in the bank, which is
  1167. @code{fvl(9%, 4, @var{x})} where @var{x} is the amount of money
  1168. you would have to put up in advance.  The @code{pv} function
  1169. finds the break-even point, @cite{x = 6479.44}, at which
  1170. @code{fvl(9%, 4, 6479.44)} is also equal to 9146.26.  This is
  1171. the largest amount you should be willing to invest.
  1172. X
  1173. @kindex I b P
  1174. @tindex pvb
  1175. The @kbd{I b P} [@code{pvb}] command solves the same problem,
  1176. but with payments occurring at the beginning of each interval.
  1177. It has the same relationship to @code{fvb} as @code{pv} has
  1178. to @code{fv}.  For example @code{pvb(9%, 4, 2000) = 7062.59},
  1179. a larger number than @code{pv} produced because we get to start
  1180. earning interest on the return from our investment sooner.
  1181. X
  1182. @kindex H b P
  1183. @tindex pvl
  1184. The @kbd{H b P} [@code{pvl}] command computes the present value of
  1185. an investment that will pay off in one lump sum at the end of the
  1186. period.  For example, if we get our $8000 all at the end of the
  1187. four years, @code{pvl(9%, 4, 8000) = 5667.40}.  This is much
  1188. less than @code{pv} reported, because we don't earn any interest
  1189. on the return from this investment.  Note that @code{pvl} and
  1190. @code{fvl} are simple inverses:  @code{fvl(9%, 4, 5667.40) = 8000}.
  1191. X
  1192. You can give an optional fourth lump-sum argument to @code{pv}
  1193. and @code{pvb}; this is handled in exactly the same way as the
  1194. fourth argument for @code{fv} and @code{fvb}.
  1195. X
  1196. @kindex b N
  1197. @pindex calc-fin-npv
  1198. @tindex npv
  1199. The @kbd{b N} (@code{calc-fin-npv}) [@code{npv}] command computes
  1200. the net present value of a series of irregular investments.
  1201. The first argument is the interest rate.  The second argument is
  1202. a vector which represents the expected return from the investment
  1203. at the end of each interval.  For example, if the rate represents
  1204. a yearly interest rate, then the vector elements are the return
  1205. from the first year, second year, and so on.
  1206. X
  1207. Thus, @code{npv(9%, [2000,2000,2000,2000]) = pv(9%, 4, 2000) = 6479.44}.
  1208. Obviously this function is more interesting when the payments are
  1209. not all the same!
  1210. X
  1211. The @code{npv} function can actually have two or more arguments.
  1212. Multiple arguments are interpreted in the same way as for the
  1213. vector statistical functions like @code{vsum}.
  1214. @xref{Single-Variable Statistics}.  Basically, if there are several
  1215. payment arguments, each either a vector or a plain number, all these
  1216. values are collected left-to-right into the complete list of payments.
  1217. A numeric prefix argument on the @kbd{b N} command says how many
  1218. payment values or vectors to take from the stack.@refill
  1219. X
  1220. @kindex I b N
  1221. @tindex npvb
  1222. The @kbd{I b N} [@code{npvb}] command computes the net present
  1223. value where payments occur at the beginning of each interval
  1224. rather than at the end.
  1225. X
  1226. @node Related Financial Functions, Depreciation Functions, Present Value, Financial Functions
  1227. @subsection Related Financial Functions
  1228. X
  1229. @noindent
  1230. The functions in this section are basically inverses of the
  1231. present value functions with respect to the various arguments.
  1232. X
  1233. @kindex b M
  1234. @pindex calc-fin-pmt
  1235. @tindex pmt
  1236. The @kbd{b M} (@code{calc-fin-pmt}) [@code{pmt}] command computes
  1237. the amount of periodic payment necessary to amortize a loan.
  1238. Thus @code{pmt(@var{rate}, @var{n}, @var{amount})} equals the
  1239. value of @var{payment} such that @code{pv(@var{rate}, @var{n},
  1240. @var{payment}) = @var{amount}}.@refill
  1241. X
  1242. @kindex I b M
  1243. @tindex pmtb
  1244. The @kbd{I b M} [@code{pmtb}] command does the same computation
  1245. but using @code{pvb} instead of @code{pv}.  Like @code{pv} and
  1246. @code{pvb}, these functions can also take a fourth argument which
  1247. represents an initial lump-sum investment.
  1248. X
  1249. @kindex H b M
  1250. The @kbd{H b M} key just invokes the @code{fvl} function, which is
  1251. the inverse of @code{pvl}.  There is no explicit @code{pmtl} function.
  1252. X
  1253. @kindex b #
  1254. @pindex calc-fin-nper
  1255. @tindex nper
  1256. The @kbd{b #} (@code{calc-fin-nper}) [@code{nper}] command computes
  1257. the number of regular payments necessary to amortize a loan.
  1258. Thus @code{nper(@var{rate}, @var{payment}, @var{amount})} equals
  1259. the value of @var{n} such that @code{pv(@var{rate}, @var{n},
  1260. @var{payment}) = @var{amount}}.  If @var{payment} is too small
  1261. ever to amortize a loan for @var{amount} at interest rate @var{rate},
  1262. the @code{nper} function is left in symbolic form.@refill
  1263. X
  1264. @kindex I b #
  1265. @tindex nperb
  1266. The @kbd{I b #} [@code{nperb}] command does the same computation
  1267. but using @code{pvb} instead of @code{pv}.  You can give a fourth
  1268. SHAR_EOF
  1269. true || echo 'restore of calc.texinfo failed'
  1270. fi
  1271. echo 'End of  part 41'
  1272. echo 'File calc.texinfo is continued in part 42'
  1273. echo 42 > _shar_seq_.tmp
  1274. exit 0
  1275. exit 0 # Just in case...
  1276. -- 
  1277. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1278. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1279. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1280. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1281.