home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / piwg / z000020.ada < prev    next >
Encoding:
Text File  |  1988-05-03  |  6.2 KB  |  202 lines

  1.  
  2. -- The purpose of this package is to define an Ada type that has exactly
  3. -- the operations that are valid for any physical quantity. This package
  4. -- is then used by the packages that define many physical units. These
  5. -- packages are used in turn by packages that define operators on physical
  6. -- units that produce other physical units. Additional packages in this
  7. -- set provide for outputting of physical units, conversions between
  8. -- physical units, and other functions needed when working with physical
  9. -- units.
  10. --
  11. -- Note: only this package needs to be generic, all other packages inherit
  12. -- the type REAL from the package PHYSICAL_REAL.
  13. --
  14.  
  15. generic
  16.  
  17. --         The formal generic parameter ANY_REAL will typically be
  18. --         FLOAT or LONG_FLOAT but can be any Ada real type. This
  19. --         type is to define the underlying computer arithmetic that
  20. --         will be used to evaluate the physical equations.
  21. --         ANY_REAL is a non dimensional quantity. It has no physical units.
  22.  
  23.   type ANY_REAL is digits <> ;
  24.  
  25. package GENERIC_PHYSICAL_REAL is
  26.  
  27.   type REAL is private ;
  28.  
  29. --                Operators available for all types derived from REAL
  30. --
  31. --     implicit :    :=    =     /=
  32. --
  33. --
  34. --             Physical quantities with the same units can be added
  35. --             preserving their physical units.
  36.  
  37.   function "+" ( LEFT , RIGHT : REAL ) return REAL ;
  38.  
  39. --             Physical quantities with the same units can be subtracted
  40. --             preserving their physical units.
  41.  
  42.   function "-" ( LEFT , RIGHT : REAL ) return REAL ;
  43.  
  44. --             Multiplying a physical quantity by itself does not produce
  45. --             the same physical quantity and thus must not be allowed.
  46. --             Multiplying a physical quantity by a non dimensional quantity
  47. --             does preserve the units of the physical quantity.
  48.  
  49.   function "*" ( LEFT : ANY_REAL ;
  50.                  RIGHT : REAL ) return REAL ;
  51.  
  52.   function "*" ( LEFT : REAL ;
  53.                  RIGHT : ANY_REAL ) return REAL ;
  54.  
  55. --             Dividing a physical quantity by a non dimensional quantity
  56. --             preserves the units of the physical quantity.
  57.  
  58.   function "/" ( LEFT : REAL ;
  59.                  RIGHT : ANY_REAL ) return REAL ;
  60.  
  61. --             Dividing a physical quantity by itself produces
  62. --             a non dimensional value.
  63.  
  64.   function "/" ( LEFT , RIGHT : REAL ) return ANY_REAL ;
  65.  
  66. --               The absolute value of a physical quantity retains the
  67. --               same physical units.
  68.  
  69.   function "abs" ( LEFT : REAL ) return REAL ;
  70.  
  71. --             Equality and inequality are implicitly defined. The other
  72. --             relational operators must be explicitly defined.
  73.  
  74.   function "<" ( LEFT , RIGHT : REAL ) return BOOLEAN ;
  75.  
  76.   function ">" ( LEFT , RIGHT : REAL ) return BOOLEAN ;
  77.  
  78.   function "<=" ( LEFT , RIGHT : REAL ) return BOOLEAN ;
  79.  
  80.   function ">=" ( LEFT , RIGHT : REAL ) return BOOLEAN ;
  81.  
  82. --              The primary purpose of this function for the user is
  83. --              to make constants into values of a specific physical
  84. --              unit.
  85. --              The use of this function in the set of physics packages
  86. --              is to apply the required Ada type to the result of a
  87. --              non dimensional computation.
  88.  
  89.   function DIMENSION ( LEFT : ANY_REAL ) return REAL ;
  90.  
  91. --              The use of this function in the set of physics packages
  92. --              is to take any physical quantity and get a non dimensional
  93. --              value in the base floating point arithmetic type in order
  94. --              to preform computation. This should not be needed by users
  95. --              of the set of physics packages.
  96.  
  97.   function UNDIMENSION ( LEFT : REAL ) return ANY_REAL ;
  98.  
  99. --    For compilers that can make use of INLINE
  100.  
  101.   pragma INLINE ( "+" , "-" , "*" , "/" , "abs" , "<" , ">" , "<=" , ">=" ,
  102.       DIMENSION , UNDIMENSION ) ;
  103.  
  104. --
  105. private
  106.   type REAL is new ANY_REAL ;  -- ANY_REAL is the generic formal parameter
  107. end GENERIC_PHYSICAL_REAL ;
  108.  
  109. package body GENERIC_PHYSICAL_REAL is
  110.  
  111.   function "+" ( LEFT , RIGHT : REAL ) return REAL is
  112.  
  113.   begin
  114.     return REAL ( ANY_REAL( LEFT ) + ANY_REAL ( RIGHT )) ;
  115.   end "+" ;
  116.  
  117.   function "-" ( LEFT , RIGHT : REAL ) return REAL is
  118.  
  119.   begin
  120.     return REAL ( ANY_REAL( LEFT ) - ANY_REAL ( RIGHT )) ;
  121.   end "-" ;
  122.  
  123.   function "*" ( LEFT : ANY_REAL ;
  124.                  RIGHT : REAL ) return REAL is
  125.  
  126.   begin
  127.     return REAL ( LEFT * ANY_REAL( RIGHT )) ;
  128.   end "*" ;
  129.  
  130.   function "*" ( LEFT : REAL ;
  131.                  RIGHT : ANY_REAL ) return REAL is
  132.  
  133.   begin
  134.     return REAL ( ANY_REAL( LEFT ) * RIGHT) ;
  135.   end "*" ;
  136.  
  137.   function "/" ( LEFT : REAL ;
  138.                  RIGHT : ANY_REAL ) return REAL is
  139.  
  140.   begin
  141.     return REAL ( ANY_REAL( LEFT ) / RIGHT) ;
  142.   end "/" ;
  143.  
  144.   function "/" ( LEFT , RIGHT : REAL ) return ANY_REAL is
  145.  
  146.   begin
  147.     return ANY_REAL ( LEFT ) / ANY_REAL ( RIGHT ) ;
  148.   end "/" ;
  149.  
  150.   function "abs" ( LEFT : REAL ) return REAL is
  151.  
  152.   begin
  153.     return REAL ( abs( ANY_REAL( LEFT ))) ;
  154.   end "abs" ;
  155.  
  156.   function "<" ( LEFT , RIGHT : REAL ) return BOOLEAN is
  157.  
  158.   begin
  159.     return ANY_REAL ( LEFT ) < ANY_REAL ( RIGHT ) ;
  160.   end "<" ;
  161.  
  162.   function ">" ( LEFT , RIGHT : REAL ) return BOOLEAN is
  163.  
  164.   begin
  165.     return ANY_REAL ( LEFT ) > ANY_REAL ( RIGHT ) ;
  166.   end ">" ;
  167.  
  168.   function "<=" ( LEFT , RIGHT : REAL ) return BOOLEAN is
  169.  
  170.   begin
  171.     return ANY_REAL ( LEFT ) <= ANY_REAL ( RIGHT ) ;
  172.   end "<=" ;
  173.  
  174.   function ">=" ( LEFT , RIGHT : REAL ) return BOOLEAN is
  175.  
  176.   begin
  177.     return ANY_REAL ( LEFT ) >= ANY_REAL ( RIGHT ) ;
  178.   end ">=" ;
  179.  
  180.   function DIMENSION ( LEFT : ANY_REAL ) return REAL is
  181.  
  182.   begin
  183.     return REAL ( LEFT ) ;
  184.   end DIMENSION ;
  185.  
  186.   function UNDIMENSION ( LEFT : REAL ) return ANY_REAL is
  187.  
  188.   begin
  189.     return ANY_REAL ( LEFT ) ;
  190.   end UNDIMENSION ;
  191.  
  192. end GENERIC_PHYSICAL_REAL ;
  193.  
  194. -- The following instantiation is used by many other packages in the
  195. -- set of physical units packages. The instantiated package becomes
  196. -- a withable library unit thus saving the reinstantiation in every
  197. -- other package.
  198. --
  199. with GENERIC_PHYSICAL_REAL ;
  200.  
  201. package PHYSICAL_REAL is new GENERIC_PHYSICAL_REAL ( FLOAT ) ;
  202.