Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2008, Manuel Mausz <manuel at mausz.at>
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  * * Redistributions of source code must retain the above copyright
  8.  * notice, this list of conditions and the following disclaimer.
  9.  * * Redistributions in binary form must reproduce the above copyright
  10.  * notice, this list of conditions and the following disclaimer in the
  11.  * documentation and/or other materials provided with the distribution.
  12.  * * Neither the name of the copyright holders nor the
  13.  * names of its contributors may be used to endorse or promote products
  14.  * derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20.  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  23.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  25.  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  27.  * DAMAGE.
  28.  */
  29.  
  30. import java.util.Stack;
  31.  
  32. /**
  33.  * Implements mathematical invers (1/x)
  34.  *
  35.  * @version 1.0
  36.  * @author Manuel Mausz (manuel at mausz.at)
  37.  * @author http://manuel.mausz.at/
  38.  */
  39. class InvOperation implements UPNOperation
  40. {
  41. /**
  42.   * constructor
  43.   */
  44. public InvOperation()
  45. {
  46. }
  47.  
  48. /**
  49.   * returns operations symbol
  50.   *
  51.   * @return operation symbol
  52.   */
  53. public String getOperationSymbol()
  54. {
  55. return "inv";
  56. }
  57.  
  58. /**
  59.   * performs the actual execution
  60.   *
  61.   * @param op stack to retrieve the values from
  62.   */
  63. public void execute (Stack<Double> op)
  64. throws InvalidParameterException
  65. {
  66. double v1, res;
  67.  
  68. if (op.empty())
  69. throw new InvalidParameterException("Insuifficent operands on stack");
  70. v1 = op.pop();
  71. if (Math.abs(v1) < EPSILON)
  72. v1 = 0;
  73.  
  74. res = 1 / v1;
  75. if (Math.abs(res) < EPSILON)
  76. res = 0;
  77. op.push(res);
  78. }
  79. }
  80.