1 (* RealStr.mod implement the ISO RealStr specification.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 IMPLEMENTATION MODULE RealStr
;
29 (* REAL/string conversions *)
33 FROM DynamicStrings
IMPORT String
, InitString
, KillString
, Length
, CopyOut
;
35 FROM ConvStringReal
IMPORT RealToFixedString
, RealToFloatString
,
39 (* the string form of a signed fixed-point real number is
40 ["+" | "-"], decimal digit, {decimal digit}, [".",
44 (* the string form of a signed floating-point real number is
45 signed fixed-point real number, "E", ["+" | "-"],
46 decimal digit, {decimal digit}
49 PROCEDURE StrToReal (str
: ARRAY OF CHAR; VAR real
: REAL;
50 VAR res
: ConvResults
) ;
51 (* Ignores any leading spaces in str. If the subsequent characters
52 in str are in the format of a signed real number, assigns a
53 corresponding value to real. Assigns a value indicating the
57 res
:= RealConv.
FormatReal(str
) ;
60 real
:= RealConv.
ValueReal(str
)
65 PROCEDURE RealToFloat (real
: REAL; sigFigs
: CARDINAL;
66 VAR str
: ARRAY OF CHAR) ;
67 (* Converts the value of real to floating-point string form, with
68 sigFigs significant figures, and copies the possibly truncated
74 s
:= RealToFloatString(real
, sigFigs
) ;
80 PROCEDURE RealToEng (real
: REAL; sigFigs
: CARDINAL;
81 VAR str
: ARRAY OF CHAR) ;
82 (* Converts the value of real to floating-point string form, with
83 sigFigs significant figures, and copies the possibly truncated
84 result to str. The number is scaled with one to three digits
85 in the whole number part and with an exponent that is a multiple
91 s
:= RealToEngString(real
, sigFigs
) ;
97 PROCEDURE RealToFixed (real
: REAL; place
: INTEGER;
98 VAR str
: ARRAY OF CHAR) ;
99 (* Converts the value of real to fixed-point string form, rounded
100 to the given place relative to the decimal point, and copies
101 the possibly truncated result to str.
106 s
:= RealToFixedString(real
, place
) ;
112 PROCEDURE RealToStr (real
: REAL; VAR str
: ARRAY OF CHAR) ;
113 (* Converts the value of real as RealToFixed if the sign and
114 magnitude can be shown within the capacity of str, or
115 otherwise as RealToFloat, and copies the possibly truncated
116 result to str. The number of places or significant digits
117 are implementation-defined.
123 sigFigs
:= HIGH(str
) ;
125 s
:= RealToFixedString(real
, sigFigs
) ;
126 IF Length(s
)<=HIGH(str
)
135 sigFigs
:= HIGH(str
) ;
137 s
:= RealToFloatString(real
, sigFigs
) ;
138 IF Length(s
)<=HIGH(str
)