Val Does Not Work on Zero

Just starting out? Need help? Post your questions and find answers here.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

I AM TOTALLY AFRAID BUT This is totally dependant on the library errors implementation. As I stated, "other libs might return negative as error"

--- EDIT ---

And I was not refering to PB libs here... Notice I said "I even use this" because I handle errors with negative hex values. That way you can know which error you've got. and as for returning I return -1 for errors. Since sometimes a 0 is required and taken as a valid result. And, this all depends on your implementation.

So if I get a -1 I do call a routine to get the last error...
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

dagcrack wrote:I AM TOTALLY AFRAID BUT This is totally dependant on the library errors implementation. As I stated, "other libs might return negative as error"

--- EDIT ---

And I was not refering to PB libs here... Notice I said "I even use this" because I handle errors with negative hex values. That way you can know which error you've got. and as for returning I return -1 for errors. Since sometimes a 0 is required and taken as a valid result. And, this all depends on your implementation.

So if I get a -1 I do call a routine to get the last error...
If you are designing your own error logic then yes you can use any numbering system you like but you raise a good point here. I know, like you said that other programing languages use '-1' to signify an error. In Purebasic however this would be seen as a true value and as such can not be reliably testing using an 'If'. I, personally, would be very careful to test any return values from non-Purebasic library commands.

This is all concerning 'If's though and shouldn't ever be a problem just as long as you understand what is considered a true and false value while testing using an 'If'. :)
--Kale

Image
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Post by Alex »

Kale wrote:If the result of any expression or value that follows this 'If' keyword so happens to be a '0' (zero) then it is determined to be false. Otherwise it is true. :)
So any operation which results in a "0," no matter how accurate, will result in a "false" result in any test?

Alex
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Val Does Not Work on Zero

Post by PB »

> Your code, as it stands, also fails on PureBasic v4.00 Beta 9 for Windows

Then Alex wrote: I'm afraid you're not correct
Then Psychophanta wrote: Sorry but not agree

To clarify: what I meant is that he is using Val incorrectly, which is why it fails
and doesn't give him the results he is expecting.

Val is meant to return a value of the string. That's why you can't use it like
"If Val(string$)" -- IT'S NOT MEANT TO BE USED THAT WAY. Read the manual:

http://www.purebasic.com/documentation/string/val.html

The manual even gives an example of what it returns:

Code: Select all

; Transform a string into a numeric value.
Result = Val("1024") ; Result will be filled with 1024.
> Does Val("0") give a value of "true" AND a result of "0"

NO! Read the manual, please. It ONLY returns the value of the string given to it:

Code: Select all

Debug Val("0") ; Returns 0 due to "0" in the brackets.
Debug Val("1") ; Returns 1 due to "1" in the brackets.
Debug Val("2") ; Returns 2 due to "2" in the brackets.
Here's a practical example that shows Val in its intended use:

Code: Select all

a$="50"
b$="2"
c$="0"

Debug Val(a$)/Val(b$) ; Returns 25, because we're actually doing 50/2.
Debug Val(a$)/Val(c$) ; Returns error, because we're actually doing 50/0.
Now do you understand how it works? It doesn't test for true/false or any
other thing like that. It simply returns, as a number, what the number in
the given string is. Hope I've made it clear now. :)

@Fred: I suggest you change the manual to make it a bit clearer:
OLD: Transform a string into a numeric value.
NEW: Transform a string into its numeric value.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Re: Val Does Not Work on Zero

Post by Alex »

PB wrote:> To clarify: what I meant is that he is using Val incorrectly, which is why it fails
and doesn't give him the results he is expecting.

Val is meant to return a value of the string. That's why you can't use it like
"If Val(string$)" -- IT'S NOT MEANT TO BE USED THAT WAY. Read the manual:
Unfortunately, I need to parse input which might be unreliable. Finding a workaround wasn't hard:

Code: Select all

If Val(A$) Or A$="0"
worked just fine - after I'd cut the string up into single-character chunks so I didn't get stuck with a false "true" if I tried to parse a problem string such as:

Code: Select all

A$="11Z"
If Val(A$)
which, oddly enough, returns a true, probably because "11Z" isn't zero.

Code: Select all

A$="11Z"
RESULT=Val(A$)
In this case, RESULT=11.

but

Code: Select all

A$="Z11"
RESULT=Val(A$)
In this case, RESULT=0.

Maybe it's not a bug in the classic sense, but it sure doesn't behave consistently. I finally arrived at:

Code: Select all

A$="Unreliable client input, transported across a network."
If MIN_LENGTH > 0
   MIN_LENGTH_COUNT=0
      While MIN_LENGTH_COUNT < MIN_LENGTH
         RESULT$=Mid(A$, MIN_LENGTH_COUNT+1, 1)
            If Val(RESULT$) Or RESULT$="0"
            Else
               Gosub Not_A_Number
            EndIf
         MIN_LENGTH_COUNT=MIN_LENGTH_COUNT+1
      Wend
EndIf
But it took ten times as long as if Val() behaved consistently.

Alex
Last edited by Alex on Sun Apr 09, 2006 5:34 am, edited 1 time in total.
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Val Does Not Work on Zero

Post by PB »

> it took ten times as long as if Val() behaved consistently

IT DOES BEHAVE CONSISTENTLY. YOU'RE USING IT INCORRECTLY.

Take your latest example: If Val(RESULT$) Or RESULT$="0"
You are trying to use the command in a way it's not meant to be used! Can
someone else here please tell him, because he's not willing to listen to me.
Last edited by PB on Sun Apr 09, 2006 7:27 am, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

If Val(RESULT$) Or RESULT$="0"
Else
Gosub Not_A_Number
EndIf
Looking at your code, it seems to me you are after some kind of Isnumeric() routine, several examples of which you can find on the forum if you do a search. Val() is not designed for this task.

Here's a basic one (no floats or negative numbers) but it can easily be modified to take more kinds of numbers:

Code: Select all

ProcedureDLL.l IsNumeric(String$) 
  If String$ 
    *p.character = @String$ 
    Repeat 
      char = *p\c
      If (char >= '0' And char <= '9') Or char=0 
        *p+1 
      Else 
        ProcedureReturn 0 
      EndIf 
    Until char = 0 
    ProcedureReturn 1 
  EndIf 
EndProcedure 
BERESHEIT
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi Alex,

Val means "Value Of", not validate.

A false condition in PureBasic is one that returns 0, and a true condition is therefore anything that does not return a false, therefore anything other than zero.

The one and only purpose of Val() is to convert the contents of a string variable to a numeric value where this is possible. That is its sole purpose in life.

It is behaving correctly. It coverts a string of characters to a numeric value if possible and returns this value. There is always the possibility that one such value may be zero. This means your use of If Val() is incorrect, not Val() itself.

If you wish to check whether the contents of a string are numeric then there are quite a few examples for doing this on these forums.



Aside, @PB: Remember your call for maturity? Please exercise some and stop shouting like a two year old. Actually, I am beginning to wonder whether you should seek some help.
@}--`--,-- A rose by any other name ..
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Re: Val Does Not Work on Zero

Post by Alex »

PB wrote:>You are trying to use the command in a way it's not meant to be used! Can someone else here please tell him, because he's not willing to listen to me.
Thanks for the help you've offered. I do appreciate the attempt even though we disagree. We both want PureBasic to be better, and I'd rather stop arguing and enjoy that commonality than keep trying to prove my point until things become nasty.

Please understand this: I already have code that works. I've tested it carefully, and the code can tell whether the string has only numbers or does not. Maybe it's "wrong" but it passed all the tests I need it to pass, and it handles all the situations I need it to handle. And it does so quickly, which is a major issue for the application I'm writing.

OK?

If you've got a brilliant code snippet I'll happily take a look. And I'll try it, and I'll doubtless agree that it's very good because you have lots of experience with Purebasic - much more than I do. I absolutely acknowledge that.

But barring a nice code snippet, I'm dropping out of the discussion. I've done my best to demonstrate some problems with this particular keyword, and by now either Fred agrees with my points or he doesn't. It would be nice if you'd take a careful look at my objections to the behavior of this keyword rather than simply telling me I'm wrong over and over again, but I'm not so wedded to being right that I'm willing to spend any more time on the issue.

Once again, thanks,

Alex
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Post by Alex »

netmaestro wrote:Looking at your code, it seems to me you are after some kind of Isnumeric() routine, several examples of which you can find on the forum if you do a search. Val() is not designed for this task.
Thanks for the code snippet. I've found a workaround which works very nicely. In fact, I found it before I made the top post. My only real concern was the inconsistency in the behavior of the keyword. Leaving aside the issue of "Val("0") reporting false, Val seems to do some odd things which I've reported above, but it's Fred's call whether to fix them, so I'm not going to worry about it.

Thanks again,

Alex
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Val Does Not Work on Zero

Post by PB »

> Leaving aside the issue of "Val("0") reporting false

For crying out loud, it's NOT reporting false. YOU just think it is. Are you
even TRYING to understand what I've posted? Or maybe you think all the
PureBasic commands should work the way YOU want them to work? :roll:

> I've done my best to demonstrate some problems with this particular keyword

There is no problem with it. Both netmaestro and Dare2 agree with me that
you're wrong on this. And yes, I posted in big letters and got angry, but that's
due to frustration because you're not willing to listen. I can see now it's not
getting through to you, so yes, I agree with you that we should just end the
discussion.

@Dare2: Being frustrated doesn't mean I need any help, mate. That's a direct
personal insult on me which I find judgemental and offensive in the extreme.
It's time like this that make me really understand why Danilo quit the forums.
Last edited by PB on Sun Apr 09, 2006 6:35 am, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Hey! Look at sec's avatar...
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Hey! Look at sec's avatar...

Mmm, pretty girls make everything better. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Post by Alex »

Dare2 wrote:Hi Alex,

Val means "Value Of", not validate.

A false condition in PureBasic is one that returns 0, and a true condition is therefore anything that does not return a false, therefore anything other than zero.

The one and only purpose of Val() is to convert the contents of a string variable to a numeric value where this is possible. That is its sole purpose in life.
Thanks, I've been told this several times now, and I have code that works. (In fact, I had code that works before I made the top post.) Even assuming that Val is only intended to convert, it still behaves strangely, IMHO, for the reasons I've enumerated above. Ideally, I'd like it not to attempt to convert any number-letter combination, such as 1W, (which it will convert) or W1, which it won't convert. I think that "RESULT=Val(1W)" and "RESULT=Val(W1)" should both return a value of "false." This would make it much more reliable when converting strings to numbers, and also make it's behavior easier to understand. For example, is "1W" a slip of the fingers when attempting to type "1," or is it a mistyped "12?"

I also have strong disagreements about Val("0") reporting as false, or worse yet, the idea some posters have implied which says that any operation which returns "0" will have a value of false. Shouldn't the results of the operation be kept separately from the true/false value? Those are my current concerns. Meanwhile, I'll just enjoy PureBasic and work around it.

Thanks,

Alex
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Post by Alex »

PB wrote:> Mmm, pretty girls make everything better. :)
You are so right. :D

Alex
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
Post Reply