Should Val() return NaN?

Everything else that doesn't fall into one of the other PB categories.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Should Val() return NaN?

Post 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?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
STARGÅTE
Addict
Addict
Posts: 2261
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Should Val() return NaN?

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Should Val() return NaN?

Post 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?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
STARGÅTE
Addict
Addict
Posts: 2261
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Should Val() return NaN?

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Should Val() return NaN?

Post 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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Should Val() return NaN?

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Should Val() return NaN?

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Should Val() return NaN?

Post 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. :)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
STARGÅTE
Addict
Addict
Posts: 2261
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Should Val() return NaN?

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Should Val() return NaN?

Post by IdeasVacuum »

Code: Select all

No, it can't.
Of course it could :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Should Val() return NaN?

Post 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!
pb 5.11
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Should Val() return NaN?

Post 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("")
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Should Val() return NaN?

Post 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:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Should Val() return NaN?

Post 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
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Should Val() return NaN?

Post 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?
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply