Determine if a string is numeric: Difference between revisions
Content added Content deleted
(→{{header|MATLAB}}: mark incorrect) |
(Added Bracmat example) |
||
Line 154: | Line 154: | ||
'12+3' is NOT a number |
'12+3' is NOT a number |
||
'end' is NOT a number</pre> |
'end' is NOT a number</pre> |
||
=={{header|Bracmat}}== |
|||
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:/ |
|||
F |
|||
260780243875083/35587980:/ |
|||
S |
|||
247/30:~/# |
|||
F |
|||
80000000000:~/# |
|||
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. |
|||
<lang>@("1.000-4E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
F |
|||
@("1.0004E-54328":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
S |
|||
@("-464641.0004E-54328":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
S |
|||
@("1/2.0004E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
F |
|||
@("1357E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
S |
|||
@("1357e0":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
S |
|||
@("13579":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
S |
|||
@("1.246":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
S |
|||
@("0.0":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
S |
|||
@("0.0000":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) |
|||
S</lang> |
|||
To do computations with such "floating point strings" you would have to convert such strings to fractional representations first. |
|||
<lang>(float2fraction= |
|||
integerPart decimalPart d1 dn exp sign |
|||
. @( !arg |
|||
: ~/#?integerPart |
|||
( &0:?decimalPart:?d1:?dn |
|||
| "." |
|||
[?d1 |
|||
(|? 0|`) |
|||
( &0:?decimalPart |
|||
| ~/#?decimalPart:>0 |
|||
) |
|||
[?dn |
|||
) |
|||
( &0:?exp |
|||
| (E|e) ~/#?exp |
|||
) |
|||
) |
|||
& ( !integerPart*(-1:?sign):>0:?integerPart |
|||
| 1:?sign |
|||
) |
|||
& !sign*(!integerPart+!decimalPart*10^(!d1+-1*!dn))*10^!exp |
|||
); |
|||
( out$float2fraction$"1.2" |
|||
& out$float2fraction$"1.02" |
|||
& out$float2fraction$"1.01" |
|||
& out$float2fraction$"10.01" |
|||
& out$float2fraction$"10.01e10" |
|||
& out$float2fraction$"10.01e1" |
|||
& out$float2fraction$"10.01e2" |
|||
& out$float2fraction$"10.01e-2" |
|||
& out$float2fraction$"-10.01e-2" |
|||
& out$float2fraction$"-10e-2" |
|||
& out$float2fraction$"0.000" |
|||
); |
|||
</lang> |
|||
Output: |
|||
<lang>6/5 |
|||
51/50 |
|||
101/100 |
|||
1001/100 |
|||
100100000000 |
|||
1001/10 |
|||
1001 |
|||
1001/10000 |
|||
-1001/10000 |
|||
-1/10 |
|||
0</lang> |
|||
=={{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). |