Val Does Not Work on Zero

Just starting out? Need help? Post your questions and find answers here.
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Val Does Not Work on Zero

Post 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
"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 »

> 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.
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: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
"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
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: Val Does Not Work on Zero

Post 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).
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Val("0") will return 0, which is false in condition.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Val Does Not Work on Zero

Post 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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Val Does Not Work on Zero

Post by Psychophanta »

PB wrote:Your code, as it stands, also fails on PureBasic v4.00 Beta 9 for Windows,
:o
Sorry but not agree.
PB wrote:but that's because you're not using it correctly.
Again, not agree. :?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

If Val("0") is exactly like the following code, or i miss something ?

Code: Select all

If 0
  Debug "Never show"
EndIf
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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! :o
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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 :oops:
Last edited by Kale on Sat Apr 08, 2006 10:08 pm, edited 1 time in total.
--Kale

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

Post 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
"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 »

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
"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
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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 
:oops:

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:

Code: Select all

If OpenWindow(....)
    ...
Endif
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).
Last edited by Kale on Sat Apr 08, 2006 10:10 pm, edited 1 time in total.
--Kale

Image
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post 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 .

:lol:
Last edited by dagcrack on Sat Apr 08, 2006 10:12 pm, edited 1 time in total.
! 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 »

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 
--Kale

Image
Post Reply