PreviousUpNext

15.4.1071  src/lib/std/src/int32.pkg

## int32.pkg

# Compiled by:
#     src/lib/std/src/standard-core.sublib

package int32_guts: (weak)  Int {               # Int           is from   src/lib/std/src/int.api
                                                # inline_t      is from   src/lib/core/init/built-in.pkg
    package i32= inline_t::int32;

    Int = int32::Int;

    precision = THE 32;

    my min_int_val:  Int   = -2147483648;

    my min_int:  Null_Or(  Int )   = THE min_int_val;
    my max_int:  Null_Or(  Int )   = THE 2147483647;

    my (-_)    : Int -> Int           = i32::neg ;
    my neg     : Int -> Int           = i32::neg ;
    my abs     : Int -> Int           = i32::abs ;

    my (+)    : (Int, Int) -> Int    = i32::(+) ;
    my (-)    : (Int, Int) -> Int    = i32::(-) ;
    my (*)    : (Int, Int) -> Int    = i32::(*) ;

    my (/)    : (Int, Int) -> Int    = i32::div ;
    my (%)    : (Int, Int) -> Int    = i32::mod ;

    my    quot : (Int, Int) -> Int    = i32::quot;
    my    rem  : (Int, Int) -> Int    = i32::rem ;

    my (<)    : (Int, Int) -> Bool   = i32::(<) ;
    my (<=)   : (Int, Int) -> Bool   = i32::(<=);
    my (>)    : (Int, Int) -> Bool   = i32::(>) ;
    my (>=)   : (Int, Int) -> Bool   = i32::(>=);

    my min     : (Int, Int) -> Int    = i32::min ;
    my max     : (Int, Int) -> Int    = i32::max ;

    fun sign  0 =>  0;
        sign  i =>  if (i32::(<) (i, 0) ) -1;
                    else                   1;
                    fi;
    end;

    fun 0! =>  1;
        n! =>  n * (n - 1)! ;
    end;

    fun same_sign (i, j)
        =
        i32::bitwise_and (i32::bitwise_xor (i, j), min_int_val) == 0;

    fun compare ( i: Int,
                  j: Int
                )
        =
        if       (i32::(<) (i, j)  )   exceptions::LESS;
        else if  (i32::(>) (i, j)  )   exceptions::GREATER;
        else                           exceptions::EQUAL;   fi; fi;

    fun is_prime p                      # A very simple and naive primality tester.  2009-09-02 CrT.
        =
        {   p = abs(p);                 # Try to do something reasonable with negative numbers.

            if   (p < 4)       TRUE;    # Call zero prime.
            elif (p % 2 == 0)  FALSE;   # Special-case even numbers to halve our loop time.
            else
                # Test all odd numbers less than sqrt(p):

                loop 3
                where
                    fun loop i
                        =
                        if   (p % i == 0)   FALSE;
                        elif (i*i >= p)     TRUE;
                        else                loop (i + 2);
                        fi;
                end;
            fi;
        };

    fun factors n
        =
        factors' (n, 2, [])
        where
            fun factors' (n, p, results)
                =
                if (p*p > n)

                    reverse (n ! results);

                elif (n % p == 0)

                   factors' (n/p, p,   p ! results);

                else

                   factors' (n,   p+1,     results);
                fi;
        end;

    fun sum ints
        =
        sum' (ints, 0)
        where
            fun sum' (      [], result) =>  result;
                sum' (i ! rest, result) =>  sum' (rest, i + result);
            end;
        end;

    fun product ints
        =
        product' (ints, 1)
        where
            fun product' (      [], result) =>  result;
                product' (i ! rest, result) =>  product' (rest, i * result);
            end;
        end;

    scan   =  num_scan::scan_int;
    format =  num_format::format_int;

    to_string   =  format number_string::DECIMAL;
    from_string =  pre_basis::scan_string (scan number_string::DECIMAL); 

    my to_int:    Int -> int::Int   = i32::to_int;
    my from_int:  int::Int -> Int   = i32::from_int;

    my to_large:    Int -> large_int::Int   = i32::to_large;
    my from_large:  large_int::Int -> Int   = i32::from_large;

};




## COPYRIGHT (c) 1995 AT&T Bell Laboratories.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2011,
## released under Gnu Public Licence version 3.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext