Changeset 1814


Ignore:
Timestamp:
Dec 22, 2005, 5:49:20 PM (15 years ago)
Author:
orlov
Message:
  • Removed argument lengths checks and other improvements.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/java/org/refal/plus/library/Arithm.java

    r1811 r1814  
    66
    77import org.refal.plus.*;
     8import java.math.BigInteger;
    89
    910public class Arithm {
    1011    /**
    11      * Refal expresions `s1` and `s2` must contain one of the numeric classes (Integer, Short, Long,
    12      * Byte, BigInteger)
     12     * Refal expresions `s1` and `s2` must contain one of the numeric classes
     13     * (Integer, Short, Long, Byte, BigInteger)
    1314     *
    1415     * @param s1 -- Refal expression.
     
    2930
    3031    /**
    31      * Refal expresions `s1` and `s2` must contain one of the numeric classes (Integer, Short, Long,
    32      * Byte, BigInteger)
     32     * Refal expresions `s1` and `s2` must contain one of the numeric classes
     33     * (Integer, Short, Long, Byte, BigInteger)
    3334     *
    3435     * @param s1 -- Refal expression.
     
    4950
    5051    /**
    51      * Refal expresions `s1` and `s2` must contain one of the numeric classes (Integer, Short, Long,
    52      * Byte, BigInteger)
     52     * Refal expresions `s1` and `s2` must contain one of the numeric classes
     53     * (Integer, Short, Long, Byte, BigInteger)
    5354     *
    5455     * @param s1 -- Refal expression.
     
    6970
    7071    /**
    71      * Refal expresions `s1` and `s2` must contain one of the numeric classes (Integer, Short, Long,
    72      * Byte, BigInteger)
     72     * Refal expresions `s1` and `s2` must contain one of the numeric classes
     73     * (Integer, Short, Long, Byte, BigInteger)
    7374     *
    7475     * @param s1 -- Refal expression.
     
    9192
    9293    /**
    93      * Refal expresions `s1` and `s2` must contain one of the numeric classes (Integer, Short, Long,
    94      * Byte, BigInteger)
     94     * Refal expresions `s1` and `s2` must contain one of the numeric classes
     95     * (Integer, Short, Long, Byte, BigInteger)
    9596     *
    9697     * @param s1 -- Refal expression.
     
    100101     */
    101102    static public void Rem(Expr s1, Expr s2, Expr res) throws RefalException {
    102         if (s1.getLen() != 1 || s2.getLen() != 1) {
    103             throw new RefalException("Arithm", "Rem", "Invalid argument");
    104         }
     103        // FIXME: insert here assertion s1.getLen() == 1
     104        // FIXME: insert here assertion s2.getLen() == 1
    105105        try {
    106106            res.assign(new Expr(Int.toBigInteger(s1.at(0)).remainder(
     
    111111               
    112112    }
     113
    113114    /**
    114      * Refal expresions `s1` and `s2` must contain one of the numeric classes (Integer, Short, Long,
    115      * Byte, BigInteger)
     115     * Refal expresions `s1` and `s2` must contain one of the numeric classes
     116     * (Integer, Short, Long, Byte, BigInteger)
    116117     *
    117118     * @param s1 -- Refal expression.
     
    120121     * @throws RefalException
    121122     */
    122     static public void Div_m_Rem (Expr s1, Expr s2, Expr res1, Expr res2) throws RefalException {
    123         if (s1.getLen() != 1 || s2.getLen() != 1) {
    124             throw new RefalException("Arithm", "Div_m_Rem", "Invalid argument");
    125         }
     123    static public void Div_m_Rem (Expr s1, Expr s2, Expr res1, Expr res2)
     124        throws RefalException
     125    {
     126        // FIXME: insert here assertion s1.getLen() == 1
     127        // FIXME: insert here assertion s2.getLen() == 1
    126128        try {
    127             res1.assign(new Expr(Int.toBigInteger(s1.at(0)).divide(
    128                     Int.toBigInteger(s2.at(0)))));
     129            BigInteger[] a = Int.toBigInteger(s1.at(0)).divideAndRemainder(
     130                             Int.toBigInteger(s2.at(0)));
     131            res1.assign(new Expr(a[0]));
     132            res2.assign(new Expr(a[1]));
    129133        } catch (ClassCastException e) {
    130             throw new RefalException("Arithm", "Div_m_Rem", "Invalid argument");
     134            throw new RefalException("Arithm", "Div-Rem", "Invalid argument");
    131135        } catch (ArithmeticException e) {
    132             throw new RefalException("Arithm", "Div_m_Rem", "Divide by zero");
     136            throw new RefalException("Arithm", "Div-Rem", "Divide by zero");
    133137        }       
    134         try {
    135             res2.assign(new Expr(Int.toBigInteger(s1.at(0)).remainder(
    136                     Int.toBigInteger(s2.at(0)))));
    137         } catch (ClassCastException e) {
    138             throw new RefalException("Arithm", "Div_m_Rem", "Invalid argument");
    139         }               
    140138    }
    141    
     139
    142140    /**
    143      * Refal expresions `s1` and `s2` must contain one of the numeric classes (Integer, Short, Long,
    144      * Byte, BigInteger)
     141     * Refal expresions `s1` and `s2` must contain one of the numeric classes
     142     * (Integer, Short, Long, Byte, BigInteger)
    145143     *
    146144     * @param s1 -- Refal expression.
     
    150148     */
    151149    static public void GCD (Expr s1, Expr s2, Expr res) throws RefalException {
    152         if (s1.getLen() != 1 || s2.getLen() != 1) {
    153             throw new RefalException("Arithm", "GCD", "Invalid argument");
    154         }
     150        // FIXME: insert here assertion s1.getLen() == 1
     151        // FIXME: insert here assertion s2.getLen() == 1
    155152        try {
    156             res.assign(new Expr(Int.toBigInteger(s1.at(0)).gcd(
    157                     Int.toBigInteger(s2.at(0)))));
     153            BigInteger d = Int.toBigInteger(s1.at(0)).gcd(
     154                           Int.toBigInteger(s2.at(0)));
     155            if (d.equals(BigInteger.ZERO))
     156                throw new RefalException("Arithm", "GCD", "Zero arguments");
     157            res.assign(new Expr(d));
    158158        } catch (ClassCastException e) {
    159159            throw new RefalException("Arithm", "GCD", "Invalid argument");
Note: See TracChangeset for help on using the changeset viewer.