new files

This commit is contained in:
Ingy döt Net 2013-04-10 12:38:42 -07:00
parent 3af7344581
commit 86c034bb8b
1364 changed files with 21352 additions and 0 deletions

View file

@ -0,0 +1 @@
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.

View file

@ -0,0 +1,2 @@
---
note: Text processing

View file

@ -0,0 +1,2 @@
$ awk 'function isnum(x){return(x==x+0)}BEGIN{print isnum("hello"),isnum("-42")}'
0 1

View file

@ -0,0 +1,4 @@
public function isNumeric(num:String):Boolean
{
return !isNaN(parseInt(num));
}

View file

@ -0,0 +1,3 @@
package Numeric_Tests is
function Is_Numeric (Item : in String) return Boolean;
end Numeric_Tests;

View file

@ -0,0 +1,11 @@
package body Numeric_Tests is
function Is_Numeric (Item : in String) return Boolean is
Dummy : Float;
begin
Dummy := Float'Value (Item);
return True;
exception
when others =>
return False;
end Is_Numeric;
end Numeric_Tests;

View file

@ -0,0 +1,12 @@
with Ada.Text_Io; use Ada.Text_Io;
with Numeric_Tests; use Numeric_Tests;
procedure Is_Numeric_Test is
S1 : String := "152";
S2 : String := "-3.1415926";
S3 : String := "Foo123";
begin
Put_Line(S1 & " results in " & Boolean'Image(Is_Numeric(S1)));
Put_Line(S2 & " results in " & Boolean'Image(Is_Numeric(S2)));
Put_Line(S3 & " results in " & Boolean'Image(Is_Numeric(S3)));
end Is_Numeric_Test;

View file

@ -0,0 +1,6 @@
10 INPUT "Enter a string";S$:GOSUB 1000
20 IF R THEN PRINT "Is num" ELSE PRINT"Not num"
99 END
1000 T1=VAL(S$):T1$=STR$(T1)
1010 R=T1$=S$ OR T1$=" "+S$
1099 RETURN

View file

@ -0,0 +1,10 @@
#include <ctype.h>
#include <stdlib.h>
int isNumeric (const char * s)
{
if (s == NULL || *s == '\0' || isspace(*s))
return 0;
char * p;
strtod (s, &p);
return *p == '\0';
}

View file

@ -0,0 +1,7 @@
(defn numeric? [s]
(if-let [s (seq s)]
(let [s (if (= (first s) \-) (next s) s)
s (drop-while #(Character/isDigit %) s)
s (if (= (first s) \.) (next s) s)
s (drop-while #(Character/isDigit %) s)]
(empty? s))))

View file

@ -0,0 +1 @@
(numeric? [\1 \2 \3]) ;; yields logical true

View file

@ -0,0 +1,2 @@
console.log (isFinite(s) for s in [5, "5", "-5", "5", "5e5", 0]) # all true
console.log (isFinite(s) for s in [NaN, "fred", "###"]) # all false

View file

@ -0,0 +1,4 @@
is_numeric(L) ->
Float = (catch erlang:list_to_float(L)),
Int = (catch erlang:list_to_integer(L)),
is_number(Float) orelse is_number(Int).

View file

@ -0,0 +1,22 @@
: 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 )

View file

@ -0,0 +1,9 @@
FUNCTION is_numeric(string)
IMPLICIT NONE
CHARACTER(len=*), INTENT(IN) :: string
LOGICAL :: is_numeric
REAL :: x
INTEGER :: e
READ(string,*,IOSTAT=e) x
is_numeric = e == 0
END FUNCTION is_numeric

View file

@ -0,0 +1,6 @@
import "strconv"
func IsNumeric(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}

View file

@ -0,0 +1,10 @@
isInteger s = case reads s :: [(Integer, String)] of
[(_, "")] -> True
_ -> False
isDouble s = case reads s :: [(Double, String)] of
[(_, "")] -> True
_ -> False
isNumeric :: String -> Bool
isNumeric s = isInteger s || isDouble s

View file

@ -0,0 +1,4 @@
areDigits = all isDigit
isDigit selects ASCII digits i.e. '0'..'9'
isOctDigit selects '0'..'7'
isHexDigit selects '0'..'9','A'..'F','a'..'f'

View file

@ -0,0 +1,10 @@
public boolean isNumeric(String input) {
try {
Integer.parseInt(input);
return true;
}
catch (NumberFormatException e) {
// s is not numeric
return false;
}
}

View file

@ -0,0 +1,10 @@
private static final boolean isNumeric(final String s) {
if (s == null || s.isEmpty()) return false;
for (int x = 0; x < s.length(); x++) {
final char c = s.charAt(x);
if (x == 0 && (c == '-')) continue; // negative
if ((c >= '0') && (c <= '9')) continue; // 0 - 9
return false; // invalid
}
return true; // valid
}

View file

@ -0,0 +1,3 @@
public static boolean isNumeric(String inputData) {
return inputData.matches("[-+]?\\d+(\\.\\d+)?");
}

View file

@ -0,0 +1,6 @@
public static boolean isNumeric(String inputData) {
NumberFormat formatter = NumberFormat.getInstance();
ParsePosition pos = new ParsePosition(0);
formatter.parse(inputData, pos);
return inputData.length() == pos.getIndex();
}

View file

@ -0,0 +1,4 @@
public static boolean isNumeric(String inputData) {
Scanner sc = new Scanner(inputData);
return sc.hasNextInt();
}

View file

@ -0,0 +1,6 @@
var value = "123.45e7"; // Assign string literal to value
if (isFinite(value)) {
// value is a number
}
//Or, in web browser in address field:
// javascript:value="123.45e4"; if(isFinite(value)) {alert('numeric')} else {alert('non-numeric')}

View file

@ -0,0 +1,3 @@
if tonumber(a) ~= nil then
--it's a number
end;

View file

@ -0,0 +1,5 @@
<?php
$string = '123';
if(is_numeric(trim($string))) {
}
?>

View file

@ -0,0 +1,2 @@
use Scalar::Util qw(looks_like_number);
print looks_like_number($str) ? "numeric" : "not numeric\n";

View file

@ -0,0 +1,8 @@
if (/\D/) { print "has nondigits\n" }
if (/^\d+\z/) { print "is a whole number\n" }
if (/^-?\d+\z/) { print "is an integer\n" }
if (/^[+-]?\d+\z/) { print "is a +/- integer\n" }
if (/^-?\d+\.?\d*\z/) { print "is a real number\n" }
if (/^-?(?:\d+(?:\.\d*)?&\.\d+)\z/) { print "is a decimal number\n" }
if (/^([+-]?)(?=\d&\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?\z/)
{ print "a C float\n" }

View file

@ -0,0 +1,15 @@
sub getnum {
use POSIX;
my $str = shift;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
$! = 0;
my($num, $unparsed) = strtod($str);
if (($str eq '') && ($unparsed != 0) && $!) {
return undef;
} else {
return $num;
}
}
sub is_numeric { defined getnum($_[0]) }

View file

@ -0,0 +1,8 @@
: (format "123")
-> 123
: (format "123a45")
-> NIL
: (format "-123.45" 4)
-> 1234500

View file

@ -0,0 +1,5 @@
s = '123'
try:
i = float(s)
except ValueError, TypeError:
print 'not numeric'

View file

@ -0,0 +1,3 @@
s = '123'
if s.isdigit():
# numeric

View file

@ -0,0 +1,28 @@
def is_numeric(lit):
'Return value of numeric literal string or ValueError exception'
# Handle '0'
if lit == '0': return 0
# Hex/Binary
litneg = lit[1:] if lit[0] == '-' else lit
if litneg[0] == '0':
if litneg[1] in 'xX':
return int(lit,16)
elif litneg[1] in 'bB':
return int(lit,2)
else:
try:
return int(lit,8)
except ValueError:
pass
# Int/Float/Complex
try:
return int(lit)
except ValueError:
pass
try:
return float(lit)
except ValueError:
pass
return complex(lit)

View file

@ -0,0 +1,16 @@
>>> for s in ['0', '00', '123', '-123.', '-123e-4', '0123', '0x1a1', '-123+4.5j', '0b0101', '0.123', '-0xabc', '-0b101']:
print "%14s -> %-14s %s" % ('"'+s+'"', is_numeric(s), type(is_numeric(s)))
"0" -> 0 <type 'int'>
"00" -> 0 <type 'int'>
"123" -> 123 <type 'int'>
"-123." -> -123.0 <type 'float'>
"-123e-4" -> -0.0123 <type 'float'>
"0123" -> 83 <type 'int'>
"0x1a1" -> 417 <type 'int'>
"-123+4.5j" -> (-123+4.5j) <type 'complex'>
"0b0101" -> 5 <type 'int'>
"0.123" -> 0.123 <type 'float'>
"-0xabc" -> -2748 <type 'int'>
"-0b101" -> -5 <type 'int'>
>>>

View file

@ -0,0 +1,3 @@
> strings <- c("152", "-3.1415926", "Foo123")
> suppressWarnings(!is.na(as.numeric(strings)))
[1] TRUE TRUE FALSE

View file

@ -0,0 +1,46 @@
/*REXX program to determine if a string is numeric. */
yyy=' -123.78' /*or some such.*/
/*strings below are all numeric (REXX).*/
zzz=' -123.78 '
zzz='-123.78'
zzz='2'
zzz="2"
zzz=2
zzz='000000000004'
zzz='+5'
zzz=' +6 '
zzz=' + 7 '
zzz=' - 8 '
zzz=' - .9'
zzz='- 19.'
zzz='.7'
zzz='2e3'
zzz=47e567
zzz='2e-3'
zzz='1.2e1'
zzz=' .2E6'
zzz=' 2.e5 '
zzz=' +1.2E0002 '
zzz=' +1.2e+002 '
zzz=' +0000001.200e+002 '
zzz=' - 000001.200e+002 '
zzz=' - 000008.201e-00000000000000002 '
/*Note: some REXX interpreters allow use of tab chars as blanks. */
/*all statements below are equivalent.*/
if \datatype(yyy,'n') then say 'oops, not numeric:' yyy
if \datatype(yyy,'N') then say 'oops, not numeric:' yyy
if ¬datatype(yyy,'N') then say 'oops, not numeric:' yyy
if ¬datatype(yyy,'numeric') then say 'oops, not numeric:' yyy
if ¬datatype(yyy,'nimrod.') then say 'oops, not numeric:' yyy
if datatype(yyy)\=='NUM' then say 'oops, not numeric:' yyy
if datatype(yyy)/=='NUM' then say 'oops, not numeric:' yyy
if datatype(yyy)¬=='NUM' then say 'oops, not numeric:' yyy
if datatype(yyy)¬= 'NUM' then say 'oops, not numeric:' yyy
/*note: REXX only looks at the first char for DATATYPE's 2nd arg. */
/*note: some REXX interpreters don't support the ¬ (not) character.*/

View file

@ -0,0 +1 @@
(define (string-numeric? s) (number? (string->number s)))

View file

@ -0,0 +1,9 @@
def is_numeric?(s)
begin
Float(s)
rescue
false # not numeric
else
true # numeric
end
end

View file

@ -0,0 +1,3 @@
def is_numeric?(s)
!!Float(s) rescue false
end

View file

@ -0,0 +1 @@
def isNumeric(input: String): Boolean = input.forall(_.isDigit)

View file

@ -0,0 +1 @@
(define (numeric? s) (string->number s))

View file

@ -0,0 +1,20 @@
String extend [
realIsNumeric [
(self first = $+) |
(self first = $-)
ifTrue: [
^ (self allButFirst) isNumeric
]
ifFalse: [
^ self isNumeric
]
]
]
{ '1234'. "true"
'3.14'. '+3.8111'. "true"
'+45'. "true"
'-3.78'. "true"
'-3.78.23'. "false"
'123e3' "false: the notation is not recognized"
} do: [ :a | a realIsNumeric printNl ]

View file

@ -0,0 +1 @@
(Number readFrom:(aString readStream) onError:[nil]) notNil

View file

@ -0,0 +1 @@
(Scanner scanNumberFrom:(aString readStream)) notNil

View file

@ -0,0 +1,3 @@
if {
[string is double -strict $varname]
} then { ... }