diff options
Diffstat (limited to 'doc/language/function')
| -rw-r--r-- | doc/language/function/cpsc-function-num_to_str.7.md | 7 | ||||
| -rw-r--r-- | doc/language/function/cpsc-function-str_to_num.7.md | 14 |
2 files changed, 14 insertions, 7 deletions
diff --git a/doc/language/function/cpsc-function-num_to_str.7.md b/doc/language/function/cpsc-function-num_to_str.7.md index 8e895ee..9d9c071 100644 --- a/doc/language/function/cpsc-function-num_to_str.7.md +++ b/doc/language/function/cpsc-function-num_to_str.7.md @@ -7,13 +7,16 @@ cpsc-function-num_to_str - convert a number to text # SYNOPSIS ```text -NUM_TO_STR(number : INTEGER or REAL) RETURNS STRING +NUM_TO_STR(number : INTEGER or REAL) RETURNS DEFAULT STRING +NUM_TO_STR(number : INTEGER or REAL) RETURNS CHAR ``` # DESCRIPTION Provided by the standard string library, loaded and linked by default. +The function table in the 9618 winter 2025 insert allows INTEGER or REAL input and STRING or CHAR output. Context selects the result type; the compiler's DEFAULT extension selects STRING when context supplies none. CHAR results use the same formatting as STRING results and must contain exactly one character, otherwise a catchable runtime error is raised. Thus INTEGER 7 can return '7', but INTEGER 12 and REAL 7.0 cannot return CHAR. + Format INTEGER as signed decimal text and REAL in the same shortest round-trip form that OUTPUT prints. The text is the shortest decimal text that reads back as the same value (as Python's `repr`): fixed notation with at least one fractional digit when 1e-4 <= |x| < 1e16 (`2.0`, `87.5`, `0.0001`), otherwise scientific notation with an exponent of at least two digits (`1e-07`, `1.5e-07`, `1e+16`); special values print as `-0.0`, `inf`, `-inf` and `nan`. # EXAMPLE @@ -24,4 +27,4 @@ OUTPUT NUM_TO_STR(42), " ", NUM_TO_STR(3.5) # SEE ALSO -[cpsc-language-overview(7)](../cpsc-language-overview.7.md), [cpsc-function-str_to_num(7)](cpsc-function-str_to_num.7.md). +[cpsc-language-overview(7)](../cpsc-language-overview.7.md), [cpsc-keyword-default(7)](../keyword/cpsc-keyword-default.7.md), [cpsc-function-str_to_num(7)](cpsc-function-str_to_num.7.md). diff --git a/doc/language/function/cpsc-function-str_to_num.7.md b/doc/language/function/cpsc-function-str_to_num.7.md index a9d008b..6351ec9 100644 --- a/doc/language/function/cpsc-function-str_to_num.7.md +++ b/doc/language/function/cpsc-function-str_to_num.7.md @@ -2,20 +2,24 @@ # NAME -cpsc-function-str_to_num - convert text to a real number +cpsc-function-str_to_num - convert text to a number # SYNOPSIS ```text -STR_TO_NUM(text : STRING) RETURNS REAL -STR_TO_NUM(text : CHAR) RETURNS REAL +STR_TO_NUM(text : STRING) RETURNS DEFAULT REAL +STR_TO_NUM(text : CHAR) RETURNS DEFAULT REAL +STR_TO_NUM(text : STRING) RETURNS INTEGER +STR_TO_NUM(text : CHAR) RETURNS INTEGER ``` # DESCRIPTION Provided by the standard string library, loaded and linked by default. -Return REAL, including for integer-looking text. The whole string must be consumed. Invalid text and trailing junk throw a catchable exception reading `STR_TO_NUM: '<text>' is not a number`, and overflowing values throw `STR_TO_NUM: '<text>' is out of the REAL range`. Underflow is accepted. Use IS_NUM first to check numeric text without throwing for invalid input. +The function table in the 9618 winter 2025 insert allows STRING or CHAR input and REAL or INTEGER output. The compiler selects the return type from context and defaults to REAL, including for integer-looking text. The INTEGER version parses as REAL and applies INT, truncating toward zero with the existing INTEGER range check; it inherits REAL rounding. For example, assigning STR_TO_NUM("-23.45") to an INTEGER gives -23. DEFAULT selection and this truncation policy are compiler extensions. + +The whole string must be consumed. Invalid text and trailing junk throw a catchable exception reading `STR_TO_NUM: '<text>' is not a number`, and REAL overflow throws `STR_TO_NUM: '<text>' is out of the REAL range`. Underflow is accepted. INTEGER overflow uses INT's range error. Use IS_NUM first to check numeric text without throwing for invalid input; it does not check INTEGER range. # EXAMPLE @@ -25,4 +29,4 @@ OUTPUT STR_TO_NUM("12.5") # SEE ALSO -[cpsc-language-overview(7)](../cpsc-language-overview.7.md), [cpsc-function-is_num(7)](cpsc-function-is_num.7.md), [cpsc-function-num_to_str(7)](cpsc-function-num_to_str.7.md). +[cpsc-language-overview(7)](../cpsc-language-overview.7.md), [cpsc-keyword-default(7)](../keyword/cpsc-keyword-default.7.md), [cpsc-function-int(7)](cpsc-function-int.7.md), [cpsc-function-is_num(7)](cpsc-function-is_num.7.md), [cpsc-function-num_to_str(7)](cpsc-function-num_to_str.7.md). |
