001/* Compute the remainder after dividing the input by the divisor.
002
003 Copyright (c) 1990-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib;
029
030import ptolemy.data.DoubleToken;
031import ptolemy.data.expr.Parameter;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// Remainder
039
040/**
041 <p>Compute the remainder after dividing the input by the divisor.
042 The input and output data types are both double.
043 This is implemented using the IEEEremainder() method of the java Math
044 class, which computes the remainder as prescribed by the IEEE 754
045 standard. The method documentation states:</p>
046 <blockquote>
047 "The remainder value is mathematically equal to f1 - f2 ? n, where n
048 is the mathematical integer closest to the exact mathematical value
049 of the quotient f1/f2, and if two mathematical integers are equally
050 close to f1/f2, then n is the integer that is even. If the
051 remainder is zero, its sign is the same as the sign of the first
052 argument. Special cases:
053 <ul>
054 <li> If either argument is NaN, or the first argument is infinite,
055 or the second argument is positive zero or negative zero,
056 then the result is NaN.</li>
057 <li> If the first argument is finite and the second argument is
058 infinite, then the result is the same as the first argument."</li>
059 </ul>
060 </blockquote>
061
062 <p>Note: The divisor parameter is available as an input port in
063 the MathFunction.Modulo() method. If you need to change the divisor
064 during run-time, the MathFunction actor may be the a better choice.</p>
065
066 @author Edward A. Lee
067 @version $Id$
068 @see UnaryMathFunction
069 @since Ptolemy II 1.0.1
070 @Pt.ProposedRating Yellow (eal)
071 @Pt.AcceptedRating Yellow (cxh)
072 */
073public class Remainder extends Transformer {
074    /** Construct an actor with the given container and name.
075     *  @param container The container.
076     *  @param name The name of this actor.
077     *  @exception IllegalActionException If the actor cannot be contained
078     *   by the proposed container.
079     *  @exception NameDuplicationException If the container already has an
080     *   actor with this name.
081     */
082    public Remainder(CompositeEntity container, String name)
083            throws NameDuplicationException, IllegalActionException {
084        super(container, name);
085
086        input.setTypeEquals(BaseType.DOUBLE);
087        output.setTypeEquals(BaseType.DOUBLE);
088
089        divisor = new Parameter(this, "divisor", new DoubleToken(1.0));
090    }
091
092    ///////////////////////////////////////////////////////////////////
093    ////                     ports and parameters                  ////
094
095    /** The divisor for calculating the remainder.
096     *  This is a double with default value 1.0.
097     */
098    public Parameter divisor;
099
100    ///////////////////////////////////////////////////////////////////
101    ////                         public methods                    ////
102
103    /** Consume at most one input token and output the remainder after
104     *  dividing the input by the divisor.
105     *  If there is no input token, then no output is produced.
106     *  @exception IllegalActionException If there is no director.
107     */
108    @Override
109    public void fire() throws IllegalActionException {
110        super.fire();
111        if (input.hasToken(0)) {
112            double in = ((DoubleToken) input.get(0)).doubleValue();
113            double divisorValue = ((DoubleToken) divisor.getToken())
114                    .doubleValue();
115            output.send(0,
116                    new DoubleToken(Math.IEEEremainder(in, divisorValue)));
117        }
118    }
119}