VSSL/source/library/vssl-conversions-generic_st...

92 lines
3.5 KiB
Ada

generic
type Number_Type (<>) is private;
with function "-" (
A : Number_Type)
return Number_Type is <>;
with function "+" (
A,
B : Number_Type)
return Number_Type is <>;
with function "*" (
A,
B : Number_Type)
return Number_Type is <>;
type Exponent_Type is range <>; -- A type to store exponent values
with function To_Number (
A : Integer)
return Number_Type is <>; -- A function to convert a digit to a given type
with function Multiply_by_Power (
A,
R : Number_Type;
N : Exponent_Type)
return Number_Type is <>;
function VSSL.Conversions.Generic_String_to_Number (
S : String)
return Number_Type;
-- Converts a textual representation of a number to an arbitrary type.
--
-- Supported formats:
--
-- * With integer and/or fractional part with or without decimal separator
-- (required if fractional part exists). Decimal separator can be dot or comma.
-- "1" => 1.0
-- "1." => 1.0
-- "1.0" => 1.0
-- "0.4" => 0.4
-- ".4" => 0.4
-- "2,3" => 2.3
-- * With or without a sign
-- "1" => 1.0
-- "+1" => 1.0
-- "-1" => -1.0
-- * Digits can be separated with undercsores (_) or spaces ( ).
-- C0 Control characters (like NUL, TAB) are also ignored in most positions.
-- "31 968" => 31.968
-- "3.14159_26" => 3.1415926
-- * In scientific exponentional format. Common letters E or e and
-- also Fortran-ish D or d can be used to introduce exponent.
-- "12E7" => 120000000.0
-- "+1.24d-03" => 0.00123
-- * In Ada-style based format. Base range is enhanced to 2..36.
-- Both hash (#) and colon (:) are supported (colon was a valid replacement
-- for hash in Ada-83).
-- If there's no exponent, the second hash can be omitted.
-- "16#7FFD.8#" => 32765.5
-- "8#3.77#E+2" => 255.0
-- * With C-style prefixes for hexadecimal (0x) and binary (0b).
-- "0x7FFD.8" => 32765.5
-- "0b1101" => 13.0
-- * With C-stype binary exponent also
-- "0x1.8p1" => 3.0
-- * In Fortran-style BOZ-format.
-- "B'1101'" => 13.0
-- "O'377'" => 255.0
-- "Z'7FFD'" => 32765.0
-- * In Algol-style 'r' based format.
-- "16r7FFD.8" => 32765.5
-- * Postfix H and B for hexadecimal and binary numbers.
-- "1101B" => 13.0
-- "7FFD.8H" => 32765.5
-- * Upper and lowercase letters allowed.
-- "16#7fFd.8#" => 32765.5
--
-- NOT supported formats:
--
-- * Any kind of non-finite values, like NANs, Infinities.
-- * Imaginary, complex, dual, imprecise etc. multi-component numbers.
-- * Periodic fractions.
-- * C-style octals.
-- * Sexagesimal numbers like in HMS time or DMS angles.
-- * Roman numerals.
-- * Anything out of the ASCII character set.
--
-- It tries to analyse the whole string. If data is in wrong format
-- then exception may be raised or wrong result produced.
-- The behaviour if the result is out of range is on the implementation
-- of underlying arithmetic subprograms.
--
-- Implementation of numeric type is required to be able a hold a value of Base**Number_of_Digits - 1.
-- (More precisely, the largest number that resemble all the digits without decimal separator)
-- This generally fits well in floating point or integers but does not fit in fixed point.