/interpreter/tags/at2-build270707/src/edu/vub/at/objects/ATNumeric.java

http://ambienttalk.googlecode.com/ · Java · 365 lines · 32 code · 31 blank · 302 comment · 0 complexity · 434d5d9d93205752e9fea2307bc37bf0 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * ATNumeric.java created on 18-aug-2006 at 11:00:01
  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;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.objects.grammar.ATExpression;
  31. /**
  32. * ATNumeric is the public interface common to numbers and fractions.
  33. *
  34. * This interface extends ATExpression as a number or fraction can also be output by the parser as a literal.
  35. *
  36. * @author tvc
  37. */
  38. public interface ATNumeric extends ATExpression {
  39. // base-level interface to both numbers and fractions
  40. /**
  41. * Returns the trigonometric cosine of the numeric data type representing an angle in radians.
  42. *
  43. * @return the cosine of the receiver.
  44. */
  45. public ATFraction base_cos() throws InterpreterException;
  46. /**
  47. * Returns the trigonometric sine of the numeric data type representing an angle in radians.
  48. *
  49. * @return the sine of the receiver.
  50. */
  51. public ATFraction base_sin() throws InterpreterException;
  52. /**
  53. * Returns the trigonometric tangent of the numeric data type representing an angle in radians.
  54. *
  55. * @return the tangent of the receiver.
  56. */
  57. public ATFraction base_tan() throws InterpreterException;
  58. /**
  59. * Returns the natural logarithm (base e) of the numeric data type.
  60. *
  61. * @return the natural logarithm of the receiver or NaN if the receiver is smaller than 0.0.
  62. */
  63. public ATFraction base_log() throws InterpreterException;
  64. /**
  65. * Returns the positive square root of the numeric data type.
  66. *
  67. * @return the correctly rounded positive square root of a or NaN if the receiver is smaller than zero.
  68. */
  69. public ATFraction base_sqrt() throws InterpreterException;
  70. /**
  71. * Returns the numeric data type raised to the power of the argument.
  72. *
  73. * @param pow a ATNumeric data type representing the exponent.
  74. * @return the receiver raised to the power of the argument.
  75. * @throws XTypeMismatch if the exponent is not an {@link ATNumeric} object.
  76. */
  77. public ATFraction base_expt(ATNumeric pow) throws InterpreterException;
  78. // addition +
  79. /**
  80. * Addition infix operator. Returns the value resulting of the sum of the receiver and
  81. * another numeric data type passed as argument.
  82. * <p>
  83. * More specifically, this operator actually calls:
  84. * <ul>
  85. * <li><code>other.addNumber(this) if the receiver is a {@link ATNumber}</code>
  86. * <li><code>other.addFraction(this) if the receiver is a {@link ATFraction}</code>
  87. * </ul>
  88. *
  89. * @param other a numeric data type.
  90. * @return a ATNumeric resulting of the sum of the receiver and other.
  91. */
  92. public ATNumeric base__oppls_(ATNumeric other) throws InterpreterException;
  93. /**
  94. * Returns the value resulting of the sum of the receiver and a number passed as argument.
  95. * <p>
  96. * More specifically, this method returns a {@link ATNumber} if both numeric data types to sum are {@link ATNumber} objects.
  97. * Otherwise, an {@link ATFraction} is returned.
  98. * <p>
  99. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the addition operator.
  100. *
  101. * @param other a number.
  102. * @return a ATNumeric resulting of the sum of the receiver and other.
  103. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  104. */
  105. public ATNumeric base_addNumber(ATNumber other) throws InterpreterException;
  106. /**
  107. * Returns the value resulting of the sum of the receiver and a fraction passed as argument.
  108. * <p>
  109. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the addition operator.
  110. *
  111. * @param other a fraction.
  112. * @return a {@link ATFraction} resulting of the sum of the receiver and other.
  113. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  114. */
  115. public ATNumeric base_addFraction(ATFraction other) throws InterpreterException;
  116. // subtraction -
  117. /**
  118. * Subtraction infix operator. Returns the value resulting of subtracting
  119. * a numeric data type passed as argument from the receiver.
  120. * <p>
  121. * More specifically, this operator actually calls:
  122. * <ul>
  123. * <li><code>other.subtractNumber(this) if the receiver is a {@link ATNumber}</code>
  124. * <li><code>other.subtractFraction(this) if the receiver is a {@link ATFraction}</code>
  125. * </ul>
  126. *
  127. * @param other a numeric data type.
  128. * @return a ATNumeric resulting of subtracting other from the receiver.
  129. */
  130. public ATNumeric base__opmns_(ATNumeric other) throws InterpreterException;
  131. /**
  132. * Returns the value resulting of subtracting the receiver from a number passed as argument.
  133. * <p>
  134. * More specifically, this method returns a {@link ATNumber} if both numeric data types to subtract are {@link ATNumber} objects.
  135. * Otherwise, an {@link ATFraction} is returned.
  136. * <p>
  137. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the subtraction operator.
  138. *
  139. * @param other a number.
  140. * @return a ATNumeric resulting of subtracting other from the receiver.
  141. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  142. */
  143. public ATNumeric base_subtractNumber(ATNumber other) throws InterpreterException;
  144. /**
  145. * Returns the value resulting of the subtracting the receiver from a fraction passed as argument.
  146. * <p>
  147. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the subtraction operator.
  148. *
  149. * @param other a fraction.
  150. * @return a {@link ATFraction} resulting of subtracting other from the receiver.
  151. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  152. */
  153. public ATNumeric base_subtractFraction(ATFraction other) throws InterpreterException;
  154. // multiplication *
  155. /**
  156. * Multiplication infix operator. Returns the value resulting of multiplying the receiver by
  157. * another numeric data type passed as argument.
  158. * <p>
  159. * More specifically, this operator actually calls:
  160. * <ul>
  161. * <li><code>other.timesNumber(this) if the receiver is a {@link ATNumber}</code>
  162. * <li><code>other.timesFraction(this) if the receiver is a {@link ATFraction}</code>
  163. * </ul>
  164. *
  165. * @param other a numeric data type.
  166. * @return a ATNumeric resulting of multiplying the receiver by other.
  167. */
  168. public ATNumeric base__optms_(ATNumeric other) throws InterpreterException;
  169. /**
  170. * Returns the value resulting of multiplying the receiver by a number passed as argument.
  171. * <p>
  172. * More specifically, this method returns a {@link ATNumber} if both numeric data types to multiply are {@link ATNumber} objects.
  173. * Otherwise, an {@link ATFraction} is returned.
  174. * <p>
  175. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the multiplication operator.
  176. *
  177. * @param other a number.
  178. * @return a ATNumeric resulting of multiplying the receiver by other.
  179. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  180. */
  181. public ATNumeric base_timesNumber(ATNumber other) throws InterpreterException;
  182. /**
  183. * Returns the value resulting of multiplying the receiver by a fraction passed as argument.
  184. * <p>
  185. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the multiplication operator.
  186. *
  187. * @param other a fraction.
  188. * @return a {@link ATFraction} resulting of multiplying the receiver by other.
  189. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  190. */
  191. public ATNumeric base_timesFraction(ATFraction other) throws InterpreterException;
  192. // division /
  193. /**
  194. * Division infix operator ("/"). Returns the value resulting of dividing the receiver by
  195. * another numeric data type passed as argument.
  196. * <p>
  197. * More specifically, this operator actually calls:
  198. * <ul>
  199. * <li><code>other.divideNumber(this) if the receiver is a {@link ATNumber}</code>
  200. * <li><code>other.divideFraction(this) if the receiver is a {@link ATFraction}</code>
  201. * </ul>
  202. *
  203. * @param other a numeric data type.
  204. * @return a ATNumeric resulting of dividing the receiver by other.
  205. */
  206. public ATNumeric base__opdiv_(ATNumeric other) throws InterpreterException;
  207. /**
  208. * Returns the value resulting of dividing a number passed as argument by the receiver.
  209. * <p>
  210. * More specifically, this method returns a {@link ATNumber} if both numeric data types to multiply are {@link ATNumber} objects.
  211. * Otherwise, an {@link ATFraction} is returned.
  212. * <p>
  213. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the division operator.
  214. *
  215. * @param other a number.
  216. * @return a ATNumeric resulting of dividing a given number by the receiver.
  217. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  218. * @throws XIllegalArgument if the receiver is 0.
  219. */
  220. public ATNumeric base_divideNumber(ATNumber other) throws InterpreterException;
  221. /**
  222. * Returns the value resulting of dividing a number passed as argument by the receiver.
  223. * <p>
  224. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the division operator.
  225. *
  226. * @param other a fraction.
  227. * @return a {@link ATFraction} resulting of dividing a given number by the receiver.
  228. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  229. * @throws XIllegalArgument if the receiver is 0.
  230. */
  231. public ATNumeric base_divideFraction(ATFraction other) throws InterpreterException;
  232. // comparison: generalized equality <=>
  233. /**
  234. * Generalized equality infix operator. Returns:
  235. * <ul>
  236. * <li>-1 if the receiver is smaller than the numeric data type passed as argument.
  237. * <li>1 if the receiver is greater than the numeric data type passed as argument.
  238. * <li>0 if the receiver is equal to the numeric data type passed as argument.
  239. * </ul>
  240. * <p>
  241. * This method actually calls:
  242. * <ul>
  243. * <li><code>other.gequalsNumber(this) if the receiver is a {@link ATNumber}</code>
  244. * <li><code>other.gequalsFraction(this) if the receiver is a {@link ATFraction}</code>
  245. * </ul>
  246. *
  247. * @param other a numeric data type.
  248. * @return a ATNumber resulting of evaluating the generalized equality between the receiver and other.
  249. */
  250. public ATNumeric base__opltx__opeql__opgtx_(ATNumeric other) throws InterpreterException;
  251. /**
  252. * Returns the value of evaluating the generalized equality between the numeric data type and a number.
  253. * <p>
  254. * The generalized equality returns:
  255. * <ul>
  256. * <li>-1 if the receiver is greater than the number passed as argument.
  257. * <li>1 if the receiver is smaller than the number passed as argument.
  258. * <li>0 if the receiver is equal to the number passed as argument.
  259. * </ul>
  260. * <p>
  261. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the generalized equality operator.
  262. *
  263. * @param other a number.
  264. * @return a {@link ATNumber} resulting of applying the generalized equality between the receiver and other.
  265. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  266. */
  267. public ATNumeric base_gequalsNumber(ATNumber other) throws InterpreterException;
  268. /**
  269. * Returns the value of evaluating the generalized equality between the numeric data type and a fraction.
  270. * <p>
  271. * The generalized equality returns:
  272. * <ul>
  273. * <li>-1 if the receiver is greater than the fraction passed as argument.
  274. * <li>1 if the receiver is smaller than the fraction passed as argument.
  275. * <li>0 if the receiver is equal to the fraction passed as argument.
  276. * </ul>
  277. * <p>
  278. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the generalized equality operator.
  279. *
  280. * @param other a number.
  281. * @return a {@link ATNumber} resulting of applying the generalized equality between the receiver and other.
  282. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  283. */
  284. public ATNumeric base_gequalsFraction(ATFraction other) throws InterpreterException;
  285. // Comparable mixin based on <=>
  286. /**
  287. * Returns true if the receiver is smaller than the numeric data type passed as argument.
  288. *
  289. * @param other a numeric data type.
  290. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) == -1</code>
  291. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  292. */
  293. public ATBoolean base__opltx_(ATNumeric other) throws InterpreterException; // <
  294. /**
  295. * Returns true if the receiver is greater than the numeric data type passed as argument.
  296. *
  297. * @param other a numeric data type.
  298. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) == 1</code>
  299. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  300. */
  301. public ATBoolean base__opgtx_(ATNumeric other) throws InterpreterException; // >
  302. /**
  303. * Returns true if the receiver is smaller than or equal to the numeric data type passed as argument.
  304. *
  305. * @param other a numeric data type.
  306. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) != 1</code>
  307. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  308. */
  309. public ATBoolean base__opltx__opeql_(ATNumeric other) throws InterpreterException; // <=
  310. /**
  311. * Returns true if the receiver is greater than or equal to the numeric data type passed as argument.
  312. *
  313. * @param other a numeric data type.
  314. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) != -1</code>
  315. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  316. */
  317. public ATBoolean base__opgtx__opeql_(ATNumeric other) throws InterpreterException; // >=
  318. /**
  319. * Returns true if the receiver is equal to the numeric data type passed as argument.
  320. *
  321. * @param other a numeric data type.
  322. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) == 0</code>
  323. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  324. */
  325. public ATBoolean base__opeql_(ATNumeric other) throws InterpreterException; // =
  326. /**
  327. * Returns true if the receiver is different than the numeric data type passed as argument.
  328. *
  329. * @param other a numeric data type.
  330. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) != 0</code>
  331. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  332. */
  333. public ATBoolean base__opnot__opeql_(ATNumeric other) throws InterpreterException; // !=
  334. }