Page 1 of 2

Should Val() return NaN?

Posted: Tue Oct 16, 2012 12:25 am
by IdeasVacuum

Code: Select all

;Example 1
sVal.s = "0"
Debug Val(sVal)

;Example 2
sVal = ""
Debug Val(sVal)
My app reads some settings from a file on start-up (all stored as strings, mostly are strings rather than numbers). For the strings that hold numbers, values 0 and up are valid. If a setting does not have a value saved, then the app will apply a default. So, from that perspective, Example 2 returning a value of zero is not helpful.

What do you think people? My solution in this instance is to save the value as -1 (or any number the app deems invalid), but should Val() return a number when there is no number?

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 12:32 am
by STARGÅTE
Val is only for integers, and integer have no NaN or INF.
You can use floats vor example, but no number is 0.0 and only the "NaN" String returns the NaN-Float-Number:

Code: Select all

;Example 2
sVal = "NaN"
Debug ValF(sVal)
If you want check if the number is ok, use IsNumber or IsNumeric like this:
http://www.purebasic.fr/english/search. ... mit=Search
http://www.purebasic.fr/english/search. ... mit=Search

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 1:27 am
by IdeasVacuum
Well, what is returned is determined by the author of the function. It could return the weather forecast for the Himalayas, but I wouldn't expect that either.

My question is: should Val() return a number when there is no number? That is what it does right now, and yes there are umpteen ways to test a string - but logically, isn't the Val() return wrong in this instance?

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 1:39 am
by STARGÅTE
i dont understand you.

Val() is a function with string input and integer output, that is "unchangeable".
So, integers are always numbers between -$80000000 and $7FFFFFFF (32Bit), and 0 is the number like "nothing".

If you write:
A.i = Val("")
A can only be an integer, it can not be "indeterminate" or "Not a Number" like in other languages like mathematica.

So 0 is better then 345763 or -34578 or -1

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 9:03 am
by MachineCode
IdeasVacuum wrote:should Val() return a number when there is no number?
Val() returns a value of a string, not a number. So an empty string passed to it would indeed have a value of 0, since there is nothing in the string.

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 11:22 am
by IdeasVacuum
Val() returns a value of a string, not a number
That is not true, if it were true then this would be greater than zero:

Code: Select all

sText.s = "abcd"
Debug Val(sText)
You guys are missing my point though. I'm not asking what Val() returns or why Val() returns what it returns. I'm asking - could the function be better designed to return a more logical result. To me, it is not logical that "0" and "" should return the same number value, because they are not the same.

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 11:27 am
by IdeasVacuum
Val() is a function with string input and integer output, that is "unchangeable"
There is nothing in the world (other than death?), never mind in app development, that is unchangeable. We would still be using an abacus if that were true.

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 11:27 am
by MachineCode
What? "abcd" correctly returns 0 because there's no numerical value there. It's all letters.

And "0" and "" both correctly return 0 because they're both nothing. Zero is nothing, and null is nothing. They ARE the same. You're thinking waaaaaaay too deep on this. :)

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 11:49 am
by STARGÅTE
IdeasVacuum wrote:could the function be better designed to return a more logical result
No, it can't.
IdeasVacuum wrote:To me, it is not logical that "0" and "" should return the same number value, because they are not the same.
lol, "logical", then close your eyes now ^^

Code: Select all

A.b = 170
B.b = -86
If A = B
	Debug "170 = -86, wtf ^^"
EndIf
If you want to work with numbers, there are limitations that you have to accept.

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 3:47 pm
by IdeasVacuum

Code: Select all

No, it can't.
Of course it could :mrgreen:

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 4:07 pm
by gnasen
Maybe I understand what you want to say:
You want to check something like this:

Code: Select all

If val("abc") <> #NaN
  ;do something with it
EndIf
However this is not possible, because if you use the #NaN operator as an integer (at least it can be interpreted as one) it may have the value 1234. But what if you do now the following?

Code: Select all

If val("1234") <> #NaN
  ; do something with it
EndIf
It wont work, because "number 1234" and "not a number" are given by the same value. So you would have to "sacrifice" some random number. And thats a no go!

Re: Should Val() return NaN?

Posted: Tue Oct 16, 2012 6:42 pm
by Demivec
IdeasVacuum wrote:You guys are missing my point though. I'm not asking what Val() returns or why Val() returns what it returns. I'm asking - could the function be better designed to return a more logical result. To me, it is not logical that "0" and "" should return the same number value, because they are not the same.
Simply check the string if Val() returns a zero and substitute your own numeric value when there is an empty string:

Code: Select all

Procedure myVal(sVal.s)
  Protected Result = Val(sVal)
  If Result = 0 And Not sVal
    ;Debug "empty string"
    Result = -1 ;magic value
  EndIf
  ProcedureReturn Result
EndProcedure

;Example 1
Debug myVal("0")

;Example 2
Debug myVal("")

Re: Should Val() return NaN?

Posted: Wed Oct 17, 2012 10:55 am
by IdeasVacuum
Thanks Demivec, in fact I do exactly that in another app - but solutions for Val() as it is now was not my interest when I started this post - it was about the design of the function. I've asked if a 3-wheeler car would be better with 4 wheels, but all the responders defend the 3-wheeler as if their life depends on it :mrgreen:

Re: Should Val() return NaN?

Posted: Wed Oct 17, 2012 11:10 am
by Danilo
Val() returns a Quad, and no quad can represent NaN. So it is good it returns 0.
No Byte, Word, Long, Integer, Quad return value can represent NaN, because values
in the range are all valid and possible.
Changing Val() to return another type? IMHO Impossible. If you use -1 as the magic value,
Val("-1") or myVal("-1") would mean 'empty string' for you. But -1 is a valid number.

Your settings use only 0+ values, so it is OK for you to write a function that returns -1 for you.
The general function Val() can not do this, because -1 is valid signed input.

It is not a problem to use your own special function for your special requirement, is it?
Why should PB change the general function to your special requirement?

In your special case, I would change your settings reader function. If it does not find the
setting value, it returns the default. Only if it finds the settings value, it gets converted with Val().
So you simply add a check before using Val(): If the strings is empty, use default value. String not empty, use Val() value.

It would be possible with ValF() and ValD():

Code: Select all

f.f = ValF("NaN")
Debug f
d.d = ValD("NaN")
Debug d

f = ValF("") ; could return NaN, returns 0.0
Debug f
d = ValD("") ; could return NaN, returns 0.0
Debug d

Re: Should Val() return NaN?

Posted: Wed Oct 17, 2012 1:36 pm
by MachineCode
IdeasVacuum wrote:it was about the design of the function
PureBasic is Basic, and all Basics use Val() that return 0 for a "" string. Why should PureBasic be different?