Talk:Determine if a string is numeric: Difference between revisions

(→‎Exact definition of IsNumeric?: added comments about leading signs. -- ~~~~)
 
(5 intermediate revisions by 5 users not shown)
Line 34:
For those who don't know VB: How exactly is IsNumeric defined?
For example: Is leading/trailing whitespace allowed (i.e. " 123" or "123 ")?
Does it also accept flotingfloating point values (e.g. "2.5" or "1e5")?
What about thousands separators (e.g. "10,000")? Is that locale-dependent?
Are numbers in other bases (e.g. hexadecimal) allowed (assuming VB supports them otherwise)?
Line 124:
 
The IsNumeric call will not always return a correct answer. Try "1234+" - IsNumeric will return True. Then pass this same value as an argument to Convert.ToDouble: it will fail "unhandled exception". There's no option but to loop thru the String character by character and check that each one is within range. --[[User:LazyZee|LazyZee]] 17:59, 19 October 2012 (UTC)
 
== R ==
 
The solution will not work correctly if the string contains "NA" (not a number). But NA can be used in numeric calculations. --[[User:Sigbert|Sigbert]] ([[User talk:Sigbert|talk]]) 13:01, 16 February 2015 (UTC)
 
== AWK code is wrong ==
 
<pre>
awk 'BEGIN { x = "01"; print x == x+0 }'
0
awk 'BEGIN { x = "+1"; print x == x+0 }'
0
awk 'BEGIN { x = "1x"; print x == x+0 }'
0
</pre>
But
<pre>
awk 'BEGIN { x = "1x"; print x+1 }'
2
</pre>
 
== Assembly implementation is harder than I thought==
 
The way I see it, the code will need to do the following:
 
Read the first character.
* If it's a null terminator, the program ends and the string is not numeric.
* If it's a minus sign, that's valid and so we can continue
* If it's a decimal point, that's valid and we can continue. However, the presence of a decimal point must be recorded. If the string contains a second decimal point it stops being numeric.
* If it's not a number, at this point the string is not numeric. Otherwise continue.
 
Read the second character.
* If it's the terminator, the string is numeric if the first character was a numeral. If the first character was a minus sign or decimal point, the string is not numeric.
* If the second character is a numeral, continue.
* If the second character is a decimal point, continue if we haven't seen one already. If there is more than one decimal point the string is not numeric.
* If the second character is anything else, the string is not numeric.
 
Loop through the rest of the string.
* If we encounter the terminator, the string is numeric.
* If we encounter a numeral, continue.
* If we encounter a second decimal point, the string is not numeric.
* If we encounter anything besides a numeral, the null terminator, or the first occurrence of a decimal point, the string is not numeric.
 
I've attempted this in both 8086 and z80 but it ends up looking like spaghetti no matter what I do.
 
: @[[User: Puppydrum64]]: Uhm, is this task even applicable for assembly? I don’t thinks so. You know, depending on the assembler used there are ''several'' ways to write numeric literals. In NASM, for instance, I can write <tt>0b0110_1101</tt>. There simply isn’t ''a/the'' programming language “8086 assembly” ''defining'' how to write numeric literals. What you are attempting to do, though, is rather writing an assembly function that accepts a null-terminated string that represents a valid numeric literal ''in C'' (and many ''other'' programming languages), but not “8086 assembly”, you know what I mean? [[User:Root|Root]] ([[User talk:Root|talk]]) 18:10, 20 August 2021 (UTC)
 
I get what you mean. I figured at least attempting something that represents a valid numeric literal in higher-level languages was as close as I could get. Throwing in the towel and just saying "This is assembly, there are no rules" would disqualify it from almost everything. --[[User:Puppydrum64|Puppydrum64]] ([[User talk:Puppydrum64|talk]]) 17:45, 15 September 2021 (UTC)
1,489

edits