;N int.cam - a small CAMILA library for small integers ;A J.N. Oliveira (jno@di.uminho.pt) ;D ; This library provides a few functions useful in simple ; integer arithmetics. Some functions in this library are obsolete. ; The CAMILA type INT ranges from -maxint to maxint. ; For long integer arithmetics other data types are required. ; Last Update: 1999.11.11 ;E FUNC intSUM(l:INT-seq):INT ; ; intSUM(l) sums sequence l of integers. ; RETURN add-orio(0,l); FUNC intMIN(s:INT-set):INT ; ; intMIN(s) computes minimum of a set of INT. ; RETURN min-orio(32767,s); FUNC intMAX(s:INT-set):INT ; ; intMIN(s) computes maximum of a set of INT. ; RETURN max-orio(0,s); FUNC intMUL(l:INT-seq):INT ; ; intMUL(l) reduces sequence l of integers via multiplication. ; RETURN mul-orio(1,l); FUNC intPot(a:INT,b:INT):INT ; ; intPot(a,b) computes a^b. ; RETURN let (l=< a | i <- inseg(b) >) in intMUL(l); FUNC intSet2IncIntSeq(s:INT-set):INT-seq ; ; intSet2IncIntSeq(s) sorts s in INT-increasing order. ; RETURN if s=={} then <> else let (m=intMIN(s), t={ x | x <- s: x != m}) in ^intSet2IncIntSeq(t); FUNC intSet2IntSeq(s:INT-set):INT-seq ; ; intSet2IntSeq(s) sorts s in INT-decreasing order. ; RETURN if s=={} then <> else let (m=intMAX(s), t={ x | x <- s: x != m}) in ^intSet2IntSeq(t); FUNC intFF2index(ff:A->INT):A-seq ; ; intFF2index(ff) builds index of ff (range INT-increasing ordering). ; RETURN if ff==[] then <> else let (m=intMIN(ran(ff)), X={ a | a <- dom(ff): ff[a] == m }, l=< a | a <- X >) in l ^ intFF2index(ff\X); FUNC intAsRealRound(n:INT,i:INT):INT ; ; intAsRealRound(n,i) regards n as a real number with i decimal places ; and rounds it. ; RETURN let (p=intPot(10,i), d= n ./ p, r= rem(n,p)) in if (r < p./2) then d else d .+ 1; FUNC intPerc(x:INT,y:INT,i:INT):INT ; ; intPerc(x,y,i) computes x as percentage of y with i decimal places ; RETURN let (p=intPot(10,i.+2)) in (x.*p)./y; FUNC intAsReal2STR(x:INT,i:INT):STR ; ; intAsReal2STR(n,i) regards n as a real number with i decimal places ; and converts into to a string. ; RETURN if i==0 then itoa(x) else let (n=if x < 0 then 0.-x else x, dig= lambda(i,n).rem(div(n,intPot(10,i.-1)),10), r = seqInv(< dig(j,n) | j <- inseg(i) > )) in (if x <0 then "-" else "") ++ itoa(div(n,intPot(10,i))) ++ "." ++ strCAT(itoa-seq(r)); FUNC intAvg(l:INT-seq,p:INT):INT ; ; intAvg(l,p) averages sequence l of integers, p adds decimal precision. ; RETURN let (a=length(l), b=intSUM(l) .* intPot(10,p)) in b ./ a;