01: /*
02: * @(#)abs.java 1.2 04/12/06
03: *
04: * Copyright (c) 2001-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.math;
10:
11: import pnuts.lang.PnutsFunction;
12: import pnuts.lang.Context;
13: import pnuts.lang.Runtime;
14:
15: public class abs extends PnutsFunction {
16: final static Integer ZERO = new Integer(0);
17:
18: public abs() {
19: super ("abs");
20: }
21:
22: public boolean defined(int nargs) {
23: return nargs == 1;
24: }
25:
26: protected Object exec(Object[] args, Context context) {
27: int nargs = args.length;
28: if (nargs == 1) {
29: Object arg = args[0];
30: if (Runtime.gt(ZERO, arg)) {
31: return Runtime.negate(arg);
32: } else {
33: return arg;
34: }
35: } else {
36: undefined(args, context);
37: return null;
38: }
39: }
40:
41: public String toString() {
42: return "fuction abs(number)";
43: }
44: }
|