angle.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
-------------------------> GNU Sather - sourcefile <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute and/or modify it under the terms of the GNU Library General --
-- Public License (LGPL) as published by the Free Software Foundation; --
-- either version 2 of the license, or (at your option) any later version. --
-- This library is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details. --
-- The license text is also available from: Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --
--------------> Please email comments to <bug-sather@gnu.org> <--------------
abstract class $ANGLES{STP, CTP < $COMPLEX{STP,CTP}}
abstract class $ANGLES{STP, CTP < $COMPLEX{STP,CTP}} is
-- This abstraction defines the interface for the angle classes.
-- Version 1.2 Aug 2001. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 9 Jan 97 kh Original extracted from FLT
-- 4 Sep 97 kh Incorporated complex operations
-- 23 Aug 01 kh added creation and conversion common.
radians(
val : STP
) : SAME ;
-- This is the creation operation where the numeric value is given in
-- radians.
degrees(
val : STP
) : SAME ;
-- This is the creation operation where the numeric value is given in
-- degrees.
phase(
val : CTP
) : SAME ;
-- This is the creation operation where the numeric value is given in
-- complex number form.
vector(
magnitude : STP
) : CTP ;
-- This routine returns the complex number which corresponds to a vector
-- of the given magnitude with self as the angle.
radians : STP ;
-- The value of self in readians.
degrees : STP ;
-- The value of self in degrees.
negate : SAME ;
-- This routine returns the negation of self.
times(
factor : STP
) : SAME ;
-- This routine returns the result of multiplying self by factor.
div(
factor : STP
) : SAME ;
-- This routine returns the result of dividing self by factor.
cos : STP ;
-- This routine returns the value which is the trigonometric cosine of
-- self.
cpxcos : CTP ;
-- This routine returns the value which is the trigonometric cosine of
-- self
sin : STP ;
-- This routine returns the value which is the trigonometric sine of
-- self
cpxsin : CTP ;
-- This routine returns the value which is the trigonometric sine of
-- self.
atan(
arg : STP
) : SAME ;
-- This routine returns the arc tangent of self divided by arg in
-- the range [-pi/2, pi/2]. The result is in the quadrant specified by
-- (self, arg).
cpxatan(
arg : CTP
) : SAME ;
-- This routine returns the arc tangent of self divided by arg in
-- the range [-pi/2, pi/2]. The result is in the quadrant specified by
-- (self, arg).
atan2(
denominator,
divisor : STP
) : SAME ;
-- This routine creates an angle whose arctangent is the result of
-- dividing denominator by divisor.
acos(
val : STP
) : SAME ;
-- This routine returns the arc cosine of self in the range [0.0 to pi].
cpxacos(
val : CTP
) : SAME ;
-- This routine returns the arc cosine of self in the range [0.0 to pi].
asin(
val : STP
) : SAME ;
-- This routine returns the arc sine of self in the range [0.0 to pi].
cpxasin(
val : CTP
) : SAME ;
-- This routine returns the arc sine of self in the range [0.0 to pi].
cis : CTP ;
-- This routine returns the result cos(self) + i sin(self).
end ; -- $ANGLES
partial class ANGLE{STP,CTP} < $ANGLES{STP,CTP}, $BINARY
partial class ANGLE{STP,CTP} < $ANGLES{STP,CTP}, $BINARY is
-- This class embodies the concept of a geometric angle. It consists
-- primarily of creation/conversion and arithmetic operations.
--
-- Angles are represented internally as normalised values in radians
-- in the range -pi to pi.
--
-- NOTE This class was originally part of the standard Sather distribution
-- approximate number class FLT. Separated out because of the
-- numeric and angular domain being different conceptually.
-- Version 1.3 Mar 99. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 9 Jan 97 kh Original extracted from FLT.
-- 4 Sep 97 kh Incorporated complex operations.
-- 11 Jan 99 kh added $BINARY sub-typing
-- 25 Mar 99 kh Parameterised for simplicity in CPX
include AVAL{BIT} ;
include COMPARABLE ;
include BINARY ;
private const Degrees_to_Radians : STP := STP::pi / Half_Circle ;
private const Radians_to_Degrees : STP := Half_Circle / STP::pi ;
--private const asize : CARD := STP::Num_Bits ;
stub radians : STP ;
-- This routine returns the numeric value of self.
stub cos : STP ;
-- This routine returns the value which is the trigonometric cosine of
-- self.
stub sin : STP ;
-- This routine returns the value which is the trigonometric sine of
-- self
stub atan(
arg : STP
) : SAME ;
-- This routine returns the arc tangent of self divided by arg in
-- the range [-pi/2, pi/2]. The result is in the quadrant specified by
-- (self, arg).
stub acos(
val : STP
) : SAME ;
-- This routine returns the arc cosine of self in the range [0.0 to pi].
stub asin(
val : STP
) : SAME ;
-- This routine returns the arc sine of self in the range [0.0 to pi].
private normalise(
val : STP
) : SAME is
-- This routine takes as its parameter an un-normalised value in radians
-- and returns its normalized value.
if val > STP::zero then
loop
if val <= STP::pi then
return priv_radians(val)
else
val := val - Two_Pi
end
end
else
loop
if val >= -STP::pi then
return priv_radians(val)
else
val := val + Two_Pi
end
end
end
end ;
build(
cursor : BIN_CURSOR
) : SAME
pre ~void(cursor)
and ~cursor.is_done
post true
is
-- This routine creates a new angle object from the indicated binary
-- string.
res : STP := STP::build(cursor) ;
return normalise(res)
end ;
radians(
val : STP
) : SAME is
-- This is the creation operation where the numeric value is given in
-- radians.
return normalise(val)
end ;
degrees(
val : STP
) : SAME is
-- This is the creation operation where the numeric value is given in
-- degrees.
return normalise(val * Degrees_to_Radians)
end ;
phase(
val : CTP
) : SAME is
-- This is the creation operation where the numeric value is given in
-- complex number form.
return atan2(val.im,val.re)
end ;
is_nil : BOOL is
-- This predicate returns true if and only if the value of self is the
-- nil value.
return radians.is_nil
end ;
nil : SAME is
-- This returns the nil value of an angle.
return priv_radians(STP::nil)
end ;
cpxacos(
val : CTP
) : SAME is
-- This routine returns the angle with the given complex cosine, ie
--
-- pi/2 - asin(val) See Steele p305
raise val -- TO DO!
end ;
cpxasin(
val : CTP
) : SAME is
-- This routine returns the angle with the given complex sine value
-- using the algorithm
--
-- -i log(i*val + sqrt(1 - val^2)) See Steele p305
raise val -- TO DO!
end ;
cpxatan(
val : CTP
) : SAME is
-- This routine creates an angle given the tangent argument. It uses
-- the algorithm
--
-- (log(1 + i * val) - log(1 - i * val)) / (2 * i). See Steele p307.
raise val -- TO DO!
end ;
binstr : BINSTR
pre true
post (result.size > 0)
is
-- This routine returns a binary string representation of self.
return radians.binstr
end ;
-- The standard arithmetic operations.
plus(
other : SAME
) : SAME is
-- This routine returns the normalised result of adding self to other.
return normalise(self.radians + other.radians)
end ;
minus(
other : SAME
) : SAME is
-- This routine returns the result of subtracting other from self
-- provided that this is represntable in the value domain.
return normalise(self.radians - other.radians)
end ;
negate : SAME is
-- This routine returns the negation of self. While the mathematical
-- number model dictates that this should be the same as subtracting self
-- from zero, the IEEE model differs in doing different things depending
-- on the sign bit and rounding mode. Built-in.
return normalise(-radians)
end ;
times(
factor : STP
) : SAME is
-- This routine returns the signed product of self and other. Built-in.
return normalise(self.radians * factor)
end ;
div(
other : SAME
) : STP is
-- This routine returns the signed quotient of self and other as a ratio.
return (self.radians / other.radians)
end ;
div(
factor : STP
) : SAME is
-- This routine returns the signed quotient of self and other as a ratio.
return normalise(self.radians / factor)
end ;
-- Relations between objects
is_eq(
other : SAME
) : BOOL is
-- This predicate returns tru if and only if self and other have the same
-- value. Built-in.
return self.radians = other.radians
end ;
is_lt(
other : SAME
) : BOOL is
-- This predicate returns true if and only if other is less than self.
-- Built-in.
return self.radians < other.radians
end ;
-- Numeric conversion routines.
degrees : STP is
-- This routine returns the number which is the normalised value of
-- self in degrees.
return radians * Radians_to_Degrees
end ;
vector(
magnitude : STP
) : CTP is
-- This routine returns the complex number which corresponds to a vector
-- of the given magnitude with self as the angle.
return CTP::create(magnitude * cos, magnitude * sin)
end ;
-- Trigonometric creation/conversion operations.
-- The trigonometric conversion functions handle exceptional arguments
-- in the spirit of IEEE 754-1985. So:
-- +-infinity.sin, +-infinity.cos, +-infinity.tan return NaN
-- x.asin and x.acos with x.abs > 1 return NaN
cpxsin : CTP is
-- This routine creates a complex number which gives the sine of the
-- angle self.
loc_res : STP := radians.exp ;
exponent : CTP := CTP::create(loc_res,loc_res) ;
neg_exponent : CTP := CTP::create(-loc_res, -loc_res) ;
return (exponent - neg_exponent)/STP::create(2.0)
end ;
cpxcos : CTP is
-- This routine creates a number which corresponds to the cosine of
-- the angle self.
loc_res : STP := radians.exp ;
exponent : CTP := CTP::create(loc_res,loc_res) ;
neg_exponent : CTP := CTP::create(-loc_res, -loc_res) ;
return (exponent + neg_exponent) / STP::create(2.0)
end ;
cpxtan : CTP is
-- This routine creates a number which corresponds to the tangent of
-- the angle self.
num : CTP := CTP::create(sin,STP::zero) ;
denom : CTP := CTP::create(cos,STP::zero) ;
return num / denom
end ;
cis : CTP is
-- This routine returns the result cos(self) + i sin(self). See Steele
-- p304.
return CTP::create(cos,sin)
end ;
end ; -- ANGLE{STP,CTP}
immutable class ANGLE < $ANGLES{FLT,CPX}, $BINARY, $ORDERED{ANGLE}, $IS_EQ
immutable class ANGLE < $ANGLES{FLT,CPX}, $BINARY, $ORDERED{ANGLE}, $IS_EQ is
-- This class embodies the concept of a geometric angle. It consists
-- primarily of creation/conversion and arithmetic operations.
--
-- Angles are represented internally as normalised values in radians
-- in the range -p1 to pi.
--
-- NOTE This class was originally part of the standard Sather distribution
-- approximate number class FLT. Separated out because of the
-- numeric and angular domains being different conceptually.
-- Version 1.2 Jan 99. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 25 Mar 99 kh Original from ANGLE{STP,CTP}
include ANGLE{FLT,CPX} ;
private const Half_Circle : FLT := 180.0 ;
private const Two_Pi : FLT := FLT::pi * 2.0 ;
private const asize : CARD := 32 ; ----FUDGE for asize!
private priv_radians(
val : FLT
) : SAME is
-- This is the creation operation where the numeric value is given in
-- radians. Built-in to this implementation.
builtin FLT_ANGLE
end ;
-- Creation routines from trigonometric values
acos(
val : FLT
) : SAME is
-- This routine creates an angle given the cosine argument.
builtin ACOS_ANGLE
end ;
asin(
val : FLT
) : SAME is
-- This routine creates an angle given the sine argument.
builtin ASIN_ANGLE
end ;
atan(
val : FLT
) : SAME is
-- This routine creates an angle given the tangent argument.
builtin ATAN_ANGLE
end ;
atan2(
denominator,
divisor : FLT
) : SAME is
-- This routine creates an angle whose arctangent is the result of
-- dividing denominator by divisor.
builtin ATAN2_ANGLE
end ;
radians : FLT is
-- This routine returns the number which is the normalised value of
-- self in radians.
builtin ANGLE_FLT
end ;
sin : FLT is
-- This routine creates a number which corresponds to the sine of the
-- angle self.
builtin ANGLE_FLT_SIN
end ;
cos : FLT is
-- This routine creates a number which corresponds to the cosine of
-- the angle self.
builtin ANGLE_FLT_COS
end ;
tan : FLT is
-- This routine creates a number which corresponds to the tangent of
-- the angle self.
builtin ANGLE_FLT_TAN
end ;
sincos(
out sine : FLT,
out cosine : FLT
) is
-- This routine simultaneously creates numbers which correspond to
-- the sine and cosine of the angle self. This is more efficient than
-- computing the two separately where both are needed.
-- TEMPORARY(?) FUDGE
sine := sin ;
cosine := cos
-- builtin ANGLE_FLT_SINCOS
end ;
end ; -- ANGLE