<commit_before>! Copyright (C) 2004, 2008 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel kernel.private math math.functions
math.private sequences summary ;
IN: math.ratios

: 2>fraction ( a/b c/d -- a c b d )
    [ >fraction ] bi@ swapd ; inline

<PRIVATE

: fraction> ( a b -- a/b )
    dup 1 number= [ drop ] [ ratio boa ] if ; inline

: scale ( a/b c/d -- a*d b*c )
    2>fraction [ * swap ] dip * swap ; inline

: ratio+d ( a/b c/d -- b*d )
    [ denominator ] bi@ * ; inline

PRIVATE>

ERROR: division-by-zero ;

M: division-by-zero summary
    drop "Division by zero" ;

M: integer /
    [
        division-by-zero
    ] [
        dup 0 < [ [ neg ] bi@ ] when
        2dup gcd nip [ /i ] curry bi@ fraction>
    ] if-zero ;

M: ratio hashcode*
    nip >fraction [ hashcode ] bi@ bitxor ;

M: ratio equal?<commit_msg>even better error handling for division by zero<commit_after>! Copyright (C) 2004, 2008 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel kernel.private math math.functions
math.private sequences summary ;
IN: math.ratios

: 2>fraction ( a/b c/d -- a c b d )
    [ >fraction ] bi@ swapd ; inline

<PRIVATE

: fraction> ( a b -- a/b )
    dup 1 number= [ drop ] [ ratio boa ] if ; inline

: scale ( a/b c/d -- a*d b*c )
    2>fraction [ * swap ] dip * swap ; inline

: ratio+d ( a/b c/d -- b*d )
    [ denominator ] bi@ * ; inline

PRIVATE>

ERROR: division-by-zero x ;

M: division-by-zero summary
    drop "Division by zero" ;

M: integer /
    [
        division-by-zero
    ] [
        dup 0 < [ [ neg ] bi@ ] when
        2dup gcd nip [ /i ] curry bi@ fraction>
    ] if-zero ;

M: ratio hashcode*
    nip >fraction [ hashcode ] bi@ bitxor ;

M: ratio equal?
{'fraction_non_alphanumeric': 0.13816534541336353, 'fraction_numerical': 0.014722536806342015, 'mean_word_length': 2.3657142857142857, 'pattern_counts': {'":': 0, '<': 9, '<?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': '339320', 'n_tokens_mistral': 671, 'n_tokens_neox': 632, 'n_words': 207}