<commit_before>package ceylon.language;

import com.redhat.ceylon.compiler.java.metadata.Ceylon;
import com.redhat.ceylon.compiler.java.metadata.Class;
import com.redhat.ceylon.compiler.java.metadata.Ignore;
import com.redhat.ceylon.compiler.java.metadata.Name;
import com.redhat.ceylon.compiler.java.metadata.SatisfiedTypes;
import com.redhat.ceylon.compiler.java.metadata.TypeInfo;

@Ceylon
@SatisfiedTypes({
    "ceylon.language.Castable<ceylon.language.Integer|ceylon.language.Float>",
    "ceylon.language.Integral<ceylon.language.Integer>"
})
@Class(extendsType="ceylon.language.Object")
public final class Integer
    implements Castable<Numeric>, Integral<Integer> {
    
    final long value;
    private Integer(long l) {
        value = l;
    }
    
    public static Integer instance(long l) {
        return new Integer(l);
    }
    
    @Override
    public Integer plus(@Name("other") Integer op) {
        return instance(value + op.value);
    }
    
    @Override
    public Integer minus(@Name("other") Integer op) {
        return instance(value - op.value);
    }
    
    @Override
    public Integer times(@Name("other") Integer op) {
        return instance(value * op.value);
    }
    
    @Override
    public Integer divided(@Name("other") Integer op) {
        return instance(value / op.value);
    }
    
    private static final long POWER_BY_SQUARING_BREAKEVEN = 6;
    
    private static long powerBySquaring(long base, long power) {
        long result = 1;
        long x = base;
        while (power != 0) {
            if ((power & 1L) == 1L) {
                result *= x;
                power -= 1;
            }
            x *= x;
            power /= 2;
        }
        return result;
    }
    
    private static long powerByMultiplying(long base, long power) {
        long result = 1;
        while (power > 0) {
            result *= base;
            power--;
        }
        return result;
    }
    
    @Override
    public Integer power(@Name("other") Integer op) {
        long power = op.value;
        if (this.value == -1) {
            return instance(power % 2 == 0 ? 1 : -1);
        } else if (this.value == 1) {
            return instance(1L);
        }
        if (power < 0) {
            throw new RuntimeException(this.value + "**" + power + " cannot be represented as an Integer");
        } else if (power == 0L) {
            return instance(1L);
        }
        if (power >= POWER_BY_SQUARING_BREAKEVEN) {
            return instance(powerBySquaring(this.value, power));
        } else {
            return instance(powerByMultiplying(this.value, power));
        }   
    }
    
    @Ignore
    public Float plus(Float op) {
        return Float.instance(value + op.value);
    }
    
    @Ignore
    public Float minus(Float op) {
        return Float.instance(value - op.value);
    }
    
    @Ignore
    public Float times(Float op) {
        return Float.instance(value * op.value);
    }
    
    @Ignore
    public Float divided(Float op) {
        return Float.instance(value / op.value);
    }
    
    @Ignore
    public Float power(Float op) {
        return Float.instance(Math.pow(value, op.value)); // FIXME: ugly
    }
    
    @Override
    public Integer getMagnitude() {		
        return instance(Math.abs(value));
    }
    
    @Override
    public Integer getFractionalPart() {		
        return instance(0);
    }
    
    @Override
    public Integer getWholePart() {		
        return this;
    }
    
    @Override
    public boolean getPositive() {
        return value > 0;
    }
    
    @Override
    public boolean getNegative() {
        return value < 0;
    }
    
    @Override
    public long getSign() {
        if (value > 0)
            return 1;
        if (value < 0)
            return -1;
        return 0;
    }
    
    @Override
    public Integer remainder(@Name("other") Integer op) {
        return instance(value % op.value);
    }
    
    @Override
    public Integer getPositiveValue() {
        return this;
    }
    
    @Override
    public Integer getNegativeValue() {
        return instance(-value);
    }
    
    public boolean test(Integer op) {
        return value == op.value;
    }
    
    @Override
    public Comparison compare(@Name("other") Integer op) {
        long x = value;
        long y = op.value;
        return (x < y) ? smaller.getSmaller() :
            ((x == y) ? equal.getEqual() : larger.getLarger());
    }
    
    @Override
    public java.lang.String toString() {
        return java.lang.Long.toString(value);
    }
    
    // Conversions between numeric types
    
    @TypeInfo(value="ceylon.language.Integer")
    @Override
    public long getInteger() {
        return value;
    }
    
    @TypeInfo(value="ceylon.language.Float")
    @Override
    public double getFloat() {
        return (double) value;
    }
    
    @Override
    public boolean getUnit() {
        return value==1;
    }
    
    @Override
    public boolean getZero() {
        return value==0;
    }
    
    // Just a kludge til we have full autoboxing
    public long longValue() {
        return value;
    }<commit_msg>Further to #51 perform an overflow check in Integer.float<commit_after>package ceylon.language;

import com.redhat.ceylon.compiler.java.metadata.Ceylon;
import com.redhat.ceylon.compiler.java.metadata.Class;
import com.redhat.ceylon.compiler.java.metadata.Ignore;
import com.redhat.ceylon.compiler.java.metadata.Name;
import com.redhat.ceylon.compiler.java.metadata.SatisfiedTypes;
import com.redhat.ceylon.compiler.java.metadata.TypeInfo;

@Ceylon
@SatisfiedTypes({
    "ceylon.language.Castable<ceylon.language.Integer|ceylon.language.Float>",
    "ceylon.language.Integral<ceylon.language.Integer>"
})
@Class(extendsType="ceylon.language.Object")
public final class Integer
    implements Castable<Numeric>, Integral<Integer> {
    
    final long value;
    private Integer(long l) {
        value = l;
    }
    
    public static Integer instance(long l) {
        return new Integer(l);
    }
    
    @Override
    public Integer plus(@Name("other") Integer op) {
        return instance(value + op.value);
    }
    
    @Override
    public Integer minus(@Name("other") Integer op) {
        return instance(value - op.value);
    }
    
    @Override
    public Integer times(@Name("other") Integer op) {
        return instance(value * op.value);
    }
    
    @Override
    public Integer divided(@Name("other") Integer op) {
        return instance(value / op.value);
    }
    
    private static final long POWER_BY_SQUARING_BREAKEVEN = 6;
    
    private static long powerBySquaring(long base, long power) {
        long result = 1;
        long x = base;
        while (power != 0) {
            if ((power & 1L) == 1L) {
                result *= x;
                power -= 1;
            }
            x *= x;
            power /= 2;
        }
        return result;
    }
    
    private static long powerByMultiplying(long base, long power) {
        long result = 1;
        while (power > 0) {
            result *= base;
            power--;
        }
        return result;
    }
    
    @Override
    public Integer power(@Name("other") Integer op) {
        long power = op.value;
        if (this.value == -1) {
            return instance(power % 2 == 0 ? 1 : -1);
        } else if (this.value == 1) {
            return instance(1L);
        }
        if (power < 0) {
            throw new RuntimeException(this.value + "**" + power + " cannot be represented as an Integer");
        } else if (power == 0L) {
            return instance(1L);
        }
        if (power >= POWER_BY_SQUARING_BREAKEVEN) {
            return instance(powerBySquaring(this.value, power));
        } else {
            return instance(powerByMultiplying(this.value, power));
        }   
    }
    
    @Ignore
    public Float plus(Float op) {
        return Float.instance(value + op.value);
    }
    
    @Ignore
    public Float minus(Float op) {
        return Float.instance(value - op.value);
    }
    
    @Ignore
    public Float times(Float op) {
        return Float.instance(value * op.value);
    }
    
    @Ignore
    public Float divided(Float op) {
        return Float.instance(value / op.value);
    }
    
    @Ignore
    public Float power(Float op) {
        return Float.instance(Math.pow(value, op.value)); // FIXME: ugly
    }
    
    @Override
    public Integer getMagnitude() {		
        return instance(Math.abs(value));
    }
    
    @Override
    public Integer getFractionalPart() {		
        return instance(0);
    }
    
    @Override
    public Integer getWholePart() {		
        return this;
    }
    
    @Override
    public boolean getPositive() {
        return value > 0;
    }
    
    @Override
    public boolean getNegative() {
        return value < 0;
    }
    
    @Override
    public long getSign() {
        if (value > 0)
            return 1;
        if (value < 0)
            return -1;
        return 0;
    }
    
    @Override
    public Integer remainder(@Name("other") Integer op) {
        return instance(value % op.value);
    }
    
    @Override
    public Integer getPositiveValue() {
        return this;
    }
    
    @Override
    public Integer getNegativeValue() {
        return instance(-value);
    }
    
    public boolean test(Integer op) {
        return value == op.value;
    }
    
    @Override
    public Comparison compare(@Name("other") Integer op) {
        long x = value;
        long y = op.value;
        return (x < y) ? smaller.getSmaller() :
            ((x == y) ? equal.getEqual() : larger.getLarger());
    }
    
    @Override
    public java.lang.String toString() {
        return java.lang.Long.toString(value);
    }
    
    // Conversions between numeric types
    
    @TypeInfo(value="ceylon.language.Integer")
    @Override
    public long getInteger() {
        return value;
    }
    
    @TypeInfo(value="ceylon.language.Float")
    @Override
    public double getFloat() {
        if (value >= 9007199254740992L
                || value <= -9007199254740992L) {
            throw new OverflowException();
        }
        return (double) value;
    }
    
    @Override
    public boolean getUnit() {
        return value==1;
    }
    
    @Override
    public boolean getZero() {
        return value==0;
    }
    
    // Just a kludge til we have full autoboxing
    public long longValue() {
        return value;
    }
{'fraction_non_alphanumeric': 0.10912886695767698, 'fraction_numerical': 0.008730309356614159, 'mean_word_length': 1.885815991237678, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '913507', 'n_tokens_mistral': 3196, 'n_tokens_neox': 3085, 'n_words': 764}