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

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