G.1.3 Complex Input-Output
The generic package Text_IO.Complex_IO defines procedures
for the formatted input and output of complex values. The generic actual
parameter in an instantiation of Text_IO.Complex_IO is an instance of
Numerics.Generic_Complex_Types for some floating point subtype. Exceptional
conditions are reported by raising the appropriate exception defined
in Text_IO.
Implementation Note: An implementation
of Text_IO.Complex_IO can be built around an instance of Text_IO.Float_IO
for the base subtype of Complex_Types.Real, where Complex_Types is the
generic formal package parameter of Text_IO.Complex_IO. There is no need
for an implementation of Text_IO.Complex_IO to parse real values.
Static Semantics
The generic library
package Text_IO.Complex_IO has the following declaration:
Ramification: Because this is a child
of Text_IO, the declarations of the visible part of Text_IO are directly
visible within it.
{
AI12-0302-1}
with Ada.Numerics.Generic_Complex_Types;
generic
with package Complex_Types
is
new Ada.Numerics.Generic_Complex_Types (<>);
package Ada.Text_IO.Complex_IO
with Global =>
in out synchronized is
use Complex_Types;
Default_Fore : Field := 2;
Default_Aft : Field := Real'Digits - 1;
Default_Exp : Field := 3;
procedure Get (File :
in File_Type;
Item :
out Complex;
Width :
in Field := 0);
procedure Get (Item :
out Complex;
Width :
in Field := 0);
procedure Put (File :
in File_Type;
Item :
in Complex;
Fore :
in Field := Default_Fore;
Aft :
in Field := Default_Aft;
Exp :
in Field := Default_Exp);
procedure Put (Item :
in Complex;
Fore :
in Field := Default_Fore;
Aft :
in Field := Default_Aft;
Exp :
in Field := Default_Exp);
{
AI12-0241-1}
procedure Get (From :
in String;
Item :
out Complex;
Last :
out Positive)
with Nonblocking;
procedure Put (To :
out String;
Item :
in Complex;
Aft :
in Field := Default_Aft;
Exp :
in Field := Default_Exp)
with Nonblocking;
end Ada.Text_IO.Complex_IO;
{
AI95-00328-01}
The library package Complex_Text_IO defines the
same subprograms as Text_IO.Complex_IO, except that the predefined type
Float is systematically substituted for Real, and the type Numerics.Complex_Types.Complex
is systematically substituted for Complex throughout. Nongeneric equivalents
of Text_IO.Complex_IO corresponding to each of the other predefined floating
point types are defined similarly, with the names Short_Complex_Text_IO,
Long_Complex_Text_IO, etc.
Reason: The nongeneric equivalents are
provided to allow the programmer to construct simple mathematical applications
without being required to understand and use generics.
The semantics of the Get and Put procedures are as
follows:
procedure Get (File : in File_Type;
Item : out Complex;
Width : in Field := 0);
procedure Get (Item : out Complex;
Width : in Field := 0);
{
8652/0092}
{
AI95-00029-01}
The input sequence is a pair of optionally signed real literals representing
the real and imaginary components of a complex value. These components
have the format defined for the corresponding Get procedure of an instance
of Text_IO.Float_IO (see
A.10.9) for the
base subtype of Complex_Types.Real. The pair of components may be separated
by a comma or surrounded by a pair of parentheses or both. Blanks are
freely allowed before each of the components and before the parentheses
and comma, if either is used. If the value of the parameter Width is
zero, then
line and page terminators are also allowed
in these places;
the components shall be separated by at
least one blank or line terminator if the comma is omitted; and
reading stops when the right parenthesis
has been read, if the input sequence includes a left parenthesis, or
when the imaginary component has been read, otherwise.
If a nonzero value
of Width is supplied, then
the components shall be separated by at
least one blank if the comma is omitted; and
exactly Width characters are read, or
the characters (possibly none) up to a line terminator, whichever comes
first (blanks are included in the count).
Reason: The parenthesized and comma-separated
form is the form produced by Put on output (see below), and also by list-directed
output in Fortran. The other allowed forms match several common styles
of edit-directed output in Fortran, allowing most preexisting Fortran
data files containing complex data to be read easily. When such files
contain complex values with no separation between the real and imaginary
components, the user will have to read those components separately, using
an instance of Text_IO.Float_IO.
Returns, in the parameter Item, the value of type
Complex that corresponds to the input sequence.
The exception Text_IO.Data_Error
is raised if the input sequence does not have the required syntax or
if the components of the complex value obtained are not of the base subtype
of Complex_Types.Real.
procedure Put (File : in File_Type;
Item : in Complex;
Fore : in Field := Default_Fore;
Aft : in Field := Default_Aft;
Exp : in Field := Default_Exp);
procedure Put (Item : in Complex;
Fore : in Field := Default_Fore;
Aft : in Field := Default_Aft;
Exp : in Field := Default_Exp);
Outputs the value of the parameter Item as a pair
of decimal literals representing the real and imaginary components of
the complex value, using the syntax of an aggregate. More specifically,
outputs a left parenthesis;
outputs the value of the real component
of the parameter Item with the format defined by the corresponding Put
procedure of an instance of Text_IO.Float_IO for the base subtype of
Complex_Types.Real, using the given values of Fore, Aft, and Exp;
outputs a comma;
outputs the value of the imaginary component
of the parameter Item with the format defined by the corresponding Put
procedure of an instance of Text_IO.Float_IO for the base subtype of
Complex_Types.Real, using the given values of Fore, Aft, and Exp;
outputs
a right parenthesis.
Discussion: If the file has a bounded
line length, a line terminator may be output implicitly before any element
of the sequence itemized above.
Discussion: The option of outputting
the complex value as a pair of reals without additional punctuation is
not provided, since it can be accomplished by outputting the real and
imaginary components of the complex value separately.
procedure Get (From : in String;
Item : out Complex;
Last : out Positive);
{
AI95-00434-01}
Reads a complex value from the beginning of the given string, following
the same rule as the Get procedure that reads a complex value from a
file, but treating the end of the string as a file terminator. Returns,
in the parameter Item, the value of type Complex that corresponds to
the input sequence. Returns in Last the index value such that From(Last)
is the last character read.
The exception Text_IO.Data_Error
is raised if the input sequence does not have the required syntax or
if the components of the complex value obtained are not of the base subtype
of Complex_Types.Real.
procedure Put (To : out String;
Item : in Complex;
Aft : in Field := Default_Aft;
Exp : in Field := Default_Exp);
Outputs the value of the parameter Item to the
given string as a pair of decimal literals representing the real and
imaginary components of the complex value, using the syntax of an aggregate.
More specifically,
a left parenthesis, the real component,
and a comma are left justified in the given string, with the real component
having the format defined by the Put procedure (for output to a file)
of an instance of Text_IO.Float_IO for the base subtype of Complex_Types.Real,
using a value of zero for Fore and the given values of Aft and Exp;
the imaginary component and a right parenthesis
are right justified in the given string, with the imaginary component
having the format defined by the Put procedure (for output to a file)
of an instance of Text_IO.Float_IO for the base subtype of Complex_Types.Real,
using a value for Fore that completely fills the remainder of the string,
together with the given values of Aft and Exp.
Reason: This rule is the one proposed
in LSN-1051. Other rules were considered, including one that would have
read “Outputs the value of the parameter Item to the given string,
following the same rule as for output to a file, using a value for Fore
such that the sequence of characters output exactly fills, or comes closest
to filling, the string; in the latter case, the string is filled by inserting
one extra blank immediately after the comma.” While this latter
rule might be considered the closest analogue to the rule for output
to a string in Text_IO.Float_IO, it requires a more difficult and inefficient
implementation involving special cases when the integer part of one component
is substantially longer than that of the other and the string is too
short to allow both to be preceded by blanks. Unless such a special case
applies, the latter rule might produce better columnar output if several
such strings are ultimately output to a file, but very nearly the same
output can be produced by outputting to the file directly, with the appropriate
value of Fore; in any case, it might validly be assumed that output to
a string is intended for further computation rather than for display,
so that the precise formatting of the string to achieve a particular
appearance is not the major concern.
The exception Text_IO.Layout_Error is raised if
the given string is too short to hold the formatted output.
Implementation Permissions
Other exceptions declared (by renaming) in Text_IO
may be raised by the preceding procedures in the appropriate circumstances,
as for the corresponding procedures of Text_IO.Float_IO.
Extensions to Ada 95
{
AI95-00328-01}
Nongeneric equivalents for Text_IO.Complex_IO are
added, to be consistent with all other language-defined Numerics generic
packages.
Wording Changes from Ada 95
{
8652/0092}
{
AI95-00029-01}
Corrigendum: Clarified that the syntax of values read by Complex_IO
is the same as that read by Text_IO.Float_IO.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe