]>
git.zerfleddert.de Git - micropolis/blob - src/tclx/src/tclxmath.c
9f967bf4f2402c7f61f0f2b8f03ef835c7ceb70d
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 *-----------------------------------------------------------------------------
24 * Prototypes of internal functions.
27 really_random
_ANSI_ARGS_((int my_range
));
31 *-----------------------------------------------------------------------------
34 * Implements the TCL max command:
35 * max num1 num2 [..numN]
38 * Standard TCL results.
40 *-----------------------------------------------------------------------------
43 Tcl_MaxCmd (clientData
, interp
, argc
, argv
)
44 ClientData clientData
;
49 double value
, maxValue
= -MAXDOUBLE
;
54 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
55 " num1 num2 [..numN]", (char *) NULL
);
59 for (idx
= 1; idx
< argc
; idx
++) {
60 if (Tcl_GetDouble (interp
, argv
[idx
], &value
) != TCL_OK
)
62 if (value
> maxValue
) {
67 strcpy (interp
->result
, argv
[maxIdx
]);
72 *-----------------------------------------------------------------------------
75 * Implements the TCL min command:
76 * min num1 num2 [..numN]
79 * Standard TCL results.
81 *-----------------------------------------------------------------------------
84 Tcl_MinCmd (clientData
, interp
, argc
, argv
)
85 ClientData clientData
;
90 double value
, minValue
= MAXDOUBLE
;
94 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
95 " num1 num2 [..numN]", (char *) NULL
);
99 for (idx
= 1; idx
< argc
; idx
++) {
100 if (Tcl_GetDouble (interp
, argv
[idx
], &value
) != TCL_OK
)
102 if (value
< minValue
) {
107 strcpy (interp
->result
, argv
[minIdx
]);
112 *-----------------------------------------------------------------------------
115 * Insure a good random return for a range, unlike an arbitrary
116 * random() % n, thanks to Ken Arnold, Unix Review, October 1987.
118 *-----------------------------------------------------------------------------
120 #ifdef TCL_32_BIT_RANDOM
121 # define RANDOM_RANGE 0x7FFFFFFF
123 # define RANDOM_RANGE 0x7FFF
128 ReallyRandom (myRange
)
131 int maxMultiple
, rnum
;
140 while ((rnum
= rand()) >= maxMultiple
) {
144 return (rnum
% myRange
);
148 *-----------------------------------------------------------------------------
151 * Implements the TCL random command:
155 * Standard TCL results.
157 *-----------------------------------------------------------------------------
160 Tcl_RandomCmd (clientData
, interp
, argc
, argv
)
161 ClientData clientData
;
168 if ((argc
< 2) || (argc
> 3))
171 if (STREQU (argv
[1], "seed")) {
175 if (Tcl_GetLong (interp
, argv
[2], &seed
) != TCL_OK
)
178 seed
= (unsigned) (getpid() + time((time_t *)NULL
));
185 if (Tcl_GetUnsigned (interp
, argv
[1], &range
) != TCL_OK
)
187 if ((range
== 0) || (range
> (int)RANDOM_RANGE
))
190 sprintf (interp
->result
, "%d", ReallyRandom (range
));
195 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
196 " limit | seed [seedval]", (char *) NULL
);
202 sprintf (buf
, "%d", (int)RANDOM_RANGE
);
203 Tcl_AppendResult (interp
, "range must be > 0 and <= ",