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

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