| 1 |
(* SRealIO.mod implement the ISO SRealIO specification. |
| 2 |
|
| 3 |
Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 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) |
| 11 |
any later version. |
| 12 |
|
| 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. |
| 17 |
|
| 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. |
| 21 |
|
| 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/>. *) |
| 26 |
|
| 27 |
IMPLEMENTATION MODULE SRealIO ; |
| 28 |
|
| 29 |
IMPORT StdChans, RealIO ; |
| 30 |
|
| 31 |
(* Input and output of real numbers in decimal text form over |
| 32 |
default channels. The read result is of the type |
| 33 |
IOConsts.ReadResults. |
| 34 |
*) |
| 35 |
|
| 36 |
(* The text form of a signed fixed-point real number is |
| 37 |
["+" | "-"], decimal digit, {decimal digit}, |
| 38 |
[".", {decimal digit}] |
| 39 |
|
| 40 |
The text form of a signed floating-point real number is |
| 41 |
signed fixed-point real number, |
| 42 |
"E", ["+" | "-"], decimal digit, {decimal digit} |
| 43 |
*) |
| 44 |
|
| 45 |
PROCEDURE ReadReal (VAR real: REAL); |
| 46 |
(* Skips leading spaces, and removes any remaining characters |
| 47 |
from the default input channel that form part of a signed |
| 48 |
fixed or floating point number. The value of this number |
| 49 |
is assigned to real. The read result is set to the value |
| 50 |
allRight, outOfRange, wrongFormat, endOfLine, or endOfInput. |
| 51 |
*) |
| 52 |
BEGIN |
| 53 |
RealIO.ReadReal(StdChans.StdInChan(), real) |
| 54 |
END ReadReal ; |
| 55 |
|
| 56 |
PROCEDURE WriteFloat (real: REAL; sigFigs: CARDINAL; width: CARDINAL); |
| 57 |
(* Writes the value of real to the default output channel in |
| 58 |
floating-point text form, with sigFigs significant figures, |
| 59 |
in a field of the given minimum width. |
| 60 |
*) |
| 61 |
BEGIN |
| 62 |
RealIO.WriteFloat(StdChans.StdOutChan(), real, sigFigs, width) |
| 63 |
END WriteFloat ; |
| 64 |
|
| 65 |
PROCEDURE WriteEng (real: REAL; sigFigs: CARDINAL; width: CARDINAL); |
| 66 |
(* As for WriteFloat, except that the number is scaled with one to |
| 67 |
three digits in the whole number part, and with an exponent that |
| 68 |
is a multiple of three. |
| 69 |
*) |
| 70 |
BEGIN |
| 71 |
RealIO.WriteFloat(StdChans.StdOutChan(), real, sigFigs, width) |
| 72 |
END WriteEng ; |
| 73 |
|
| 74 |
PROCEDURE WriteFixed (real: REAL; place: INTEGER; width: CARDINAL); |
| 75 |
(* Writes the value of real to the default output channel in |
| 76 |
fixed-point text form, rounded to the given place relative |
| 77 |
to the decimal point, in a field of the given minimum width. |
| 78 |
*) |
| 79 |
BEGIN |
| 80 |
RealIO.WriteFixed(StdChans.StdOutChan(), real, place, width) |
| 81 |
END WriteFixed ; |
| 82 |
|
| 83 |
PROCEDURE WriteReal (real: REAL; width: CARDINAL); |
| 84 |
(* Writes the value of real to the default output channel, as |
| 85 |
WriteFixed if the sign and magnitude can be shown in the |
| 86 |
given width, or otherwise as WriteFloat. The number of |
| 87 |
places or significant digits depends on the given width. |
| 88 |
*) |
| 89 |
BEGIN |
| 90 |
RealIO.WriteReal(StdChans.StdOutChan(), real, width) |
| 91 |
END WriteReal ; |
| 92 |
|
| 93 |
END SRealIO. |