This is no true. There is an error, since if I write this code:
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
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,
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.
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.
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).
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.
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