/interpreter/tags/at_build150307/src/edu/vub/at/objects/natives/NATFraction.java

http://ambienttalk.googlecode.com/ · Java · 204 lines · 113 code · 25 blank · 66 comment · 13 complexity · 00e807a4a07703c466a27706bf8ee937 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.ATFraction;
  32. import edu.vub.at.objects.ATNumber;
  33. import edu.vub.at.objects.ATNumeric;
  34. import edu.vub.at.objects.ATTable;
  35. import edu.vub.at.objects.coercion.NativeStripes;
  36. /**
  37. * The native implementation of an AmbientTalk fraction.
  38. * A fraction is implemented by a Java double.
  39. *
  40. * @author tvc
  41. */
  42. public final class NATFraction extends NATNumeric implements ATFraction {
  43. public static final NATFraction INFTY = new NATFraction(Double.POSITIVE_INFINITY);
  44. public final double javaValue;
  45. /**
  46. * This method currently serves as a hook for fraction creation.
  47. * Currently fraction objects are not reused, but this might change in the future.
  48. */
  49. public static final NATFraction atValue(double javaFrc) {
  50. return new NATFraction(javaFrc);
  51. }
  52. private NATFraction(double javaFrc) {
  53. javaValue = javaFrc;
  54. }
  55. public NATFraction asNativeFraction() {
  56. return this;
  57. }
  58. public boolean equals(Object other) {
  59. return (other instanceof NATFraction) &&
  60. (javaValue == ((NATFraction) other).javaValue);
  61. }
  62. public NATText meta_print() throws InterpreterException {
  63. return NATText.atValue(String.valueOf(javaValue));
  64. }
  65. public ATTable meta_getStripes() throws InterpreterException {
  66. return NATTable.of(NativeStripes._FRACTION_);
  67. }
  68. // contract with NATNumeric
  69. protected double getJavaValue() { return javaValue; }
  70. /* -------------------------------------
  71. * - base-level interface to fractions -
  72. * ------------------------------------- */
  73. // Fraction arithmetic operations
  74. /**
  75. * FRC(n).inc() => FRC(n+1)
  76. */
  77. public ATNumeric base_inc() {
  78. return NATFraction.atValue(javaValue+1);
  79. }
  80. /**
  81. * FRC(n).dec() => FRC(n-1)
  82. */
  83. public ATNumeric base_dec() {
  84. return NATFraction.atValue(javaValue-1);
  85. }
  86. /**
  87. * FRC(n).abs() => FRC(abs(n))
  88. */
  89. public ATNumeric base_abs() {
  90. return NATFraction.atValue(Math.abs(javaValue));
  91. }
  92. /**
  93. * FRC(n).round() => NBR(round(n))
  94. */
  95. public ATNumber base_round() {
  96. return NATNumber.atValue(Math.round((float) javaValue));
  97. }
  98. /**
  99. * FRC(n).floor() => NBR(floor(n))
  100. */
  101. public ATNumber base_floor() {
  102. return NATNumber.atValue(Math.round((float) Math.floor(javaValue)));
  103. }
  104. /**
  105. * FRC(n).ceiling() => NBR(ceil(n))
  106. */
  107. public ATNumber base_ceiling() {
  108. return NATNumber.atValue(Math.round((float) Math.ceil(javaValue)));
  109. }
  110. // Numeric arithmetic operations
  111. // addition +
  112. public ATNumeric base__oppls_(ATNumeric other) throws InterpreterException {
  113. return other.base_addFraction(this);
  114. }
  115. public ATNumeric base_addNumber(ATNumber other) throws InterpreterException {
  116. return NATFraction.atValue(javaValue + other.asNativeNumber().javaValue);
  117. }
  118. public ATNumeric base_addFraction(ATFraction other) throws InterpreterException {
  119. return NATFraction.atValue(javaValue + other.asNativeFraction().javaValue);
  120. }
  121. // subtraction -
  122. public ATNumeric base__opmns_(ATNumeric other) throws InterpreterException {
  123. return other.base_subtractFraction(this);
  124. }
  125. public ATNumeric base_subtractNumber(ATNumber other) throws InterpreterException {
  126. return NATFraction.atValue(other.asNativeNumber().javaValue - javaValue);
  127. }
  128. public ATNumeric base_subtractFraction(ATFraction other) throws InterpreterException {
  129. return NATFraction.atValue(other.asNativeFraction().javaValue - javaValue);
  130. }
  131. // multiplication *
  132. public ATNumeric base__optms_(ATNumeric other) throws InterpreterException {
  133. return other.base_timesFraction(this);
  134. }
  135. public ATNumeric base_timesNumber(ATNumber other) throws InterpreterException {
  136. return NATFraction.atValue(other.asNativeNumber().javaValue * javaValue);
  137. }
  138. public ATNumeric base_timesFraction(ATFraction other) throws InterpreterException {
  139. return NATFraction.atValue(other.asNativeFraction().javaValue * javaValue);
  140. }
  141. // division /
  142. public ATNumeric base__opdiv_(ATNumeric other) throws InterpreterException {
  143. return other.base_divideFraction(this);
  144. }
  145. public ATNumeric base_divideNumber(ATNumber other) throws InterpreterException {
  146. if (javaValue == 0)
  147. throw new XIllegalArgument("Division by zero: " + other);
  148. return NATFraction.atValue((other.asNativeNumber().javaValue * 1.0) / javaValue);
  149. }
  150. public ATNumeric base_divideFraction(ATFraction other) throws InterpreterException {
  151. if (javaValue == 0)
  152. throw new XIllegalArgument("Division by zero: " + other);
  153. return NATFraction.atValue(other.asNativeFraction().javaValue / javaValue);
  154. }
  155. // comparison: generalized equality <=>
  156. public ATNumeric base__opltx__opeql__opgtx_(ATNumeric other) throws InterpreterException {
  157. return other.base_gequalsFraction(this);
  158. }
  159. public ATNumeric base_gequalsNumber(ATNumber other) throws InterpreterException {
  160. int n = other.asNativeNumber().javaValue;
  161. if (n < javaValue) {
  162. return NATNumber.MONE; // -1
  163. } else if (n > javaValue) {
  164. return NATNumber.ONE; // +1
  165. } else {
  166. return NATNumber.ZERO; // 0
  167. }
  168. }
  169. public ATNumeric base_gequalsFraction(ATFraction other) throws InterpreterException {
  170. double n = other.asNativeFraction().javaValue;
  171. if (n < javaValue) {
  172. return NATNumber.MONE; // -1
  173. } else if (n > javaValue) {
  174. return NATNumber.ONE; // +1
  175. } else {
  176. return NATNumber.ZERO; // 0
  177. }
  178. }
  179. }