How to resolve the algorithm Determine if a string is numeric step by step in the Forth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determine if a string is numeric step by step in the Forth programming language

Table of Contents

Problem Statement

Create a boolean function which takes in a string and tells whether it is a numeric string (floating point and negative numbers included) in the syntax the language uses for numeric literals or numbers converted from strings.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determine if a string is numeric step by step in the Forth programming language

Source code in the forth programming language

: is-numeric ( addr len -- )
  2dup snumber? ?dup if      \ not standard, but >number is more cumbersome to use
   0< if
     -rot type ."  as integer = " .
   else
     2swap type ."  as double = " <# #s #> type
   then
  else 2dup >float if
    type ."  as float = " f.
  else
    type ."  isn't numeric in base " base @ dec.
  then then ;

s" 1234" is-numeric    \ 1234 as integer = 1234
s" 1234." is-numeric    \ 1234. as double = 1234
s" 1234e" is-numeric    \ 1234e as float = 1234.
s" $1234" is-numeric    \ $1234 as integer = 4660  ( hex literal )
s" %1010" is-numeric    \ %1010 as integer = 10  ( binary literal )
s" beef" is-numeric    \ beef isn't numeric in base 10
hex
s" beef" is-numeric    \ beef as integer = BEEF
s" &1234" is-numeric    \ &1234 as integer = 4D2 ( decimal literal )


  

You may also check:How to resolve the algorithm Inverted syntax step by step in the Phix programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the Maple programming language