/interpreter/tags/at2dist090708/src/edu/vub/at/objects/ATNumeric.java

http://ambienttalk.googlecode.com/ · Java · 388 lines · 35 code · 34 blank · 319 comment · 0 complexity · 850ce6320b60f8279081451e6a8e315b 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 closest number to the fraction.
  72. * <p>
  73. * More specifically, rounding a number is equivalent to <code> (fraction + 0.5).floor() </code>
  74. *
  75. * @return an ATNumber resulting of rounding the receiver to the closest number value.
  76. */
  77. public ATNumber base_round() throws InterpreterException;
  78. /**
  79. * Returns the closest number to positive infinity that is smaller than the fraction.
  80. *
  81. * @return the closest number to positive infinity that is smaller than the fraction.
  82. */
  83. public ATNumber base_floor() throws InterpreterException;
  84. /**
  85. * Returns the closest number to negative infinity that is greater than the fraction.
  86. *
  87. * @return the closest number to negative infinity that is greater than the fraction.
  88. */
  89. public ATNumber base_ceiling() throws InterpreterException;
  90. /**
  91. * Returns the numeric data type raised to the power of the argument.
  92. *
  93. * @param pow a ATNumeric data type representing the exponent.
  94. * @return the receiver raised to the power of the argument.
  95. * @throws XTypeMismatch if the exponent is not an {@link ATNumeric} object.
  96. */
  97. public ATFraction base_expt(ATNumeric pow) throws InterpreterException;
  98. // addition +
  99. /**
  100. * Addition infix operator. Returns the value resulting of the sum of the receiver and
  101. * another numeric data type passed as argument.
  102. * <p>
  103. * More specifically, this operator actually calls:
  104. * <ul>
  105. * <li><code>other.addNumber(this) if the receiver is a {@link ATNumber}</code>
  106. * <li><code>other.addFraction(this) if the receiver is a {@link ATFraction}</code>
  107. * </ul>
  108. *
  109. * @param other a numeric data type.
  110. * @return a ATNumeric resulting of the sum of the receiver and other.
  111. */
  112. public ATNumeric base__oppls_(ATNumeric other) throws InterpreterException;
  113. /**
  114. * Returns the value resulting of the sum of the receiver and a number passed as argument.
  115. * <p>
  116. * More specifically, this method returns a {@link ATNumber} if both numeric data types to sum are {@link ATNumber} objects.
  117. * Otherwise, an {@link ATFraction} is returned.
  118. * <p>
  119. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the addition operator.
  120. *
  121. * @param other a number.
  122. * @return a ATNumeric resulting of the sum of the receiver and other.
  123. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  124. */
  125. public ATNumeric base_addNumber(ATNumber other) throws InterpreterException;
  126. /**
  127. * Returns the value resulting of the sum of the receiver and a fraction passed as argument.
  128. * <p>
  129. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the addition operator.
  130. *
  131. * @param other a fraction.
  132. * @return a {@link ATFraction} resulting of the sum of the receiver and other.
  133. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  134. */
  135. public ATNumeric base_addFraction(ATFraction other) throws InterpreterException;
  136. // subtraction -
  137. /**
  138. * Subtraction infix operator. Returns the value resulting of subtracting
  139. * a numeric data type passed as argument from the receiver.
  140. * <p>
  141. * More specifically, this operator actually calls:
  142. * <ul>
  143. * <li><code>other.subtractNumber(this) if the receiver is a {@link ATNumber}</code>
  144. * <li><code>other.subtractFraction(this) if the receiver is a {@link ATFraction}</code>
  145. * </ul>
  146. *
  147. * @param other a numeric data type.
  148. * @return a ATNumeric resulting of subtracting other from the receiver.
  149. */
  150. public ATNumeric base__opmns_(ATNumeric other) throws InterpreterException;
  151. /**
  152. * Returns the value resulting of subtracting the receiver from a number passed as argument.
  153. * <p>
  154. * More specifically, this method returns a {@link ATNumber} if both numeric data types to subtract are {@link ATNumber} objects.
  155. * Otherwise, an {@link ATFraction} is returned.
  156. * <p>
  157. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the subtraction operator.
  158. *
  159. * @param other a number.
  160. * @return a ATNumeric resulting of subtracting other from the receiver.
  161. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  162. */
  163. public ATNumeric base_subtractNumber(ATNumber other) throws InterpreterException;
  164. /**
  165. * Returns the value resulting of the subtracting the receiver from a fraction passed as argument.
  166. * <p>
  167. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the subtraction operator.
  168. *
  169. * @param other a fraction.
  170. * @return a {@link ATFraction} resulting of subtracting other from the receiver.
  171. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  172. */
  173. public ATNumeric base_subtractFraction(ATFraction other) throws InterpreterException;
  174. // multiplication *
  175. /**
  176. * Multiplication infix operator. Returns the value resulting of multiplying the receiver by
  177. * another numeric data type passed as argument.
  178. * <p>
  179. * More specifically, this operator actually calls:
  180. * <ul>
  181. * <li><code>other.timesNumber(this) if the receiver is a {@link ATNumber}</code>
  182. * <li><code>other.timesFraction(this) if the receiver is a {@link ATFraction}</code>
  183. * </ul>
  184. *
  185. * @param other a numeric data type.
  186. * @return a ATNumeric resulting of multiplying the receiver by other.
  187. */
  188. public ATNumeric base__optms_(ATNumeric other) throws InterpreterException;
  189. /**
  190. * Returns the value resulting of multiplying the receiver by a number passed as argument.
  191. * <p>
  192. * More specifically, this method returns a {@link ATNumber} if both numeric data types to multiply are {@link ATNumber} objects.
  193. * Otherwise, an {@link ATFraction} is returned.
  194. * <p>
  195. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the multiplication operator.
  196. *
  197. * @param other a number.
  198. * @return a ATNumeric resulting of multiplying the receiver by other.
  199. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  200. */
  201. public ATNumeric base_timesNumber(ATNumber other) throws InterpreterException;
  202. /**
  203. * Returns the value resulting of multiplying the receiver by a fraction passed as argument.
  204. * <p>
  205. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the multiplication operator.
  206. *
  207. * @param other a fraction.
  208. * @return a {@link ATFraction} resulting of multiplying the receiver by other.
  209. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  210. */
  211. public ATNumeric base_timesFraction(ATFraction other) throws InterpreterException;
  212. // division /
  213. /**
  214. * Division infix operator ("/"). Returns the value resulting of dividing the receiver by
  215. * another numeric data type passed as argument.
  216. * <p>
  217. * More specifically, this operator actually calls:
  218. * <ul>
  219. * <li><code>other.divideNumber(this) if the receiver is a {@link ATNumber}</code>
  220. * <li><code>other.divideFraction(this) if the receiver is a {@link ATFraction}</code>
  221. * </ul>
  222. *
  223. * @param other a numeric data type.
  224. * @return a ATNumeric resulting of dividing the receiver by other.
  225. */
  226. public ATNumeric base__opdiv_(ATNumeric other) throws InterpreterException;
  227. /**
  228. * Returns the value resulting of dividing a number passed as argument by the receiver.
  229. * <p>
  230. * More specifically, this method returns a {@link ATNumber} if both numeric data types to multiply are {@link ATNumber} objects.
  231. * Otherwise, an {@link ATFraction} is returned.
  232. * <p>
  233. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the division operator.
  234. *
  235. * @param other a number.
  236. * @return a ATNumeric resulting of dividing a given number by the receiver.
  237. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  238. * @throws XIllegalArgument if the receiver is 0.
  239. */
  240. public ATNumeric base_divideNumber(ATNumber other) throws InterpreterException;
  241. /**
  242. * Returns the value resulting of dividing a number passed as argument by the receiver.
  243. * <p>
  244. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the division operator.
  245. *
  246. * @param other a fraction.
  247. * @return a {@link ATFraction} resulting of dividing a given number by the receiver.
  248. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  249. * @throws XIllegalArgument if the receiver is 0.
  250. */
  251. public ATNumeric base_divideFraction(ATFraction other) throws InterpreterException;
  252. // comparison: generalized equality <=>
  253. /**
  254. * Generalized equality infix operator. Returns:
  255. * <ul>
  256. * <li>-1 if the receiver is smaller than the numeric data type passed as argument.
  257. * <li>1 if the receiver is greater than the numeric data type passed as argument.
  258. * <li>0 if the receiver is equal to the numeric data type passed as argument.
  259. * </ul>
  260. * <p>
  261. * This method actually calls:
  262. * <ul>
  263. * <li><code>other.gequalsNumber(this) if the receiver is a {@link ATNumber}</code>
  264. * <li><code>other.gequalsFraction(this) if the receiver is a {@link ATFraction}</code>
  265. * </ul>
  266. *
  267. * @param other a numeric data type.
  268. * @return a ATNumber resulting of evaluating the generalized equality between the receiver and other.
  269. */
  270. public ATNumeric base__opltx__opeql__opgtx_(ATNumeric other) throws InterpreterException;
  271. /**
  272. * Returns the value of evaluating the generalized equality between the numeric data type and a number.
  273. * <p>
  274. * The generalized equality returns:
  275. * <ul>
  276. * <li>-1 if the receiver is greater than the number passed as argument.
  277. * <li>1 if the receiver is smaller than the number passed as argument.
  278. * <li>0 if the receiver is equal to the number passed as argument.
  279. * </ul>
  280. * <p>
  281. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the generalized equality operator.
  282. *
  283. * @param other a number.
  284. * @return a {@link ATNumber} resulting of applying the generalized equality between the receiver and other.
  285. * @throws XTypeMismatch if other is not an {@link ATNumber} object.
  286. */
  287. public ATNumeric base_gequalsNumber(ATNumber other) throws InterpreterException;
  288. /**
  289. * Returns the value of evaluating the generalized equality between the numeric data type and a fraction.
  290. * <p>
  291. * The generalized equality returns:
  292. * <ul>
  293. * <li>-1 if the receiver is greater than the fraction passed as argument.
  294. * <li>1 if the receiver is smaller than the fraction passed as argument.
  295. * <li>0 if the receiver is equal to the fraction passed as argument.
  296. * </ul>
  297. * <p>
  298. * Note that this is a double-dispatch method used to determine the correct runtime type of both arguments of the generalized equality operator.
  299. *
  300. * @param other a number.
  301. * @return a {@link ATNumber} resulting of applying the generalized equality between the receiver and other.
  302. * @throws XTypeMismatch if other is not an {@link ATFraction} object.
  303. */
  304. public ATNumeric base_gequalsFraction(ATFraction other) throws InterpreterException;
  305. // Comparable mixin based on <=>
  306. /**
  307. * Returns true if the receiver is smaller than the numeric data type passed as argument.
  308. *
  309. * @param other a numeric data type.
  310. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) == -1</code>
  311. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  312. */
  313. public ATBoolean base__opltx_(ATNumeric other) throws InterpreterException; // <
  314. /**
  315. * Returns true if the receiver is greater than the numeric data type passed as argument.
  316. *
  317. * @param other a numeric data type.
  318. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) == 1</code>
  319. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  320. */
  321. public ATBoolean base__opgtx_(ATNumeric other) throws InterpreterException; // >
  322. /**
  323. * Returns true if the receiver is smaller than or equal to the numeric data type passed as argument.
  324. *
  325. * @param other a numeric data type.
  326. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) != 1</code>
  327. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  328. */
  329. public ATBoolean base__opltx__opeql_(ATNumeric other) throws InterpreterException; // <=
  330. /**
  331. * Returns true if the receiver is greater than or equal to the numeric data type passed as argument.
  332. *
  333. * @param other a numeric data type.
  334. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) != -1</code>
  335. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  336. */
  337. public ATBoolean base__opgtx__opeql_(ATNumeric other) throws InterpreterException; // >=
  338. /**
  339. * Returns true if the receiver is equal to the numeric data type passed as argument.
  340. *
  341. * @param other a numeric data type.
  342. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) == 0</code>
  343. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  344. */
  345. public ATBoolean base__opeql_(ATNumeric other) throws InterpreterException; // =
  346. /**
  347. * Returns true if the receiver is different than the numeric data type passed as argument.
  348. *
  349. * @param other a numeric data type.
  350. * @return a {@link ATBoolean} resulting of evaluating <code> (receiver <=> other) != 0</code>
  351. * @throws XTypeMismatch if other is not an {@link ATNumeric} object.
  352. */
  353. public ATBoolean base__opnot__opeql_(ATNumeric other) throws InterpreterException; // !=
  354. }