1 10 package org.jgap.gp.function; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.jgap.gp.impl.*; 15 16 23 public class Modulo 24 extends MathCommand { 25 26 private static final String CVS_REVISION = "$Revision: 1.7 $"; 27 28 public Modulo(final GPConfiguration a_conf, Class a_type) 29 throws InvalidConfigurationException { 30 super(a_conf, 2, a_type); 31 } 32 33 public String toString() { 34 return "&1 % &2"; 35 } 36 37 43 public String getName() { 44 return "Modulo"; 45 } 46 47 public int execute_int(ProgramChromosome c, int n, Object [] 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 [] 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 [] 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 [] 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 execute_object(ProgramChromosome c, int n, Object [] args) { 84 try { 85 return ( (Compatible) c.execute_object(n, 0, args)).execute_mod(c. 86 execute_object(n, 1, args)); 87 } catch (ArithmeticException aex) { 88 throw new IllegalStateException ("mod with illegal arguments"); 89 } 90 } 91 92 protected interface Compatible { 93 public Object execute_mod(Object o); 94 } 95 } 96 | Popular Tags |