<commit_before>
import { DEFAULT_DISPLAY_BASE , ZeroDivisionError } from './' ;

import {
	stringify , convert , _trim_positive ,
	_alloc , _copy , _zeros ,
	_jz , _cmp , _eq ,
	_add , _sub , _mul , _div , _powd ,
	_increment ,
} from '@aureooms/js-integer-big-endian' ;

export class Integer {

	constructor ( base , is_negative , limbs ) {
		this.base = base ;
		this.is_negative = is_negative ;
		this.limbs = limbs ;
	}

	move ( other ) {
		other.base = this.base ;
		other.is_negative = this.is_negative ;
		other.limbs = this.limbs ;
		return other ;
	}

	copy ( ) {
		return new Integer( this.base , this.is_negative , this.limbs ) ;
	}

	_limbs_in_base ( base ) {
		if ( this.base === base ) return this.limbs ;
		else return convert( this.base , base , this.limbs , 0 , this.limbs.length ) ;
	}

	toString ( base = DEFAULT_DISPLAY_BASE ) {

		if ( this.iszero( ) ) return '0' ;

		const digits = stringify( this.base , base , this.limbs , 0 , this.limbs.length ) ;

		return this.is_negative ? '-' + digits : digits ;

	}

	add ( other ) {

		if ( this.is_negative !== other.is_negative ) {

			if ( other.is_negative ) return this.sub( other.opposite() ) ;

			else return other.sub( this.opposite() ) ;

		}

		else {

			const result_is_negative = this.is_negative ;
			const r = this.base ;

			const a = this.limbs ;

			const b = other._limbs_in_base( r ) ;

			const c = _zeros( Math.max( a.length , b.length ) + 1 ) ;

			_add( r , a , 0 , a.length , b , 0 , b.length , c , 0 , c.length ) ;

			return new Integer( r , result_is_negative , c ) ;

		}

	}

	iadd ( other ) {
		// TODO optimize but be careful with side effects
		return this.add(other).move(this);
	}

	sub ( other ) {

		if ( this.is_negative !== other.is_negative ) {

			if ( other.is_negative ) return this.add( other.opposite() ) ;

			else return this.opposite().add( other ).opposite() ;

		}

		else {

			const r = this.base ;
			const a = this.limbs ;

			const b = other._limbs_in_base( r ) ;

			const c = _zeros( Math.max( a.length , b.length ) ) ;

			if ( _cmp( a , 0 , a.length , b , 0 , b.length ) < 0 ) {

				_sub( r , b , 0 , b.length , a , 0 , a.length , c , 0 , c.length ) ;

				return new Integer( r , ~this.is_negative , c ) ;
			}

			else {

				_sub( r , a , 0 , a.length , b , 0 , b.length , c , 0 , c.length ) ;

				return new Integer( r , this.is_negative , c ) ;

			}

		}

	}

	isub ( other ) {
		// TODO optimize but be careful with side effects
		return this.sub(other).move(this);
	}<commit_msg>fix issue in Integer#sub<commit_after>import { DEFAULT_DISPLAY_BASE , ZeroDivisionError } from './' ;

import {
	stringify , convert , _trim_positive ,
	_alloc , _copy , _zeros ,
	_jz , _cmp , _eq ,
	_add , _sub , _mul , _div , _powd ,
	_increment ,
} from '@aureooms/js-integer-big-endian' ;

export class Integer {

	constructor ( base , is_negative , limbs ) {
		this.base = base ;
		this.is_negative = is_negative ;
		this.limbs = limbs ;
	}

	move ( other ) {
		other.base = this.base ;
		other.is_negative = this.is_negative ;
		other.limbs = this.limbs ;
		return other ;
	}

	copy ( ) {
		return new Integer( this.base , this.is_negative , this.limbs ) ;
	}

	_limbs_in_base ( base ) {
		if ( this.base === base ) return this.limbs ;
		else return convert( this.base , base , this.limbs , 0 , this.limbs.length ) ;
	}

	toString ( base = DEFAULT_DISPLAY_BASE ) {

		if ( this.iszero( ) ) return '0' ;

		const digits = stringify( this.base , base , this.limbs , 0 , this.limbs.length ) ;

		return this.is_negative ? '-' + digits : digits ;

	}

	add ( other ) {

		if ( this.is_negative !== other.is_negative ) {

			if ( other.is_negative ) return this.sub( other.opposite() ) ;

			else return other.sub( this.opposite() ) ;

		}

		else {

			const result_is_negative = this.is_negative ;
			const r = this.base ;

			const a = this.limbs ;

			const b = other._limbs_in_base( r ) ;

			const c = _zeros( Math.max( a.length , b.length ) + 1 ) ;

			_add( r , a , 0 , a.length , b , 0 , b.length , c , 0 , c.length ) ;

			return new Integer( r , result_is_negative , c ) ;

		}

	}

	iadd ( other ) {
		// TODO optimize but be careful with side effects
		return this.add(other).move(this);
	}

	sub ( other ) {

		if ( this.is_negative !== other.is_negative ) {

			if ( other.is_negative ) return this.add( other.opposite() ) ;

			else return this.opposite().add( other ).opposite() ;

		}

		else {

			// /!\ _sub needs |c| >= |a| >= |b|

			const r = this.base ;
			const a = this.limbs ;
			const aj = a.length ;
			const ai = _trim_positive( a , 0 , aj ) ;

			if ( ai >= aj ) return other.opposite() ;

			const b = other._limbs_in_base( r ) ;
			const bj = b.length ;
			const bi = _trim_positive( b , 0 , bj ) ;

			if ( bi >= bj ) return this.copy() ;

			if ( _cmp( a , ai , aj , b , bi , bj ) < 0 ) {

				const c = _zeros( bj - bi ) ;

				_sub( r , b , bi , bj , a , ai , aj , c , 0 , c.length ) ;

				return new Integer( r , ~this.is_negative , c ) ;
			}

			else {

				const c = _zeros( aj - ai ) ;

				_sub( r , a , ai , aj , b , bi , bj , c , 0 , c.length ) ;

				return new Integer( r , this.is_negative , c ) ;

			}

		}

	}

	isub ( other ) {
		// TODO optimize but be careful with side effects
		return this.sub(other).move(this);
	}
{'fraction_non_alphanumeric': 0.1425600302514653, 'fraction_numerical': 0.005294006428436377, 'mean_word_length': 2.404118404118404, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3774790', 'n_tokens_mistral': 2245, 'n_tokens_neox': 1941, 'n_words': 517}