number.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
---------------------------> Sather 1.1 source file <--------------------------
-- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
-- Copyright (C) 1995, International Computer Science Institute
-- $Id: number.sa,v 1.6 1996/07/25 19:17:22 gomes Exp $
-- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
-- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
-- LICENSE contained in the file: Sather/Doc/License of the
-- Sather distribution. The license is also available from ICSI,
-- 1947 Center St., Suite 600, Berkeley CA 94704, USA.


abstract class $NFE{NTP} < $NIL,$STR,$IS_EQ

abstract class $NFE{NTP} < $NIL,$STR,$IS_EQ is -- Abstract class defined over numeric field elements. Due to -- contravariance, we need to parametrize over NTP which is the -- type of the argument to many of the defined routines. Note that -- this abstraction is not under $IS_LT{T}, since elements such as -- complex numbers are incomparable -- -- The subtyping structure is: -- $NFE{T} --- $NUMBER{T} ------ $REAL_NUMBER{T} -- FLT -- -- FLTD -- ------ INT -- --- $CPX_NUMBER{ETP,T} --- CPX zero: NTP; -- Return the zero value maxval: NTP;; -- Return the maximal allowed value one: NTP; -- Return a unit value create(v: INT): SAME; -- Create a number from the floating point value. There might -- be some loss or gain of precision. create(v: FLT): SAME; -- Create a number from the floating point value. There might -- be some loss or gain of precision. create(v: FLTD): SAME; -- Create from a double value in some reasonable way -- minval: SAME; -- Return the minimal allowed value (not yet defined for FLT) times(n: NTP): SAME; -- Return self * n plus(n: NTP): SAME; -- Return self+n minus(n: NTP): SAME; -- Return self - n div(n: NTP): SAME; -- Return the quotient of self and "n" negate: NTP; -- Return the negation of self abs: NTP; -- Return the absolute value of self end; -- class $NFE

abstract class $NUMBER{NTP} < $IS_LT{NTP},$NFE{NTP}

abstract class $NUMBER{NTP} < $IS_LT{NTP},$NFE{NTP} is -- Abstraction over Real numbers and Integers This abstraction is -- needed to add in the the constraint that numbers are comparable, -- unlike general field elements which might not be. end; -- class $NUMBER

abstract class $REAL_NUMBER{NTP} < $NUMBER{NTP}

abstract class $REAL_NUMBER{NTP} < $NUMBER{NTP} is -- In addition to generic number properties, defines -- functions that are applicable to floating point values exp:NTP; -- Return the exponent of the number cos: NTP; -- Return the cosine of self sin: NTP; -- Return the sine of self sqrt: NTP; -- Return the square root of self atan2(arg: NTP): SAME; -- The arc tangent of self divided by arg in the range [-pi/2, pi/2]. -- It chooses the quadrant specified by (self, arg). acos:SAME; -- The arc cosine of self in the range [0.0 to pi] asin:SAME; -- The arc cosine of self in the range [0.0 to pi] log:SAME; -- Logarithm of self is_nan: BOOL; -- Is not a number. This only makes sense for IEEE real -- numbers. This class may need to be factored if -- we have non-IEEE real numbers at some point end;

abstract class $CPX_NUMBER{ETP<$REAL_NUMBER{ETP},NTP} < $NFE{NTP}

abstract class $CPX_NUMBER{ETP<$REAL_NUMBER{ETP},NTP} < $NFE{NTP} is -- Complex numbers are strictly more general than real numbers. -- The right abstraction would be to force real numbers to povide a -- "zero" real part, but this is quite awkward. It is simpler to -- enforce no relationship, which conforms to the way we seem to -- actually deal with complex and real numbers create(re,im: ETP): SAME; -- Create a complex number with a real "re" and imaginary "im" part create_real(repart: ETP): SAME; -- Create a complex number with real part = "re" and imaginary part -- equal to zero re: ETP; -- Real part im: ETP; -- Imaginary part conjugate: SAME; end;