home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / piwg / z000003.ada < prev    next >
Encoding:
Text File  |  1988-05-03  |  5.2 KB  |  181 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.  
  12. package PHYSICAL_REAL is
  13.  
  14.   type REAL is private ;
  15.  
  16. --                Operators available for all types derived from REAL
  17. --
  18. --     implicit :    :=    =     /=
  19. --
  20. --
  21. --             Physical quantities with the same units can be added
  22. --             preserving their physical units.
  23.  
  24.   function "+" ( LEFT , RIGHT : REAL ) return REAL ;
  25.  
  26. --             Physical quantities with the same units can be subtracted
  27. --             preserving their physical units.
  28.  
  29.   function "-" ( LEFT , RIGHT : REAL ) return REAL ;
  30.  
  31. --             Multiplying a physical quantity by itself does not produce
  32. --             the same physical quantity and thus must not be allowed.
  33. --             Multiplying a physical quantity by a non dimensional quantity
  34. --             does preserve the units of the physical quantity.
  35.  
  36.   function "*" ( LEFT : FLOAT ;
  37.                  RIGHT : REAL ) return REAL ;
  38.  
  39.   function "*" ( LEFT : REAL ;
  40.                  RIGHT : FLOAT ) return REAL ;
  41.  
  42. --             Dividing a physical quantity by a non dimensional quantity
  43. --             preserves the units of the physical quantity.
  44.  
  45.   function "/" ( LEFT : REAL ;
  46.                  RIGHT : FLOAT ) return REAL ;
  47.  
  48. --             Dividing a physical quantity by itself produces
  49. --             a non dimensional value.
  50.  
  51.   function "/" ( LEFT , RIGHT : REAL ) return FLOAT ;
  52.  
  53. --               The absolute value of a physical quantity retains the
  54. --               same physical units.
  55.  
  56.   function "abs" ( LEFT : REAL ) return REAL ;
  57.  
  58. --             Equality and inequality are implicitly defined. The other
  59. --             relational operators must be explicitly defined.
  60.  
  61.   function "<" ( LEFT , RIGHT : REAL ) return BOOLEAN ;
  62.  
  63.   function ">" ( LEFT , RIGHT : REAL ) return BOOLEAN ;
  64.  
  65.   function "<=" ( LEFT , RIGHT : REAL ) return BOOLEAN ;
  66.  
  67.   function ">=" ( LEFT , RIGHT : REAL ) return BOOLEAN ;
  68.  
  69. --              The primary purpose of this function for the user is
  70. --              to make constants into values of a specific physical
  71. --              unit.
  72. --              The use of this function in the set of physics packages
  73. --              is to apply the required Ada type to the result of a
  74. --              non dimensional computation.
  75.  
  76.   function DIMENSION ( LEFT : FLOAT ) return REAL ;
  77.  
  78. --              The use of this function in the set of physics packages
  79. --              is to take any physical quantity and get a non dimensional
  80. --              value in the base floating point arithmetic type in order
  81. --              to preform computation. This should not be needed by users
  82. --              of the set of physics packages.
  83.  
  84.   function UNDIMENSION ( LEFT : REAL ) return FLOAT ;
  85.  
  86. --    For compilers that can make use of INLINE
  87.  
  88.   pragma INLINE ( "+" , "-" , "*" , "/" , "abs" , "<" , ">" , "<=" , ">=" ,
  89.       DIMENSION , UNDIMENSION ) ;
  90.  
  91. --
  92. private
  93.   type REAL is new FLOAT ;  
  94. end PHYSICAL_REAL ;
  95.  
  96. package body PHYSICAL_REAL is
  97.  
  98.   function "+" ( LEFT , RIGHT : REAL ) return REAL is
  99.  
  100.   begin
  101.     return REAL ( FLOAT( LEFT ) + FLOAT ( RIGHT )) ;
  102.   end "+" ;
  103.  
  104.   function "-" ( LEFT , RIGHT : REAL ) return REAL is
  105.  
  106.   begin
  107.     return REAL ( FLOAT( LEFT ) - FLOAT ( RIGHT )) ;
  108.   end "-" ;
  109.  
  110.   function "*" ( LEFT : FLOAT ;
  111.                  RIGHT : REAL ) return REAL is
  112.  
  113.   begin
  114.     return REAL ( LEFT * FLOAT( RIGHT )) ;
  115.   end "*" ;
  116.  
  117.   function "*" ( LEFT : REAL ;
  118.                  RIGHT : FLOAT ) return REAL is
  119.  
  120.   begin
  121.     return REAL ( FLOAT( LEFT ) * RIGHT) ;
  122.   end "*" ;
  123.  
  124.   function "/" ( LEFT : REAL ;
  125.                  RIGHT : FLOAT ) return REAL is
  126.  
  127.   begin
  128.     return REAL ( FLOAT( LEFT ) / RIGHT) ;
  129.   end "/" ;
  130.  
  131.   function "/" ( LEFT , RIGHT : REAL ) return FLOAT is
  132.  
  133.   begin
  134.     return FLOAT ( LEFT ) / FLOAT ( RIGHT ) ;
  135.   end "/" ;
  136.  
  137.   function "abs" ( LEFT : REAL ) return REAL is
  138.  
  139.   begin
  140.     return REAL ( abs( FLOAT( LEFT ))) ;
  141.   end "abs" ;
  142.  
  143.   function "<" ( LEFT , RIGHT : REAL ) return BOOLEAN is
  144.  
  145.   begin
  146.     return FLOAT ( LEFT ) < FLOAT ( RIGHT ) ;
  147.   end "<" ;
  148.  
  149.   function ">" ( LEFT , RIGHT : REAL ) return BOOLEAN is
  150.  
  151.   begin
  152.     return FLOAT ( LEFT ) > FLOAT ( RIGHT ) ;
  153.   end ">" ;
  154.  
  155.   function "<=" ( LEFT , RIGHT : REAL ) return BOOLEAN is
  156.  
  157.   begin
  158.     return FLOAT ( LEFT ) <= FLOAT ( RIGHT ) ;
  159.   end "<=" ;
  160.  
  161.   function ">=" ( LEFT , RIGHT : REAL ) return BOOLEAN is
  162.  
  163.   begin
  164.     return FLOAT ( LEFT ) >= FLOAT ( RIGHT ) ;
  165.   end ">=" ;
  166.  
  167.   function DIMENSION ( LEFT : FLOAT ) return REAL is
  168.  
  169.   begin
  170.     return REAL ( LEFT ) ;
  171.   end DIMENSION ;
  172.  
  173.   function UNDIMENSION ( LEFT : REAL ) return FLOAT is
  174.  
  175.   begin
  176.     return FLOAT ( LEFT ) ;
  177.   end UNDIMENSION ;
  178.  
  179. end PHYSICAL_REAL ;
  180.  
  181.