<commit_before>package de.lmu.ifi.dbs.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Random;

/**
 * RationalNumber represents rational numbers in arbitrary precision.
 * Note that the best possible precision is the primary objective
 * of this class. Since numerator and denominator of the RationalNumber
 * are represented as BigIntegers, the required space can grow unlimited.
 * Also arithmetic operations are considerably less efficient compared to
 * the operations with doubles.
 * 
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 */
public class RationalNumber extends Number implements Arithmetic<RationalNumber>
{
    /**
     * Generated serial version UID. 
     */
    private static final long serialVersionUID = 7347098153261459646L;
    
    /**
     * The canonical representation of zero as RationalNumber. 
     */
    public static final RationalNumber ZERO = new RationalNumber(BigInteger.ZERO,BigInteger.ONE);
    
    /**
     * The canonical representation of 1 as RationalNumber.
     */
    public static final RationalNumber ONE = new RationalNumber(BigInteger.ONE,BigInteger.ONE);

    /**
     * Holding the numerator of the RationalNumber.
     */
    private BigInteger numerator;
    
    /**
     * Holding the denominator of the RationalNumber.
     */
    private BigInteger denominator;

    /**
     * Constructs a RationalNumber for a given numerator and denominator.
     * The denominator must not be 0.
     * 
     * @param numerator the numerator of the RationalNumber
     * @param denominator the denominator of the RationalNumber
     * @throws IllegalArgumentException if <code>denominator.equals(BigInteger.ZERO)</code>
     */
    public RationalNumber(final BigInteger numerator, final BigInteger denominator) throws IllegalArgumentException
    {
        if(denominator.equals(BigInteger.ZERO))
        {
            throw new IllegalArgumentException("denominator is 0");
        }
        this.numerator = new BigInteger(numerator.toByteArray());
        this.denominator = new BigInteger(denominator.toByteArray());
        normalize();
    }
    
    
    /**
     * Constructs a RationalNumber for a given numerator and denominator.
     * The denominator must not be 0.
     * 
     * @param numerator the numerator of the RationalNumber
     * @param denominator the denominator of the RationalNumber
     * @throws IllegalArgumentException if <code>denominator.equals(BigInteger.ZERO)</code>
     */
    public RationalNumber(final long numerator, final long denominator) throws IllegalArgumentException
    {
        if(denominator==0)
        {
            throw new IllegalArgumentException("denominator is 0");
        }
        this.numerator = BigInteger.valueOf(numerator);
        this.denominator = BigInteger.valueOf(denominator);
        normalize();
    }
    
    /**
     * Constructs a RationalNumber out of the given double number.
     * 
     * @param number a double number to be represented as a RationalNumber
     * @throws IllegalArgumentException if the given String represents a doubel number that is infinit or not a number
     */
    public RationalNumber(final Double number) throws IllegalArgumentException
    {
        this(number.toString());
    }
    
    /**
     * Constructs a RationalNumber for a given String
     * representing a double.
     * 
     * @param doubleString a String representing a double number
     * @throws IllegalArgumentException if the given String represents a doubel number that is infinit or not a number
     */
    public RationalNumber(final String doubleString) throws IllegalArgumentException
    {
        try
        {
            Double number = Double.parseDouble(doubleString);
            if(number.isInfinite())
            {
                throw new IllegalArgumentException("given number is infinite");
            }
            if(number.isNaN())
            {
                throw new IllegalArgumentException("given number is NotANumber");
            }
            // ensure standard encoding of the double argument
            String standardDoubleString = number.toString();
            // index of decimal point '.'
            int pointIndex = standardDoubleString.indexOf('\u002E');
            // read integer part
            String integerPart = pointIndex == -1 ? standardDoubleString : standardDoubleString.substring(0,pointIndex);
            // index of power 'E'
            int powerIndex = standardDoubleString.indexOf('\u0045');
            // read fractional part
            String fractionalPart = powerIndex == -1 ? standardDoubleString.substring(pointIndex+1) : standardDoubleString.substring(pointIndex+1, powerIndex);
            // read power
            int power = powerIndex == -1 ? 0 : Integer.parseInt(standardDoubleString.substring(powerIndex+1));
            // concatenate integer part and fractional part to numerator<commit_msg>comments<commit_after>package de.lmu.ifi.dbs.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Random;

/**
 * RationalNumber represents rational numbers in arbitrary precision.
 * Note that the best possible precision is the primary objective
 * of this class. Since numerator and denominator of the RationalNumber
 * are represented as BigIntegers, the required space can grow unlimited.
 * Also arithmetic operations are considerably less efficient compared to
 * the operations with doubles.
 * 
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 */
public class RationalNumber extends Number implements Arithmetic<RationalNumber>
{
    /**
     * Generated serial version UID. 
     */
    private static final long serialVersionUID = 7347098153261459646L;
    
    /**
     * The canonical representation of zero as RationalNumber. 
     */
    public static final RationalNumber ZERO = new RationalNumber(BigInteger.ZERO,BigInteger.ONE);
    
    /**
     * The canonical representation of 1 as RationalNumber.
     */
    public static final RationalNumber ONE = new RationalNumber(BigInteger.ONE,BigInteger.ONE);

    /**
     * Holding the numerator of the RationalNumber.
     */
    private BigInteger numerator;
    
    /**
     * Holding the denominator of the RationalNumber.
     */
    private BigInteger denominator;

    /**
     * Constructs a RationalNumber for a given numerator and denominator.
     * The denominator must not be 0.
     * 
     * @param numerator the numerator of the RationalNumber
     * @param denominator the denominator of the RationalNumber
     * @throws IllegalArgumentException if {@link BigInteger#equals(java.lang.Object) denominator.equals(}{@link BigInteger#ZERO BigInteger.ZERO)}
     */
    public RationalNumber(final BigInteger numerator, final BigInteger denominator) throws IllegalArgumentException
    {
        if(denominator.equals(BigInteger.ZERO))
        {
            throw new IllegalArgumentException("denominator is 0");
        }
        this.numerator = new BigInteger(numerator.toByteArray());
        this.denominator = new BigInteger(denominator.toByteArray());
        normalize();
    }
    
    
    /**
     * Constructs a RationalNumber for a given numerator and denominator.
     * The denominator must not be 0.
     * 
     * @param numerator the numerator of the RationalNumber
     * @param denominator the denominator of the RationalNumber
     * @throws IllegalArgumentException if {@link BigInteger#equals(java.lang.Object) denominator.equals(}{@link BigInteger#ZERO BigInteger.ZERO)}
     */
    public RationalNumber(final long numerator, final long denominator) throws IllegalArgumentException
    {
        if(denominator==0)
        {
            throw new IllegalArgumentException("denominator is 0");
        }
        this.numerator = BigInteger.valueOf(numerator);
        this.denominator = BigInteger.valueOf(denominator);
        normalize();
    }
    
    /**
     * Constructs a RationalNumber out of the given double number.
     * 
     * @param number a double number to be represented as a RationalNumber
     * @throws IllegalArgumentException if the given Double is infinit or not a number
     */
    public RationalNumber(final Double number) throws IllegalArgumentException
    {
        this(number.toString());
    }
    
    /**
     * Constructs a RationalNumber for a given String
     * representing a double.
     * 
     * @param doubleString a String representing a double number
     * @throws IllegalArgumentException if the given String represents a double number that is infinit or not a number
     */
    public RationalNumber(final String doubleString) throws IllegalArgumentException
    {
        try
        {
            Double number = Double.parseDouble(doubleString);
            if(number.isInfinite())
            {
                throw new IllegalArgumentException("given number is infinite");
            }
            if(number.isNaN())
            {
                throw new IllegalArgumentException("given number is NotANumber");
            }
            // ensure standard encoding of the double argument
            String standardDoubleString = number.toString();
            // index of decimal point '.'
            int pointIndex = standardDoubleString.indexOf('\u002E');
            // read integer part
            String integerPart = pointIndex == -1 ? standardDoubleString : standardDoubleString.substring(0,pointIndex);
            // index of power 'E'
            int powerIndex = standardDoubleString.indexOf('\u0045');
            // read fractional part
            String fractionalPart = powerIndex == -1 ? standardDoubleString.substring(pointIndex+1) : standardDoubleString.substring(pointIndex+1, powerIndex);
            // read power
            int power = powerIndex == -1 ? 0 : Integer.parseInt(standardDoubleString.substring(powerIndex+1));
            // concatenate integer part and fractional part to numerator
{'fraction_non_alphanumeric': 0.07708312691964728, 'fraction_numerical': 0.007926285544436739, 'mean_word_length': 2.7357512953367875, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7239010', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2431, 'n_words': 800}