]>
cvs.zerfleddert.de Git - micropolis/blob - src/tclx/src/tclxmath.c
bf5a52a76f3eca59260299096436aebaf8ba2818
4 * Mathematical Tcl commands.
5 *-----------------------------------------------------------------------------
6 * Copyright 1992 Karl Lehenbauer and Mark Diekhans.
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted, provided
10 * that the above copyright notice appear in all copies. Karl Lehenbauer and
11 * Mark Diekhans make no representations about the suitability of this
12 * software for any purpose. It is provided "as is" without express or
14 *-----------------------------------------------------------------------------
15 * $Id: tclXmath.c,v 2.0 1992/10/16 04:50:59 markd Rel $
16 *-----------------------------------------------------------------------------
25 * Prototypes of internal functions.
28 really_random
_ANSI_ARGS_((int my_range
));
32 *-----------------------------------------------------------------------------
35 * Implements the TCL max command:
36 * max num1 num2 [..numN]
39 * Standard TCL results.
41 *-----------------------------------------------------------------------------
44 Tcl_MaxCmd (clientData
, interp
, argc
, argv
)
45 ClientData clientData
;
50 double value
, maxValue
= -MAXDOUBLE
;
55 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
56 " num1 num2 [..numN]", (char *) NULL
);
60 for (idx
= 1; idx
< argc
; idx
++) {
61 if (Tcl_GetDouble (interp
, argv
[idx
], &value
) != TCL_OK
)
63 if (value
> maxValue
) {
68 strcpy (interp
->result
, argv
[maxIdx
]);
73 *-----------------------------------------------------------------------------
76 * Implements the TCL min command:
77 * min num1 num2 [..numN]
80 * Standard TCL results.
82 *-----------------------------------------------------------------------------
85 Tcl_MinCmd (clientData
, interp
, argc
, argv
)
86 ClientData clientData
;
91 double value
, minValue
= MAXDOUBLE
;
95 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
96 " num1 num2 [..numN]", (char *) NULL
);
100 for (idx
= 1; idx
< argc
; idx
++) {
101 if (Tcl_GetDouble (interp
, argv
[idx
], &value
) != TCL_OK
)
103 if (value
< minValue
) {
108 strcpy (interp
->result
, argv
[minIdx
]);
113 *-----------------------------------------------------------------------------
116 * Insure a good random return for a range, unlike an arbitrary
117 * random() % n, thanks to Ken Arnold, Unix Review, October 1987.
119 *-----------------------------------------------------------------------------
121 #ifdef TCL_32_BIT_RANDOM
122 # define RANDOM_RANGE 0x7FFFFFFF
124 # define RANDOM_RANGE 0x7FFF
129 ReallyRandom (myRange
)
132 int maxMultiple
, rnum
;
141 while ((rnum
= rand()) >= maxMultiple
) {
145 return (rnum
% myRange
);
149 *-----------------------------------------------------------------------------
152 * Implements the TCL random command:
156 * Standard TCL results.
158 *-----------------------------------------------------------------------------
161 Tcl_RandomCmd (clientData
, interp
, argc
, argv
)
162 ClientData clientData
;
169 if ((argc
< 2) || (argc
> 3))
172 if (STREQU (argv
[1], "seed")) {
176 if (Tcl_GetLong (interp
, argv
[2], &seed
) != TCL_OK
)
179 seed
= (unsigned) (getpid() + time((time_t *)NULL
));
186 if (Tcl_GetUnsigned (interp
, argv
[1], &range
) != TCL_OK
)
188 if ((range
== 0) || (range
> (int)RANDOM_RANGE
))
191 sprintf (interp
->result
, "%d", ReallyRandom (range
));
196 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
197 " limit | seed [seedval]", (char *) NULL
);
203 sprintf (buf
, "%d", (int)RANDOM_RANGE
);
204 Tcl_AppendResult (interp
, "range must be > 0 and <= ",