KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > function > Modulo


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.gp.function;
11
12 import org.jgap.*;
13 import org.jgap.gp.*;
14 import org.jgap.gp.impl.*;
15
16 /**
17  * The modulo operation.
18  *
19  * @author Konrad Odell
20  * @author Klaus Meffert
21  * @since 3.0
22  */

23 public class Modulo
24     extends MathCommand {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private static final String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
27
28   public Modulo(final GPConfiguration a_conf, Class JavaDoc a_type)
29       throws InvalidConfigurationException {
30     super(a_conf, 2, a_type);
31   }
32
33   public String JavaDoc toString() {
34     return "&1 % &2";
35   }
36
37   /**
38    * @return textual name of this command
39    *
40    * @author Klaus Meffert
41    * @since 3.2
42    */

43   public String JavaDoc getName() {
44     return "Modulo";
45   }
46
47   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
48     int v1 = c.execute_int(n, 0, args);
49     int v2 = c.execute_int(n, 1, args);
50     if (v2 == 0) {
51       return 0;
52     }
53     return v1 % v2;
54   }
55
56   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
57     long v1 = c.execute_long(n, 0, args);
58     long v2 = c.execute_long(n, 1, args);
59     if (v2 == 0) {
60       return 0;
61     }
62     return v1 % v2;
63   }
64
65   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
66     float v1 = c.execute_float(n, 0, args);
67     float v2 = c.execute_float(n, 1, args);
68     if (Math.abs(v2) < DELTA) {
69       return 0;
70     }
71     return v1 % v2;
72   }
73
74   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
75     double v1 = c.execute_double(n, 0, args);
76     double v2 = c.execute_double(n, 1, args);
77     if (Math.abs(v2) < DELTA) {
78       return 0;
79     }
80     return v1 % v2;
81   }
82
83   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
84     try {
85       return ( (Compatible) c.execute_object(n, 0, args)).execute_mod(c.
86           execute_object(n, 1, args));
87     } catch (ArithmeticException JavaDoc aex) {
88       throw new IllegalStateException JavaDoc("mod with illegal arguments");
89     }
90   }
91
92   protected interface Compatible {
93     public Object JavaDoc execute_mod(Object JavaDoc o);
94   }
95 }
96
Popular Tags