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

http://ambienttalk.googlecode.com/ · Java · 148 lines · 58 code · 16 blank · 74 comment · 0 complexity · 91d6292817c24983b1822b83e05846c5 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATNumeric.java created on 18-aug-2006 at 11:06:11
  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.XTypeMismatch;
  31. import edu.vub.at.objects.ATBoolean;
  32. import edu.vub.at.objects.ATFraction;
  33. import edu.vub.at.objects.ATNumeric;
  34. import edu.vub.at.objects.ATObject;
  35. import edu.vub.at.objects.ATTable;
  36. import edu.vub.at.objects.coercion.NativeTypeTags;
  37. import edu.vub.at.objects.natives.grammar.AGExpression;
  38. /**
  39. * A common superclass of both numbers and fractions to factor out common base-level behaviour.
  40. *
  41. * @author tvc
  42. */
  43. public abstract class NATNumeric extends AGExpression implements ATNumeric {
  44. /**
  45. * Template method that should return the value of the underlying number or fraction as a double
  46. */
  47. protected abstract double getJavaValue();
  48. public NATNumeric asNativeNumeric() throws XTypeMismatch {
  49. return this;
  50. }
  51. public ATTable meta_typeTags() throws InterpreterException {
  52. return NATTable.of(NativeTypeTags._NUMERIC_);
  53. }
  54. // numbers and fractions are singletons
  55. public ATObject meta_clone() throws InterpreterException {
  56. return this;
  57. }
  58. // trigonometric functions
  59. /**
  60. * NUM(n).cos() => FRC(Math.cos(n))
  61. */
  62. public ATFraction base_cos() {
  63. return NATFraction.atValue(Math.cos(getJavaValue()));
  64. }
  65. /**
  66. * NUM(n).sin() => FRC(Math.sin(n))
  67. */
  68. public ATFraction base_sin() {
  69. return NATFraction.atValue(Math.sin(getJavaValue()));
  70. }
  71. /**
  72. * NUM(n).tan() => FRC(Math.tan(n))
  73. */
  74. public ATFraction base_tan() {
  75. return NATFraction.atValue(Math.tan(getJavaValue()));
  76. }
  77. /**
  78. * NUM(n).log() => FRC(log(e,n))
  79. */
  80. public ATFraction base_log() {
  81. return NATFraction.atValue(Math.log(getJavaValue()));
  82. }
  83. /**
  84. * NUM(n).sqrt() => FRC(Math.sqrt(n))
  85. */
  86. public ATFraction base_sqrt() {
  87. return NATFraction.atValue(Math.sqrt(getJavaValue()));
  88. }
  89. /**
  90. * NUM(n).expt(NUM(e)) => FRC(Math.pow(n,e))
  91. */
  92. public ATFraction base_expt(ATNumeric pow) throws InterpreterException {
  93. return NATFraction.atValue(Math.pow(getJavaValue(), pow.asNativeNumeric().getJavaValue()));
  94. }
  95. // Comparable 'mixin' based on <=>
  96. /**
  97. * a < b iff (a <=> b) == -1
  98. */
  99. public ATBoolean base__opltx_(ATNumeric other) throws InterpreterException {
  100. return NATBoolean.atValue(this.base__opltx__opeql__opgtx_(other).equals(NATNumber.MONE));
  101. }
  102. /**
  103. * a > b iff (a <=> b) == +1
  104. */
  105. public ATBoolean base__opgtx_(ATNumeric other) throws InterpreterException {
  106. return NATBoolean.atValue(this.base__opltx__opeql__opgtx_(other).equals(NATNumber.ONE));
  107. }
  108. /**
  109. * a <= b iff (a <=> b) != +1
  110. */
  111. public ATBoolean base__opltx__opeql_(ATNumeric other) throws InterpreterException {
  112. return NATBoolean.atValue(! this.base__opltx__opeql__opgtx_(other).equals(NATNumber.ONE));
  113. }
  114. /**
  115. * a >= b iff (a <=> b) != -1
  116. */
  117. public ATBoolean base__opgtx__opeql_(ATNumeric other) throws InterpreterException {
  118. return NATBoolean.atValue(! this.base__opltx__opeql__opgtx_(other).equals(NATNumber.MONE));
  119. }
  120. /**
  121. * a = b iff (a <=> b) == 0
  122. */
  123. public ATBoolean base__opeql_(ATNumeric other) throws InterpreterException {
  124. return NATBoolean.atValue(this.base__opltx__opeql__opgtx_(other).equals(NATNumber.ZERO));
  125. }
  126. /**
  127. * a != b iff (a <=> b) != 0
  128. */
  129. public ATBoolean base__opnot__opeql_(ATNumeric other) throws InterpreterException {
  130. return NATBoolean.atValue(! this.base__opltx__opeql__opgtx_(other).equals(NATNumber.ZERO));
  131. }
  132. }