/interpreter/tags/at2-build060407/src/edu/vub/at/objects/natives/NATFraction.java

http://ambienttalk.googlecode.com/ · Java · 210 lines · 118 code · 26 blank · 66 comment · 13 complexity · c58f5935ddfee0c9446d9c13530088e3 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATFraction.java created on 26-jul-2006 at 16:42:48
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.objects.natives;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.exceptions.XIllegalArgument;
  31. import edu.vub.at.objects.ATBoolean;
  32. import edu.vub.at.objects.ATFraction;
  33. import edu.vub.at.objects.ATNumber;
  34. import edu.vub.at.objects.ATNumeric;
  35. import edu.vub.at.objects.ATObject;
  36. import edu.vub.at.objects.ATTable;
  37. import edu.vub.at.objects.coercion.NativeStripes;
  38. /**
  39. * The native implementation of an AmbientTalk fraction.
  40. * A fraction is implemented by a Java double.
  41. *
  42. * @author tvc
  43. */
  44. public final class NATFraction extends NATNumeric implements ATFraction {
  45. public static final NATFraction INFTY = new NATFraction(Double.POSITIVE_INFINITY);
  46. public final double javaValue;
  47. /**
  48. * This method currently serves as a hook for fraction creation.
  49. * Currently fraction objects are not reused, but this might change in the future.
  50. */
  51. public static final NATFraction atValue(double javaFrc) {
  52. return new NATFraction(javaFrc);
  53. }
  54. private NATFraction(double javaFrc) {
  55. javaValue = javaFrc;
  56. }
  57. public NATFraction asNativeFraction() {
  58. return this;
  59. }
  60. public ATBoolean base__opeql__opeql_(ATObject comparand) {
  61. return NATBoolean.atValue(this.equals(comparand));
  62. }
  63. public boolean equals(Object comparand) {
  64. return (comparand instanceof NATFraction) &&
  65. (javaValue == ((NATFraction) comparand).javaValue);
  66. }
  67. public NATText meta_print() throws InterpreterException {
  68. return NATText.atValue(String.valueOf(javaValue));
  69. }
  70. public ATTable meta_getStripes() throws InterpreterException {
  71. return NATTable.of(NativeStripes._FRACTION_);
  72. }
  73. // contract with NATNumeric
  74. protected double getJavaValue() { return javaValue; }
  75. /* -------------------------------------
  76. * - base-level interface to fractions -
  77. * ------------------------------------- */
  78. // Fraction arithmetic operations
  79. /**
  80. * FRC(n).inc() => FRC(n+1)
  81. */
  82. public ATNumeric base_inc() {
  83. return NATFraction.atValue(javaValue+1);
  84. }
  85. /**
  86. * FRC(n).dec() => FRC(n-1)
  87. */
  88. public ATNumeric base_dec() {
  89. return NATFraction.atValue(javaValue-1);
  90. }
  91. /**
  92. * FRC(n).abs() => FRC(abs(n))
  93. */
  94. public ATNumeric base_abs() {
  95. return NATFraction.atValue(Math.abs(javaValue));
  96. }
  97. /**
  98. * FRC(n).round() => NBR(round(n))
  99. */
  100. public ATNumber base_round() {
  101. return NATNumber.atValue(Math.round((float) javaValue));
  102. }
  103. /**
  104. * FRC(n).floor() => NBR(floor(n))
  105. */
  106. public ATNumber base_floor() {
  107. return NATNumber.atValue(Math.round((float) Math.floor(javaValue)));
  108. }
  109. /**
  110. * FRC(n).ceiling() => NBR(ceil(n))
  111. */
  112. public ATNumber base_ceiling() {
  113. return NATNumber.atValue(Math.round((float) Math.ceil(javaValue)));
  114. }
  115. // Numeric arithmetic operations
  116. // addition +
  117. public ATNumeric base__oppls_(ATNumeric other) throws InterpreterException {
  118. return other.base_addFraction(this);
  119. }
  120. public ATNumeric base_addNumber(ATNumber other) throws InterpreterException {
  121. return NATFraction.atValue(javaValue + other.asNativeNumber().javaValue);
  122. }
  123. public ATNumeric base_addFraction(ATFraction other) throws InterpreterException {
  124. return NATFraction.atValue(javaValue + other.asNativeFraction().javaValue);
  125. }
  126. // subtraction -
  127. public ATNumeric base__opmns_(ATNumeric other) throws InterpreterException {
  128. return other.base_subtractFraction(this);
  129. }
  130. public ATNumeric base_subtractNumber(ATNumber other) throws InterpreterException {
  131. return NATFraction.atValue(other.asNativeNumber().javaValue - javaValue);
  132. }
  133. public ATNumeric base_subtractFraction(ATFraction other) throws InterpreterException {
  134. return NATFraction.atValue(other.asNativeFraction().javaValue - javaValue);
  135. }
  136. // multiplication *
  137. public ATNumeric base__optms_(ATNumeric other) throws InterpreterException {
  138. return other.base_timesFraction(this);
  139. }
  140. public ATNumeric base_timesNumber(ATNumber other) throws InterpreterException {
  141. return NATFraction.atValue(other.asNativeNumber().javaValue * javaValue);
  142. }
  143. public ATNumeric base_timesFraction(ATFraction other) throws InterpreterException {
  144. return NATFraction.atValue(other.asNativeFraction().javaValue * javaValue);
  145. }
  146. // division /
  147. public ATNumeric base__opdiv_(ATNumeric other) throws InterpreterException {
  148. return other.base_divideFraction(this);
  149. }
  150. public ATNumeric base_divideNumber(ATNumber other) throws InterpreterException {
  151. if (javaValue == 0)
  152. throw new XIllegalArgument("Division by zero: " + other);
  153. return NATFraction.atValue((other.asNativeNumber().javaValue * 1.0) / javaValue);
  154. }
  155. public ATNumeric base_divideFraction(ATFraction other) throws InterpreterException {
  156. if (javaValue == 0)
  157. throw new XIllegalArgument("Division by zero: " + other);
  158. return NATFraction.atValue(other.asNativeFraction().javaValue / javaValue);
  159. }
  160. // comparison: generalized equality <=>
  161. public ATNumeric base__opltx__opeql__opgtx_(ATNumeric other) throws InterpreterException {
  162. return other.base_gequalsFraction(this);
  163. }
  164. public ATNumeric base_gequalsNumber(ATNumber other) throws InterpreterException {
  165. int n = other.asNativeNumber().javaValue;
  166. if (n < javaValue) {
  167. return NATNumber.MONE; // -1
  168. } else if (n > javaValue) {
  169. return NATNumber.ONE; // +1
  170. } else {
  171. return NATNumber.ZERO; // 0
  172. }
  173. }
  174. public ATNumeric base_gequalsFraction(ATFraction other) throws InterpreterException {
  175. double n = other.asNativeFraction().javaValue;
  176. if (n < javaValue) {
  177. return NATNumber.MONE; // -1
  178. } else if (n > javaValue) {
  179. return NATNumber.ONE; // +1
  180. } else {
  181. return NATNumber.ZERO; // 0
  182. }
  183. }
  184. }