langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,19 @@
using System;
using System.Console;
module IsNumeric
{
IsNumeric( input : string) : bool
{
mutable meh = 0.0; // I don't want it, not going to use it, why force me to declare it?
double.TryParse(input, out meh)
}
Main() : void
{
def num = "-1.2345E6";
def not = "abc45";
WriteLine($"$num is numeric: $(IsNumeric(num))");
WriteLine($"$not is numeric: $(IsNumeric(not))");
}
}

View file

@ -0,0 +1,37 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
numeric digits 20
loop n_ over getTestData()
-- could have used n_.datatype('N') directly here...
if isNumeric(n_) then msg = 'numeric'
else msg = 'not numeric'
say ('"'n_'"').right(25)':' msg
end n_
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Pointless in NetRexx; the DATATYPE built-in-function is more powerful!
method isNumeric(testString) public static returns boolean
return testString.datatype('N')
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getTestData() private static returns Rexx[]
-- Coercing numbers into the Rexx type has the effect of converting them to strings.
-- NetRexx will still perform arithmetic on Rexx strings if those strings represent numbers.
-- Notice that whitespace between the sign and the number are ignored even when inside a string constant
testData = [ Rexx -
' one and a half', 1, 1.5, 1.5e+27, ' 1 ', ' 1.5 ', ' 1.5e+27 ', -
'-one and a half', - 1, - 1.5, - 1.5e-27, ' - 1 ', '- 1.5 ', '- 1.5e-27 ', -
'+one and a half', + 1, + 1.5, + 1.5e+27, ' + 1 ', '+ 1.5 ', '+ 1.5e+27 ', -
'Math Constants', -
Math.PI, Math.E, -
-Math.PI, -Math.E, -
+Math.PI, +Math.E, -
'Numeric Constants', -
Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY -
]
return testData

View file

@ -0,0 +1,9 @@
let is_int s =
try ignore (int_of_string s); true
with _ -> false
let is_float s =
try ignore (float_of_string s); true
with _ -> false
let is_numeric s = is_int s || is_float s

View file

@ -0,0 +1,10 @@
if( [[NSScanner scannerWithString:@"-123.4e5"] scanFloat:NULL] )
NSLog( @"\"-123.4e5\" is numeric" );
else
NSLog( @"\"-123.4e5\" is not numeric" );
if( [[NSScanner scannerWithString:@"Not a number"] scanFloat:NULL] )
NSLog( @"\"Not a number\" is numeric" );
else
NSLog( @"\"Not a number\" is not numeric" );
// prints: "-123.4e5" is numeric
// prints: "Not a number" is not numeric

View file

@ -0,0 +1,9 @@
BOOL isNumeric(NSString *s)
{
NSScanner *sc = [NSScanner scannerWithString: s];
if ( [sc scanFloat:NULL] )
{
return [sc isAtEnd];
}
return NO;
}

View file

@ -0,0 +1,25 @@
BOOL isNumericI(NSString *s)
{
NSUInteger len = [s length];
NSUInteger i;
BOOL status = NO;
for(i=0; i < len; i++)
{
unichar singlechar = [s characterAtIndex: i];
if ( (singlechar == ' ') && (!status) )
{
continue;
}
if ( ( singlechar == '+' ||
singlechar == '-' ) && (!status) ) { status=YES; continue; }
if ( ( singlechar >= '0' ) &&
( singlechar <= '9' ) )
{
status = YES;
} else {
return NO;
}
}
return (i == len) && status;
}

View file

@ -0,0 +1,15 @@
function r = isnum(a)
if ( isnumeric(a) )
r = 1;
else
o = str2num(a);
r = !isempty(o);
endif
endfunction
% tests
disp(isnum(123)) % 1
disp(isnum("123")) % 1
disp(isnum("foo123")) % 0
disp(isnum("123bar")) % 0
disp(isnum("3.1415")) % 1

View file

@ -0,0 +1,8 @@
function r = isnum(a)
if ( isnumeric(a) )
r = 1;
else
o = str2double(a);
r = !isnan(o);
endif
endfunction

View file

@ -0,0 +1,3 @@
fun {IsNumeric S}
{String.isInt S} orelse {String.isFloat S}
end

View file

@ -0,0 +1,4 @@
isNumeric(s)={
my(t=type(eval(s)));
t == "t_INT" || t == "T_REAL"
};

View file

@ -0,0 +1,12 @@
is_numeric: procedure (text) returns (bit (1));
declare text character (*);
declare x float;
on conversion go to done;
get string(text) edit (x) (E(length(text),0));
return ('1'b);
done:
return ('0'b);
end is_numeric;

View file

@ -0,0 +1,11 @@
FUNCTION IsNumeric( value IN VARCHAR2 )
RETURN BOOLEAN
IS
help NUMBER;
BEGIN
help := to_number( value );
return( TRUE );
EXCEPTION
WHEN others THEN
return( FALSE );
END;

View file

@ -0,0 +1,5 @@
Value VARCHAR2( 10 ) := '123';
IF( IsNumeric( Value ) )
THEN
NULL;
END IF;

View file

@ -0,0 +1,17 @@
function IsNumeric(Value: string; const AllowFloat: Boolean): Boolean;
var
ValueInt: Integer;
ValueFloat: Extended;
ErrCode: Integer;
begin
// Check for integer: Val only accepts integers when passed integer param
Value := SysUtils.Trim(Value);
Val(Value, ValueInt, ErrCode);
Result := ErrCode = 0; // Val sets error code 0 if OK
if not Result and AllowFloat then
begin
// Check for float: Val accepts floats when passed float param
Val(Value, ValueFloat, ErrCode);
Result := ErrCode = 0; // Val sets error code 0 if OK
end;
end;

View file

@ -0,0 +1,4 @@
sub is-number( $term --> Bool ) {
$term ~~ /\d/ and +$term ~~ Numeric;
}
say "true" if is-number( 10111 );

View file

@ -0,0 +1,12 @@
int(0..1) is_number(string s)
{
array test = array_sscanf(s, "%s%f%s");
if (sizeof(test) == 3 && test[1] && !sizeof(test[0]) && !sizeof(test[2]) )
return true;
else
return false;
}
string num = "-1.234"
is_number(num);
-> true

View file

@ -0,0 +1,8 @@
function isNumeric ($x) {
try {
0 + $x | Out-Null
return $true
} catch {
return $false
}
}

View file

@ -0,0 +1,5 @@
function isNumeric ($x) {
$x2 = 0
$isNum = [System.Int32]::TryParse($x, [ref]$x2)
return $isNum
}

View file

@ -0,0 +1,54 @@
Procedure IsNumeric(InString.s, DecimalCharacter.c = '.')
#NotNumeric = #False
#IsNumeric = #True
InString = Trim(InString)
Protected IsDecimal, CaughtDecimal, CaughtE
Protected IsSignPresent, IsSignAllowed = #True, CountNumeric
Protected *CurrentChar.Character = @InString
While *CurrentChar\c
Select *CurrentChar\c
Case '0' To '9'
CountNumeric + 1
IsSignAllowed = #False
Case DecimalCharacter
If CaughtDecimal Or CaughtE Or CountNumeric = 0
ProcedureReturn #NotNumeric
EndIf
CountNumeric = 0
CaughtDecimal = #True
IsDecimal = #True
Case '-', '+'
If IsSignPresent Or Not IsSignAllowed: ProcedureReturn #NotNumeric: EndIf
IsSignPresent = #True
Case 'E', 'e'
If CaughtE Or CountNumeric = 0
ProcedureReturn #NotNumeric
EndIf
CaughtE = #True
CountNumeric = 0
CaughtDecimal = #False
IsSignPresent = #False
IsSignAllowed = #True
Default
ProcedureReturn #NotNumeric
EndSelect
*CurrentChar + SizeOf(Character)
Wend
If CountNumeric = 0: ProcedureReturn #NotNumeric: EndIf
ProcedureReturn #IsNumeric
EndProcedure
If OpenConsole()
PrintN("'+3183.31151E+321' = " + Str(IsNumeric("+3183.31151E+321")))
PrintN("'-123456789' = " + Str(IsNumeric("-123456789")))
PrintN("'123.45.6789+' = " + Str(IsNumeric("123.45.6789+")))
PrintN("'-e' = " + Str(IsNumeric("-e")))
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,35 @@
REBOL [
Title: "Is Numeric?"
Author: oofoe
Date: 2009-12-04
URL: http://rosettacode.org/wiki/IsNumeric
]
; Built-in.
numeric?: func [x][not error? try [to-decimal x]]
; Parse dialect for numbers.
sign: [0 1 "-"]
digit: charset "0123456789"
int: [some digit]
float: [int "." int]
number: [
sign float ["e" | "E"] sign int |
sign int ["e" | "E"] sign int |
sign float |
sign int
]
pnumeric?: func [x][parse x number]
; Test cases.
cases: parse {
10 -99
10.43 -12.04
1e99 1.0e10 -10e3 -9.12e7 2e-4 -3.4E-5
3phase Garkenhammer e n3v3r phase3
} none
foreach x cases [print [x numeric? x pnumeric? x]]

View file

@ -0,0 +1 @@
"123" isNumber?

View file

@ -0,0 +1,24 @@
print isNumeric("123")
print isNumeric("1ab")
' ------------------------
' Numeric Check
' 0 = bad
' 1 = good
' ------------------------
FUNCTION isNumeric(f$)
isNumeric = 1
f$ = trim$(f$)
if left$(f$,1) = "-" or left$(f$,1) = "+" then f$ = mid$(f$,2)
for i = 1 to len(f$)
if mid$(f$,i,1) = "." then
if dot$ = "." then isNumeric = 0
dot$ = "."
goto [nxtDigit]
end if
if mid$(f$,i,1) = "," then goto [nxtDigit]
if mid$(f$,i,1) < "0" then isNumeric = 0
if mid$(f$,i,1) > "9" then isNumeric = 0
[nxtDigit]
next i
END FUNCTION

View file

@ -0,0 +1,20 @@
define('nchk(str)') :(nchk_end)
nchk convert(str,'real') :s(return)f(freturn)
nchk_end
* # Wrapper for testing
define('isnum(str)') :(isnum_end)
isnum isnum = 'F'; isnum = nchk(str) 'T'
isnum = isnum ': ' str :(return)
isnum_end
* # Test and display
output = isnum('123')
output = isnum('123.0')
output = isnum('123.')
output = isnum('-123')
output = isnum('3.14159')
output = isnum('1.2.3')
output = isnum('abc')
output = isnum('A440')
end

View file

@ -0,0 +1,7 @@
declare @s varchar(10)
set @s = '1234.56'
print isnumeric(@s) --prints 1 if numeric, 0 if not.
if isnumeric(@s)=1 begin print 'Numeric' end
else print 'Non-numeric'

View file

@ -0,0 +1,10 @@
(* this function only recognizes integers in decimal format *)
fun isInteger s = case Int.scan StringCvt.DEC Substring.getc (Substring.full s) of
SOME (_,subs) => Substring.isEmpty subs
| NONE => false
fun isReal s = case Real.scan Substring.getc (Substring.full s) of
SOME (_,subs) => Substring.isEmpty subs
| NONE => false
fun isNumeric s = isInteger s orelse isReal s

View file

@ -0,0 +1,10 @@
[ ( string -- flag )
>number nip ] is isNumeric
( Some tests )
decimal
" 100" isNumeric . ( succeeds, 100 is a valid decimal integer )
" 100.21" isNumeric . ( fails, 100.21 is not an integer)
" a" isNumeric . ( fails, 'a' is not a valid integer in the decimal base )
" $a" isNumeric . ( succeeds, because $ is a valid override prefix )
( denoting that the following character is a hexadecimal number )

View file

@ -0,0 +1,6 @@
:IS_NUMERIC:
if (Num_Eval(SUPPRESS)==0 && Cur_Char != '0') {
Return(FALSE)
} else {
Return(TRUE)
}

View file

@ -0,0 +1,5 @@
Dim Value As String = "+123"
If IsNumeric(Value) Then
PRINT "It is numeric."
End If