This is no true. There is an error, since if I write this code:
msgbox str(Sqrt(-5))
I get
-1.#IND
This error is not very important unless you pretend to detect NaN numbers in your program. Both Nan and Infinity use “#” when converting to string.
I’ve created a function like this:
Function IsNotANumber (x as double) as Boolean
if Str(x).InStr("#") = 0 Then Return False else Return True
end function
This works very well. But, can I trust Xojo will keep this conversion to string for ever?
Have you created your own function for this? How do you do it?
Should I use “IsNumeric”?
Thanks in advance,
Yes, I’m in Windows but the program pretends to be Mac & Windows.
And I know that this is not a simple thing.
That’s why I ask for help, because I fear that my function may fail very easily.
Norman, do you suggest anything about it?
Do you think “IsNumeric” will work in all cases?
(My context is solving big numerical matrices, where sometimes results are NaN)
No
NaN is actually numeric - go figure - so IsNumeric(“NaN”) returns true
If what you want to test for is “it contains a value that is represented with digits (maybe a decimal point and a leading +/-)” thats something slightly different
Whether you’ve seen it really doesn’t matter to the compiler or IDE
We don’t have a “check with Kem” syntax check module
I would however check what VAL allows which is not quite the same as what you can type in the IDE
It definitely allows
dim d as double = val(“1.0e2.0”)
Well, you did ask, and we’re hashing it out, right?
Yes, that’s allowed as string, not as a literal, but the decimal is truncated just as any other character is removed. These lines result in exactly the same value:
d = Val( "1e1" )
d = Val( "1e1.0" )
d = Val( "1e1.9" )
d = Val( "1e1x" )
d = Val( "10y" )
// d = 10.0 in all cases
If the goal is to validate a string as a number that would be acceptable as a literal, I recommend the pattern as posted. If the goal is to validate something that Val will accept, you don’t need a pattern at all since it will return a value with almost anything, if not anything.
Its not though
It to figure out what Str might return since it MIGHT return NaN etc and IsNumeric treats NaN as numeric (since it is its just not represented as digits)
dim rx as new RegEx
rx.SearchPattern = "(?i)\\A[\\d\\-+e\\.]+\\z"
return rx.Search( s ) <> nil and IsNumeric( s )
This will check to make sure the string only contains numbers, +, -, ., or e in any order, and, if so, runs it through IsNumeric. This seems the safest course if you just want to eliminate things like “NaN” and “INF”.
BTW, I just realized that my first pattern would not validate “.1” as a number. Bad.
I just took another whack at a complete RegEx and come up with this pattern. When I compare various strings to IsNumeric, the results match, pass or fail.
[quote=87032:@Kem Tekinay]I just took another whack at a complete RegEx and come up with this pattern. When I compare various strings to IsNumeric, the results match, pass or fail.
That latest iteration would fail those because of the anchors \A (start of document) and \z (end of document).
I still prefer using the RegEx to check for exclusive characters, then running it through IsNumeric if that’s satisfied. It keeps from recreating IsNumeric and would adapt if some future version of Xojo decided that “1.” was no longer valid.
Thanks Norman and Kem for your dissertation on RegEx.
The truth is that my problem is not so complex.
All the numbers I want to check have been produced by Xojo, so there is no possibility to have strange combinations, except if Xojo produces them itself.
What Thomas says should work some times but not always.
Just have a look at what Xojo produces in my PC (Windows 7).
I didn’t know, but according to what Norman says it is different in Windows and OSX.
Who produces this output? Xojo or the OS?
I think there should be a simple way to know if a result (not a text) is a real number or just one of these strange outputs.
Should I post a feature request for it?
Well, I’m on a Mac but the documentation clearly states:
[quote]Infinity: some calculations lead to an infinite result (positive or negative), e.g. Log( 0 ), or you may exceed the maximum value which can be hold. In such a case, a double will be set to a special value, whose Str will return “INF” (for INFinity) or “-INF” (negative INFinity). Any further calculation will lead to a NaN or infinity value.
[/quote]
In case it work different on Windows it seems to be a bug.
That’s what I pointed at the beginning of this thread: What I get is not what LR says (at least on Windows).
But it is still worse. If you debug your program you don’t see that this is not a number.
Look a the image: a = 5 / 0, that is an infinity, but you see 1. Only if you try to string it you notice the problem.
So the problem is not the LR, but the string you get in Windows, that seems is different from the one you get in OSX (and Linux?)
Who generates this string?
If it is Xojo, then it should be the same in any OS, since Xojo is cross-platform. If it is the OS, then it should be said in the LR, warning about the different possible values you can get.
I would appreciate that someone from Xojo tells me which is the answer to the previous question, so that I can write a function that suits my problem and it is cross-platform