Page 1 of 3
Val Does Not Work on Zero
Posted: Sat Apr 08, 2006 8:13 am
by Alex
If I use the Val function it works with numbers 1-9. However, if I use "Val" on a 0, it fails.
Code: Select all
A$="0"
If Val(A$)
PrintN("Found Value")
Else
PrintN("Is this a bug?")
EndIf
I'm using 3.94 with Slackware Linux
Re: Val Does Not Work on Zero
Posted: Sat Apr 08, 2006 8:23 am
by PB
> I'm using 3.94 with Slackware Linux
Your code, as it stands, also fails on PureBasic v4.00 Beta 9 for Windows, but
that's because you're not using it correctly. You're supposed to do it like so:
Code: Select all
A$="0"
If Val(A$)=0
Debug ("Found Value")
Else
Debug("Is this a bug?")
EndIf
In other words, you're supposed to test the value that Val returns, which you
didn't do. What you're doing is just testing it for True (non-zero) or False (zero).
However, Val doesn't return just True or False, it returns the value of the string.
Re: Val Does Not Work on Zero
Posted: Sat Apr 08, 2006 9:17 am
by Alex
PB wrote:Your code, as it stands, also fails on PureBasic v4.00 Beta 9 for Windows, but that's because you're not using it correctly. You're supposed to do it like so:
I'm afraid you're not correct. Val behaves differently if I do this:
Code: Select all
A$="1"
If Val(A$)
PrintN("Found Value")
Else
PrintN("Is this a bug?")
EndIf
than if I do this:
Code: Select all
A$="0"
If Val(A$)
PrintN("Found Value")
Else
PrintN("Is this a bug?")
EndIf
So it reports success in turning 1-9 into a number, but it reports failure turning 0 into a number, which means I can't use Val as a test because I can successfully test the numerals from 1-9 with Val, but if I try to test 0 it fails to test correctly. Thus:
Code: Select all
A$=(a string that might translate into a number)
If Val(A$) ; something IS a number
Gosub Deal_With_Numbers
Else
Gosub Deal_With_Strings
Endif
doesn't work. If Val can report success turning strings of 1-9 into numbers, it should be able to report success in turning the string "0" into a number.
Alex
Re: Val Does Not Work on Zero
Posted: Sat Apr 08, 2006 9:32 am
by gnozal
Alex wrote:If Val can report success turning strings of 1-9 into numbers, it should be able to report success in turning the string "0" into a number.
Alex
Val() is not an IsNumber() function. It just converts a string to a number. And like in most basic dialects, Val("NotANumber") = 0 (not a bug).
Posted: Sat Apr 08, 2006 11:05 am
by Fred
Val("0") will return 0, which is false in condition.
Re: Val Does Not Work on Zero
Posted: Sat Apr 08, 2006 12:58 pm
by blueznl
PB wrote:
However, Val doesn't return just True or False, it returns the value of the string.
yep, and in pb logic 0 = false, and <> 0 = true
Re: Val Does Not Work on Zero
Posted: Sat Apr 08, 2006 1:53 pm
by Psychophanta
PB wrote:Your code, as it stands, also fails on PureBasic v4.00 Beta 9 for Windows,
Sorry but not agree.
PB wrote:but that's because you're not using it correctly.
Again, not agree.

Posted: Sat Apr 08, 2006 2:35 pm
by Fred
If Val("0") is exactly like the following code, or i miss something ?
Posted: Sat Apr 08, 2006 3:40 pm
by Psychophanta
@Alex;
Why don't you use the "Coding questions" Section for this topic instead of "Bug reports" one?
I can't understand a thing!

Posted: Sat Apr 08, 2006 5:59 pm
by Kale
One thing to remember about an '
If' statement is that it tests for a true value, not necessarily a number. It just so happens that all numbers in Purebasic other than '0' are considered 'True'.
His code was good code.
Code: Select all
A$="0"
If Val(A$)
Debug "this is shown when the 'If' finds a true value (a number that is not 0)"
Else
Debug "this is shown when the 'If' finds a false value (a number equal to 0)"
EndIf
Debug #True
Debug #False
EDIT: changed my poor code, lol

Posted: Sat Apr 08, 2006 8:08 pm
by Alex
Kale wrote:
His code was good code.
Code: Select all
A$="0"
If Val(A$)
Debug "this is shown when the 'If' finds a true value (a number)"
Else
Debug "this is shown when the 'If' finds a false value (a number that is not 0)"
EndIf
Debug #True
Debug #False
Do I have a fundamental misunderstanding of how "If" operates? When I test, is "If" looking at the true/false result, or the result of the operation? Try the following code:
Code: Select all
A$="9"
If Val(A$)
Debug "this is shown when the 'If' finds a true value (a number)"
Else
Debug "this is shown when the 'If' finds a false value (a number that is not 0)"
EndIf
Debug #True
Debug #FALSE
Debug RESULT=Val(A$)
The debugger return appears as follows:
Code: Select all
this is shown when the 'If' finds a true value (a number)
-1
0
9
So there is a difference between the value and result. That is, Val(A$) returns a value of true, (expressed as "not zero,"). It also expresses the operation's result, expressed as the numeral "9"
So it looks like "If" is looking at the true/false value, not the result of the operation, and it appears to distinguish between the two. If "If" was looking at the result of the operation, "#TRUE" would equal "9," unless some internal translation was happening.
But with the same code, if A$=0, I get the following debugger return:
Code: Select all
this is shown when the 'If' finds a false value (a number that is not 0)
-1
0
0
So it looks like "If" is looking at the numerical result, and not the true/false value.
Which of the two is "If" looking at, and how many sets of values are there? It seems to me that there should be two pieces of information returned for every operation - the "true/false" value and the "result" of the operation - a number, string, array, or whatever. As I see things, "If" should access the true/false value. Can you straighten me out?
Thanks,
Alex
Posted: Sat Apr 08, 2006 8:22 pm
by Alex
Fred wrote:Val("0") will return 0, which is false in condition.
Maybe I'm unclear on the concept. Shouldn't Val() (or any other operation) return two pieces of information; a true/false value AND a result, and shouldn't "If" look at the true/false value rather than the result?
Does Val("0") give a value of "true" AND a result of "0" which are kept in separate places? Or does it put BOTH the result and true/false value in the same place?
Alex
Posted: Sat Apr 08, 2006 10:04 pm
by Kale
Lol! I think i made a typo.

This is how it should read:
Code: Select all
A$="0"
If Val(A$)
Debug "this is shown when the 'If' finds a true value (a number that is not 0)"
Else
Debug "this is shown when the 'If' finds a false value (a number equal to 0)"
EndIf
Debug #True
Debug #False
anyway, the main point is that an 'If' in purebasic will test for a true value ONLY. 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.
This allows you to do stuff like:
because the return value from 'OpenWindow' should be a handle and if not '0' it must of been successful.
All commands that return a value in PB if they are non-successful they always return a '0' (false).
Posted: Sat Apr 08, 2006 10:10 pm
by dagcrack
about logic on errros... if equals 0 or is less than 0 its an error, else its not an error. Even if PB does not use negative values as error, many do (I do as well). Just saying though
Code: Select all
a = 0;
If a <=0
Debug "oops!"
Else
Debug "meh!"
EndIf
no? - It sure is slower than checking if it equals 0 ... But... depends on the lib you're using, some return negative values.
Tip for newcomers: Never say "you're not right" to Fred, after all, he made what you're using, behave, respect, shut-up and listen to him .

Posted: Sat Apr 08, 2006 10:12 pm
by Kale
No. Any number other than '0' is true, even negative numbers.
Code: Select all
If -1
Debug "this is shown when the 'If' finds a true value (a number)"
Else
Debug "this is shown when the 'If' finds a false value (a number that is not 0)"
EndIf