val() function returns Indefinite value ("-1.#IND")

  • Thread starter Thread starter BillE
  • Start date Start date
B

BillE

There are some string expressions which the val() function returns
"-1.#IND", which I believe is a variation of "NaN".

An example is "d48609".
"e48609" also returns "-1.#IND"

However, "c48609" or "f48609" or "d4860" return 0.0, like any other string
beginning with a letter, in my experience.

I was looping through a datatable, assigning the results of the val()
function to a numeric field when it happened. The error was trapped, but
people are still upset about getting an error message.

Does anyone know why this occurs?

Thanks
Bill
 
Hi BillE,

I would probably recommend just getting rid of all non alpha numeric
characters from the string before attempting the conversion, at least then
you know you are strictly feeding the function a number. I'm not quite sure
what the benefit of intentionally converting a string to a number would be
if you know it's going to come back incorrect? Unless I'm missing the
point?

Also, Integer.Parse would probably be a better way to go about
converting your strings into Integers too.

Nick.
 
Hi BillE,

I would probably recommend just getting rid of all non alpha numeric
characters from the string before attempting the conversion, at least then
you know you are strictly feeding the function a number. I'm not quite sure
what the benefit of intentionally converting a string to a number would be
if you know it's going to come back incorrect? Unless I'm missing the
point?

Also, Integer.Parse would probably be a better way to go about
converting your strings into Integers too.

Nick.
 
I have to sort a list of Employees by the alphanumeric Employee Number (not
the primary key, but a unique index available to the user).

Sometimes users want to sort by the initial numerals of the employee number.
"2abc" should come before "11abc".

I thought the val function should accept any ascii characters. The val
function should return a double: val("2abc") = 2.0 val("11abc") = 11.0
val("abc") = 0.0

The value which caused the error was "d486091": val("d48609") = -1.#IND

My question is, why does "d48609" return this, when "f48609" returns 0.0, as
expected?

Thanks for your help.

Bill
 
I have to sort a list of Employees by the alphanumeric Employee Number (not
the primary key, but a unique index available to the user).

Sometimes users want to sort by the initial numerals of the employee number.
"2abc" should come before "11abc".

I thought the val function should accept any ascii characters. The val
function should return a double: val("2abc") = 2.0 val("11abc") = 11.0
val("abc") = 0.0

The value which caused the error was "d486091": val("d48609") = -1.#IND

My question is, why does "d48609" return this, when "f48609" returns 0.0, as
expected?

Thanks for your help.

Bill
 
BillE said:
There are some string expressions which the val() function returns
"-1.#IND", which I believe is a variation of "NaN".

An example is "d48609".
"e48609" also returns "-1.#IND"

However, "c48609" or "f48609" or "d4860" return 0.0, like any other
string beginning with a letter, in my experience.

I was looping through a datatable, assigning the results of the val()
function to a numeric field when it happened. The error was trapped,
but people are still upset about getting an error message.

Does anyone know why this occurs?

"e" = exponent, like val("5e3") = 5 * 10^3 = 5,000

val("e48609") = 0 * 10^48609 = 0 * 1.#INF = -1.#INF

Obviously, "d" is handled equally.


Armin
 
BillE said:
There are some string expressions which the val() function returns
"-1.#IND", which I believe is a variation of "NaN".

An example is "d48609".
"e48609" also returns "-1.#IND"

However, "c48609" or "f48609" or "d4860" return 0.0, like any other
string beginning with a letter, in my experience.

I was looping through a datatable, assigning the results of the val()
function to a numeric field when it happened. The error was trapped,
but people are still upset about getting an error message.

Does anyone know why this occurs?

"e" = exponent, like val("5e3") = 5 * 10^3 = 5,000

val("e48609") = 0 * 10^48609 = 0 * 1.#INF = -1.#INF

Obviously, "d" is handled equally.


Armin
 
BillE said:
There are some string expressions which the val() function returns
"-1.#IND", which I believe is a variation of "NaN".

An example is "d48609".
"e48609" also returns "-1.#IND"

However, "c48609" or "f48609" or "d4860" return 0.0, like any other string
beginning with a letter, in my experience.

I was looping through a datatable, assigning the results of the val()
function to a numeric field when it happened. The error was trapped, but
people are still upset about getting an error message.

Does anyone know why this occurs?

Thanks
Bill

What is going on is that you hitting characters that have specific
meaning. See

Double Data Type (Visual Basic)
http://msdn.microsoft.com/en-us/library/x99xtshc.aspx

and this one:

http://msdn.microsoft.com/en-us/library/ae382yt8.aspx

Use the literal type character D to force literals to Decimal, in
case their values are too large for the Long data type.

You will have to watch for other characters like

E
G
R

What else?

&H hex number

-
 
BillE said:
There are some string expressions which the val() function returns
"-1.#IND", which I believe is a variation of "NaN".

An example is "d48609".
"e48609" also returns "-1.#IND"

However, "c48609" or "f48609" or "d4860" return 0.0, like any other string
beginning with a letter, in my experience.

I was looping through a datatable, assigning the results of the val()
function to a numeric field when it happened. The error was trapped, but
people are still upset about getting an error message.

Does anyone know why this occurs?

Thanks
Bill

What is going on is that you hitting characters that have specific
meaning. See

Double Data Type (Visual Basic)
http://msdn.microsoft.com/en-us/library/x99xtshc.aspx

and this one:

http://msdn.microsoft.com/en-us/library/ae382yt8.aspx

Use the literal type character D to force literals to Decimal, in
case their values are too large for the Long data type.

You will have to watch for other characters like

E
G
R

What else?

&H hex number

-
 
Hmm, I still personally don't think that's a good way of sorting the string,
you are relying on errors. If you want to specifically sort by the
numerical digits at the beginning of the string then you should parse those
out prior to checking, being explicit as possible is always going to be the
most reliable way. If you want to perform an alphabetical sorting then
change your code to suit, relying on the same method to do it out of
convenience doesn't appear to be working for you.

Also how about using the StringComparer class?

http://msdn.microsoft.com/en-us/library/system.stringcomparer.aspx

Also you can create a custom IComparer and then peform a more advanced form
of sorting where you can take any part of the string in for account.
Anyway, this is just my personal opinion of course, according to the docs on
Val,

"The Val function stops reading the string at the first character it cannot
recognize as part of a number. Symbols and characters that are often
considered parts of numeric values, such as dollar signs and commas, are not
recognized. However, the function recognizes the radix prefixes &O (for
octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are
stripped from the argument."

I can't see anywhere on the page mentioning that it's specifically used for
comparing characters on their own, hence why I personally suggest going the
other route.

Anyway I hope this helps.

Nick.
 
Hmm, I still personally don't think that's a good way of sorting the string,
you are relying on errors. If you want to specifically sort by the
numerical digits at the beginning of the string then you should parse those
out prior to checking, being explicit as possible is always going to be the
most reliable way. If you want to perform an alphabetical sorting then
change your code to suit, relying on the same method to do it out of
convenience doesn't appear to be working for you.

Also how about using the StringComparer class?

http://msdn.microsoft.com/en-us/library/system.stringcomparer.aspx

Also you can create a custom IComparer and then peform a more advanced form
of sorting where you can take any part of the string in for account.
Anyway, this is just my personal opinion of course, according to the docs on
Val,

"The Val function stops reading the string at the first character it cannot
recognize as part of a number. Symbols and characters that are often
considered parts of numeric values, such as dollar signs and commas, are not
recognized. However, the function recognizes the radix prefixes &O (for
octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are
stripped from the argument."

I can't see anywhere on the page mentioning that it's specifically used for
comparing characters on their own, hence why I personally suggest going the
other route.

Anyway I hope this helps.

Nick.
 
Thank you very much.

nak said:
Hmm, I still personally don't think that's a good way of sorting the
string, you are relying on errors. If you want to specifically sort by
the numerical digits at the beginning of the string then you should parse
those out prior to checking, being explicit as possible is always going to
be the most reliable way. If you want to perform an alphabetical sorting
then change your code to suit, relying on the same method to do it out of
convenience doesn't appear to be working for you.

Also how about using the StringComparer class?

http://msdn.microsoft.com/en-us/library/system.stringcomparer.aspx

Also you can create a custom IComparer and then peform a more advanced
form of sorting where you can take any part of the string in for account.
Anyway, this is just my personal opinion of course, according to the docs
on Val,

"The Val function stops reading the string at the first character it
cannot recognize as part of a number. Symbols and characters that are
often considered parts of numeric values, such as dollar signs and commas,
are not recognized. However, the function recognizes the radix prefixes &O
(for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed
characters are stripped from the argument."

I can't see anywhere on the page mentioning that it's specifically used
for comparing characters on their own, hence why I personally suggest
going the other route.

Anyway I hope this helps.

Nick.
 
Thank you very much.

nak said:
Hmm, I still personally don't think that's a good way of sorting the
string, you are relying on errors. If you want to specifically sort by
the numerical digits at the beginning of the string then you should parse
those out prior to checking, being explicit as possible is always going to
be the most reliable way. If you want to perform an alphabetical sorting
then change your code to suit, relying on the same method to do it out of
convenience doesn't appear to be working for you.

Also how about using the StringComparer class?

http://msdn.microsoft.com/en-us/library/system.stringcomparer.aspx

Also you can create a custom IComparer and then peform a more advanced
form of sorting where you can take any part of the string in for account.
Anyway, this is just my personal opinion of course, according to the docs
on Val,

"The Val function stops reading the string at the first character it
cannot recognize as part of a number. Symbols and characters that are
often considered parts of numeric values, such as dollar signs and commas,
are not recognized. However, the function recognizes the radix prefixes &O
(for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed
characters are stripped from the argument."

I can't see anywhere on the page mentioning that it's specifically used
for comparing characters on their own, hence why I personally suggest
going the other route.

Anyway I hope this helps.

Nick.
 
I have to sort a list of Employees by the alphanumeric Employee Number (not
the primary key, but a unique index available to the user).

Sometimes users want to sort by the initial numerals of the employee number.
"2abc" should come before "11abc".

I thought the val function should accept any ascii characters. The val
function should return a double: val("2abc") = 2.0 val("11abc") = 11.0
val("abc") = 0.0

The value which caused the error was "d486091": val("d48609") = -1.#IND

My question is, why does "d48609" return this, when "f48609" returns 0.0, as
expected?

Thanks for your help.

Bill

Bill, I just steped into the Val source code, and the reason is the d as the
first character. It specifically checks this, so it is interpreting your
value as 10 ^ 48609 power. This causes System.Double to over flow....

The characters it specifically checks for are E, e, D, d. To be honest, I can
understand E/e - but I'm not familiar with using D/d. Must be a notation I
missed somewhere :)
 
I have to sort a list of Employees by the alphanumeric Employee Number (not
the primary key, but a unique index available to the user).

Sometimes users want to sort by the initial numerals of the employee number.
"2abc" should come before "11abc".

I thought the val function should accept any ascii characters. The val
function should return a double: val("2abc") = 2.0 val("11abc") = 11.0
val("abc") = 0.0

The value which caused the error was "d486091": val("d48609") = -1.#IND

My question is, why does "d48609" return this, when "f48609" returns 0.0, as
expected?

Thanks for your help.

Bill

Bill, I just steped into the Val source code, and the reason is the d as the
first character. It specifically checks this, so it is interpreting your
value as 10 ^ 48609 power. This causes System.Double to over flow....

The characters it specifically checks for are E, e, D, d. To be honest, I can
understand E/e - but I'm not familiar with using D/d. Must be a notation I
missed somewhere :)
 
BillE said:
I have to sort a list of Employees by the alphanumeric Employee Number (not
the primary key, but a unique index available to the user).

Sometimes users want to sort by the initial numerals of the employee number.
"2abc" should come before "11abc".

I thought the val function should accept any ascii characters. The val
function should return a double: val("2abc") = 2.0 val("11abc") = 11.0
val("abc") = 0.0

I see your need.

This is where a C/C++ strtol() functionality would work nicely. It
will convert the first part as a long and also return a pointer to the
reminder strings where it stopped.

long strtol(const char *s, char *out, base)

in VB, it would be something like (ignoring base)

function strtol(byval sin as string, byref last as string) as long
dim npart as string = ""
last = ""
for each ch as char in sin
if last = "" andalso ch >= "0"c andalso ch <= "9"c then
npart += ch
else
last += ch
end if
next
return ctype(val(npart), long)
end function

Now you can use this to create a lexical sortable string function:

function SortableString(byval sin as string) as string
dim sl as string
dim l as long = strtol(sin,sl)
sl = right("00000000"+hex(l),8)+sl
return sl.ToUpper()
end function

Usage:

console.writeline("{0}",SortableString("2abc"))
console.writeline("{0}",SortableString("11abc"))

result:

00000002ABC
0000000BABC

--
 
BillE said:
I have to sort a list of Employees by the alphanumeric Employee Number (not
the primary key, but a unique index available to the user).

Sometimes users want to sort by the initial numerals of the employee number.
"2abc" should come before "11abc".

I thought the val function should accept any ascii characters. The val
function should return a double: val("2abc") = 2.0 val("11abc") = 11.0
val("abc") = 0.0

I see your need.

This is where a C/C++ strtol() functionality would work nicely. It
will convert the first part as a long and also return a pointer to the
reminder strings where it stopped.

long strtol(const char *s, char *out, base)

in VB, it would be something like (ignoring base)

function strtol(byval sin as string, byref last as string) as long
dim npart as string = ""
last = ""
for each ch as char in sin
if last = "" andalso ch >= "0"c andalso ch <= "9"c then
npart += ch
else
last += ch
end if
next
return ctype(val(npart), long)
end function

Now you can use this to create a lexical sortable string function:

function SortableString(byval sin as string) as string
dim sl as string
dim l as long = strtol(sin,sl)
sl = right("00000000"+hex(l),8)+sl
return sl.ToUpper()
end function

Usage:

console.writeline("{0}",SortableString("2abc"))
console.writeline("{0}",SortableString("11abc"))

result:

00000002ABC
0000000BABC

--
 
I see; it is interesting, however, that val("d4860") = 0.0, not 10^4860.
val("d2") = 0.0, but it would seem that it should be 1000.0. Val("10e2") =
1000.0
 
I see; it is interesting, however, that val("d4860") = 0.0, not 10^4860.
val("d2") = 0.0, but it would seem that it should be 1000.0. Val("10e2") =
1000.0
 
Back
Top