Determine if a string is numeric: Difference between revisions
Content added Content deleted
(Updated D entry) |
|||
Line 157: | Line 157: | ||
To check whether a string is a number, a fraction or an integer, use the patterns <code>#</code>, <code>/</code> and <code>~/#</code> ("not a fraction and yet a number"). In the pattern matching examples below (which can be typed in at the Bracmat prompt) <code>F</code> denotes 'failure' and <code>S</code> denotes 'success'. |
To check whether a string is a number, a fraction or an integer, use the patterns <code>#</code>, <code>/</code> and <code>~/#</code> ("not a fraction and yet a number"). In the pattern matching examples below (which can be typed in at the Bracmat prompt) <code>F</code> denotes 'failure' and <code>S</code> denotes 'success'. |
||
<lang>43257349578692:/ |
<lang bracmat>43257349578692:/ |
||
F |
F |
||
Line 169: | Line 169: | ||
S</lang> |
S</lang> |
||
Bracmat doesn't do floating point computations (historical reason: the Acorn Risc Machine a.k.a. ARM processor in the Archimedes computer did not have an FPU), but the pattern <code>~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)</code> recognises string representations of floating point numbers. |
Bracmat doesn't do floating point computations (historical reason: the Acorn Risc Machine a.k.a. ARM processor in the Archimedes computer did not have an FPU), but the pattern <code>~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)</code> recognises string representations of floating point numbers. |
||
<lang>@("1.000-4E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
<lang bracmat>@("1.000-4E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
||
F |
F |
||
Line 200: | Line 200: | ||
To do computations with such "floating point strings" you would have to convert such strings to fractional representations first. |
To do computations with such "floating point strings" you would have to convert such strings to fractional representations first. |
||
<lang>(float2fraction= |
<lang bracmat>(float2fraction= |
||
integerPart decimalPart d1 dn exp sign |
integerPart decimalPart d1 dn exp sign |
||
. @( !arg |
. @( !arg |
||
Line 237: | Line 237: | ||
</lang> |
</lang> |
||
Output: |
Output: |
||
< |
<pre>6/5 |
||
51/50 |
51/50 |
||
101/100 |
101/100 |
||
Line 247: | Line 247: | ||
-1001/10000 |
-1001/10000 |
||
-1/10 |
-1/10 |
||
0</ |
0</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Returns true (non-zero) if character-string parameter represents a signed or unsigned floating-point number. Otherwise returns false (zero). |
Returns true (non-zero) if character-string parameter represents a signed or unsigned floating-point number. Otherwise returns false (zero). |