This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1 @@
: numeric? ( string -- ? ) string>number >boolean ;

View file

@ -0,0 +1,24 @@
class Main
{
// function to see if str contains a number of any of built-in types
static Bool readNum (Str str)
{
int := Int.fromStr (str, 10, false) // use base 10
if (int != null) return true
float := Float.fromStr (str, false)
if (float != null) return true
decimal := Decimal.fromStr (str, false)
if (decimal != null) return true
return false
}
public static Void main ()
{
echo ("For '2': " + readNum ("2"))
echo ("For '-2': " + readNum ("-2"))
echo ("For '2.5': " + readNum ("2.5"))
echo ("For '2a5': " + readNum ("2a5"))
echo ("For '-2.1e5': " + readNum ("-2.1e5"))
}
}

View file

@ -0,0 +1,9 @@
def isNumeric = {
def formatter = java.text.NumberFormat.instance
def pos = [0] as java.text.ParsePosition
formatter.parse(it, pos)
// if parse position index has moved to end of string
// them the whole string was numeric
pos.index == it.size()
}

View file

@ -0,0 +1,5 @@
println isNumeric('1')
println isNumeric('-.555')
println isNumeric('1,000,000')
println isNumeric(' 1 1 1 1 ')
println isNumeric('abcdef')

View file

@ -0,0 +1,27 @@
! = bin + 2*int + 4*flt + 8*oct +16*hex + 32*sci
isNumeric("1001") ! 27 = 1 1 0 1 1 0
isNumeric("123") ! 26 = 0 1 0 1 1 0
isNumeric("1E78") ! 48 = 0 0 0 0 1 1
isNumeric("-0.123") ! 4 = 0 0 1 0 0 1
isNumeric("-123.456e-78") ! 32 = 0 0 0 0 0 1
isNumeric(" 123") ! 0: leading blank
isNumeric("-123.456f-78") ! 0: illegal character f
FUNCTION isNumeric(string) ! true ( > 0 ), no leading/trailing blanks
CHARACTER string
b = INDEX(string, "[01]+", 128, Lbin) ! Lbin returns length found
i = INDEX(string, "-?\d+", 128, Lint) ! regular expression: 128
f = INDEX(string, "-?\d+\.\d*", 128, Lflt)
o = INDEX(string, "[0-7]+", 128, Loct)
h = INDEX(string, "[0-9A-F]+", 128, Lhex) ! case sensitive: 1+128
s = INDEX(string, "-?\d+\.*\d*E[+-]*\d*", 128, Lsci)
IF(anywhere) THEN ! 0 (false) by default
isNumeric = ( b > 0 ) + 2*( i > 0 ) + 4*( f > 0 ) + 8*( o > 0 ) + 16*( h > 0 ) + 32*( s > 0 )
ELSEIF(boolean) THEN ! 0 (false) by default
isNumeric = ( b + i + f + o + h + s ) > 0 ! this would return 0 or 1
ELSE
L = LEN(string)
isNumeric = (Lbin==L) + 2*(Lint==L) + 4*(Lflt==L) + 8*(Loct==L) + 16*(Lhex==L) + 32*(Lsci==L)
ENDIF
END

View file

@ -0,0 +1,6 @@
function isnumeric,input
on_ioerror, false
test = double(input)
return, 1
false: return, 0
end

View file

@ -0,0 +1,4 @@
if isnumeric('-123.45e-2') then print, 'yes' else print, 'no'
; ==> yes
if isnumeric('picklejuice') then print, 'yes' else print, 'no'
; ==> no

View file

@ -0,0 +1 @@
write(image(x), if numeric(x) then " is numeric." else " is not numeric")

View file

@ -0,0 +1,4 @@
isNumeric=: _ ~: _ ". ]
isNumericScalar=: 1 -: isNumeric
TXT=: ,&' a scalar numeric value.' &.> ' is not';' represents'
sayIsNumericScalar=: , TXT {::~ isNumericScalar

View file

@ -0,0 +1,10 @@
isNumeric '152'
1
isNumeric '152 -3.1415926 Foo123'
1 1 0
isNumeric '42 foo42 4.2e1 4200e-2 126r3 16b2a 42foo'
1 0 1 1 1 1 0
isNumericScalar '152 -3.1415926 Foo123'
0
sayIsNumericScalar '-3.1415926'
-3.1415926 represents a scalar numeric value.

View file

@ -0,0 +1,56 @@
DATA "PI", "0123", "-0123", "12.30", "-12.30", "123!", "0"
DATA "0.0", ".123", "-.123", "12E3", "12E-3", "12+3", "end"
while n$ <> "end"
read n$
print n$, IsNumber(n$)
wend
end
function IsNumber(string$)
on error goto [NotNumber]
string$ = trim$(string$)
'check for float overflow
n = val(string$)
'assume it is number and try to prove wrong
IsNumber = 1
for i = 1 to len(string$)
select case mid$(string$, i, 1)
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
HasNumeric = 1 'to check if there are any digits
case "e", "E"
'"e" must not occur more than once
'must not occur before digits
if HasE > 0 or HasNumeric = 0 then
IsNumber = 0
exit for
end if
HasE = i 'store position of "e"
HasNumeric = 0 'needs numbers after "e"
case "-", "+"
'must be either first character or immediately after "e"
'(HasE = 0 if no occurrences yet)
if HasE <> i-1 then
IsNumber = 0
exit for
end if
case "."
'must not have previous points and must not come after "e"
if HasE <> 0 or HasPoint <> 0 then
IsNumber = 0
exit for
end if
HasPoint = 1
case else
'no other characters allowed
IsNumber = 0
exit for
end select
next i
'must have digits
if HasNumeric = 0 then IsNumber = 0
[NotNumber]
end function

View file

@ -0,0 +1,2 @@
"123457".is_integer.println;
// write TRUE on stdin

View file

@ -0,0 +1 @@
show number? "-1.23 ; true

View file

@ -0,0 +1,10 @@
fn isNumeric str =
(
try
(
(str as integer) != undefined
)
catch(false)
)
isNumeric "123"

View file

@ -0,0 +1 @@
NumberQ[ToExpression["02553352000242"]]

View file

@ -0,0 +1 @@
numberp(parse_string("170141183460469231731687303715884105727"));

View file

@ -0,0 +1,125 @@
import java.text.NumberFormat
import java.text.ParsePosition
import java.util.Scanner
# this first example relies on catching an exception,
# which is bad style and poorly performing in Java
def is_numeric?(s:string)
begin
Double.parseDouble(s)
return true
rescue
return false
end
end
puts '123 is numeric' if is_numeric?('123')
puts '-123 is numeric' if is_numeric?('-123')
puts '123.1 is numeric' if is_numeric?('123.1')
puts 'nil is not numeric' unless is_numeric?(nil)
puts "'' is not numeric" unless is_numeric?('')
puts 'abc is not numeric' unless is_numeric?('abc')
puts '123- is not numeric' unless is_numeric?('123-')
puts '1.2.3 is not numeric' unless is_numeric?('1.2.3')
# check every element of the string
def is_numeric2?(s: string)
if (s == nil || s.isEmpty())
return false
end
if (!s.startsWith('-'))
if s.contains('-')
return false
end
end
0.upto(s.length()-1) do |x|
c = s.charAt(x)
if ((x == 0) && (c == '-'.charAt(0)))
# negative number
elsif (c == '.'.charAt(0))
if (s.indexOf('.', x) > -1)
return false # more than one period
end
elsif (!Character.isDigit(c))
return false
end
end
true
end
puts '123 is numeric' if is_numeric2?('123')
puts '-123 is numeric' if is_numeric2?('-123')
puts '123.1 is numeric' if is_numeric2?('123.1')
puts 'nil is not numeric' unless is_numeric2?(nil)
puts "'' is not numeric" unless is_numeric2?('')
puts 'abc is not numeric' unless is_numeric2?('abc')
puts '123- is not numeric' unless is_numeric2?('123-')
puts '1.2.3 is not numeric' unless is_numeric2?('1.2.3')
# use a regular expression
def is_numeric3?(s:string)
s == nil || s.matches("[-+]?\\d+(\\.\\d+)?")
end
puts '123 is numeric' if is_numeric3?('123')
puts '-123 is numeric' if is_numeric3?('-123')
puts '123.1 is numeric' if is_numeric3?('123.1')
puts 'nil is not numeric' unless is_numeric3?(nil)
puts "'' is not numeric" unless is_numeric3?('')
puts 'abc is not numeric' unless is_numeric3?('abc')
puts '123- is not numeric' unless is_numeric3?('123-')
puts '1.2.3 is not numeric' unless is_numeric3?('1.2.3')
# use the positional parser in the java.text.NumberFormat object
# (a more robust solution). If, after parsing, the parse position is at
# the end of the string, we can deduce that the entire string was a
# valid number.
def is_numeric4?(s:string)
return false if s == nil
formatter = NumberFormat.getInstance()
pos = ParsePosition.new(0)
formatter.parse(s, pos)
s.length() == pos.getIndex()
end
puts '123 is numeric' if is_numeric4?('123')
puts '-123 is numeric' if is_numeric4?('-123')
puts '123.1 is numeric' if is_numeric4?('123.1')
puts 'nil is not numeric' unless is_numeric4?(nil)
puts "'' is not numeric" unless is_numeric4?('')
puts 'abc is not numeric' unless is_numeric4?('abc')
puts '123- is not numeric' unless is_numeric4?('123-')
puts '1.2.3 is not numeric' unless is_numeric4?('1.2.3')
# use the java.util.Scanner object. Very useful if you have to
# scan multiple entries. Scanner also has similar methods for longs,
# shorts, bytes, doubles, floats, BigIntegers, and BigDecimals as well
# as methods for integral types where you may input a base/radix other than
# 10 (10 is the default, which can be changed using the useRadix method).
def is_numeric5?(s:string)
return false if s == nil
Scanner sc = Scanner.new(s)
sc.hasNextDouble()
end
puts '123 is numeric' if is_numeric5?('123')
puts '-123 is numeric' if is_numeric5?('-123')
puts '123.1 is numeric' if is_numeric5?('123.1')
puts 'nil is not numeric' unless is_numeric5?(nil)
puts "'' is not numeric" unless is_numeric5?('')
puts 'abc is not numeric' unless is_numeric5?('abc')
puts '123- is not numeric' unless is_numeric5?('123-')
puts '1.2.3 is not numeric' unless is_numeric5?('1.2.3')

View file

@ -0,0 +1,25 @@
MODULE Numeric EXPORTS Main;
IMPORT IO, Fmt, Text;
PROCEDURE isNumeric(s: TEXT): BOOLEAN =
BEGIN
FOR i := 0 TO Text.Length(s) DO
WITH char = Text.GetChar(s, i) DO
IF i = 0 AND char = '-' THEN
EXIT;
END;
IF char >= '0' AND char <= '9' THEN
EXIT;
END;
RETURN FALSE;
END;
END;
RETURN TRUE;
END isNumeric;
BEGIN
IO.Put("isNumeric(152) = " & Fmt.Bool(isNumeric("152")) & "\n");
IO.Put("isNumeric(-3.1415926) = " & Fmt.Bool(isNumeric("-3.1415926")) & "\n");
IO.Put("isNumeric(Foo123) = " & Fmt.Bool(isNumeric("Foo123")) & "\n");
END Numeric.