| 1 |
(* ShortStr.def provides conversion between shortreal and strings. |
| 2 |
|
| 3 |
Copyright (C) 2023-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 |
DEFINITION MODULE ShortStr; |
| 28 |
|
| 29 |
(* SHORTREAL/string conversions *) |
| 30 |
|
| 31 |
IMPORT |
| 32 |
ConvTypes; |
| 33 |
|
| 34 |
TYPE |
| 35 |
(* strAllRight, strOutOfRange, strWrongFormat, strEmpty *) |
| 36 |
ConvResults = ConvTypes.ConvResults; |
| 37 |
|
| 38 |
(* the string form of a signed fixed-point real number is |
| 39 |
["+" | "-"], decimal digit, {decimal digit}, [".", |
| 40 |
{decimal digit}] |
| 41 |
*) |
| 42 |
|
| 43 |
(* the string form of a signed floating-point real number is |
| 44 |
signed fixed-point real number, "E", ["+" | "-"], |
| 45 |
decimal digit, {decimal digit} |
| 46 |
*) |
| 47 |
|
| 48 |
PROCEDURE StrToReal (str: ARRAY OF CHAR; VAR real: SHORTREAL; |
| 49 |
VAR res: ConvResults); |
| 50 |
(* Ignores any leading spaces in str. If the subsequent characters |
| 51 |
in str are in the format of a signed real number, assigns a |
| 52 |
corresponding value to real. Assigns a value indicating the |
| 53 |
format of str to res. |
| 54 |
*) |
| 55 |
|
| 56 |
PROCEDURE RealToFloat (real: SHORTREAL; sigFigs: CARDINAL; |
| 57 |
VAR str: ARRAY OF CHAR); |
| 58 |
(* Converts the value of real to floating-point string form, with |
| 59 |
sigFigs significant figures, and copies the possibly truncated |
| 60 |
result to str. |
| 61 |
*) |
| 62 |
|
| 63 |
PROCEDURE RealToEng (real: SHORTREAL; sigFigs: CARDINAL; |
| 64 |
VAR str: ARRAY OF CHAR); |
| 65 |
(* Converts the value of real to floating-point string form, with |
| 66 |
sigFigs significant figures, and copies the possibly truncated |
| 67 |
result to str. The number is scaled with one to three digits |
| 68 |
in the whole number part and with an exponent that is a |
| 69 |
multiple of three. |
| 70 |
*) |
| 71 |
|
| 72 |
PROCEDURE RealToFixed (real: SHORTREAL; place: INTEGER; |
| 73 |
VAR str: ARRAY OF CHAR); |
| 74 |
(* Converts the value of real to fixed-point string form, rounded |
| 75 |
to the given place relative to the decimal point, and copies |
| 76 |
the possibly truncated result to str. |
| 77 |
*) |
| 78 |
|
| 79 |
PROCEDURE RealToStr (real: SHORTREAL; VAR str: ARRAY OF CHAR); |
| 80 |
(* Converts the value of real as RealToFixed if the sign and |
| 81 |
magnitude can be shown within the capacity of str, or |
| 82 |
otherwise as RealToFloat, and copies the possibly truncated |
| 83 |
result to str. The number of places or significant digits |
| 84 |
depend on the capacity of str. |
| 85 |
*) |
| 86 |
|
| 87 |
END ShortStr. |