val function returns NaN

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

BillE

I am using vb.net 2005, windows forms.

What expression will cause the Val() function to return NaN?

A user is reporting that this has occurred, but I can't find any test
expression to provide to the Val function which will return NaN. I have
requested the data so I can attempt to reproduce the error, but I don't know
if I will be able to get it.

I am using the Val function to obtain values from an alphanumeric field in
order to sort the rows in a datagrid accordingly.

Thanks
Bill
 
Hello, Bill,

I don't know any string for which Val will return NaN.

Are you sure that it's coming directly from the Val (and not some subsequent
code)?

Is the user actually reporting that Val is returning a NaN? (Must be a
fairly sophisticated user.)

Perhaps if you can show some of the code, someone will be able to offer some
ideas.

Cheers,
Randy
 
Hello, Bill,

I don't know any string for which Val will return NaN.

Are you sure that it's coming directly from the Val (and not some subsequent
code)?

Is the user actually reporting that Val is returning a NaN? (Must be a
fairly sophisticated user.)

Perhaps if you can show some of the code, someone will be able to offer some
ideas.

Cheers,
Randy
 
BillE said:
I am using vb.net 2005, windows forms.

What expression will cause the Val() function to return NaN?

NaN = Not a Number

Anything that is not an alphanumeric will produce this.

"123" <-- alphanumeric
A123" said:
A user is reporting that this has occurred, but I can't find any test
expression to provide to the Val function which will return NaN. I have
requested the data so I can attempt to reproduce the error, but I don't know
if I will be able to get it.

I am using the Val function to obtain values from an alphanumeric field in
order to sort the rows in a datagrid accordingly.

You must have a non-alphanumeric in a cell.

What strikes me odd is that I only normally see "NaN" in languages
like JavaScript, Jscript or VBScript perhaps. In VB and I just tried
it under VB.NET, you get a ZERO result.

------------ cut here ----------
' file: test_val.vb

Option Strict On
Option Explicit On
Module Module1
function atoi(byval s as string) as integer
return cint(val(s))
end function

sub test(byval s as string)
dim n as integer = atoi(s)
console.writeline("atoi('{0}') ==> {1}",s,n)
end sub

Sub Main()
test("a123")
test("123")
test("+++")
test("789x25")
End Sub
End Module
------------ cut here ----------

result:

atoi('a123') ==> 0
atoi('123') ==> 123
atoi('+++') ==> 0
atoi('789x25') ==> 789

Note, the last one is VALID in the C world, which sort of tells me the
VAL() command is either implemented via the C RTL (Run Time Library)
atoi() function or MS made the VAL behave the same. By design, atoi()
will stop at the first non numeric character. We write a BASIC
compiler for our server product and we also use the Windows RTL atoi()
function in our RTE (Run Time Engine) for the VAL() command.

But again, I never saw or would expect NaN results in VB's VAL
command. That comes from script languages like jscript.

So I think you need to recheck the user's report or check if VBSCRIPT
behaves this way because I would never expect this in VB and I don't
see it in VB.NET as shown above.

--
 
BillE said:
I am using vb.net 2005, windows forms.

What expression will cause the Val() function to return NaN?

NaN = Not a Number

Anything that is not an alphanumeric will produce this.

"123" <-- alphanumeric
A123" said:
A user is reporting that this has occurred, but I can't find any test
expression to provide to the Val function which will return NaN. I have
requested the data so I can attempt to reproduce the error, but I don't know
if I will be able to get it.

I am using the Val function to obtain values from an alphanumeric field in
order to sort the rows in a datagrid accordingly.

You must have a non-alphanumeric in a cell.

What strikes me odd is that I only normally see "NaN" in languages
like JavaScript, Jscript or VBScript perhaps. In VB and I just tried
it under VB.NET, you get a ZERO result.

------------ cut here ----------
' file: test_val.vb

Option Strict On
Option Explicit On
Module Module1
function atoi(byval s as string) as integer
return cint(val(s))
end function

sub test(byval s as string)
dim n as integer = atoi(s)
console.writeline("atoi('{0}') ==> {1}",s,n)
end sub

Sub Main()
test("a123")
test("123")
test("+++")
test("789x25")
End Sub
End Module
------------ cut here ----------

result:

atoi('a123') ==> 0
atoi('123') ==> 123
atoi('+++') ==> 0
atoi('789x25') ==> 789

Note, the last one is VALID in the C world, which sort of tells me the
VAL() command is either implemented via the C RTL (Run Time Library)
atoi() function or MS made the VAL behave the same. By design, atoi()
will stop at the first non numeric character. We write a BASIC
compiler for our server product and we also use the Windows RTL atoi()
function in our RTE (Run Time Engine) for the VAL() command.

But again, I never saw or would expect NaN results in VB's VAL
command. That comes from script languages like jscript.

So I think you need to recheck the user's report or check if VBSCRIPT
behaves this way because I would never expect this in VB and I don't
see it in VB.NET as shown above.

--
 
BillE said:
I am using vb.net 2005, windows forms.

What expression will cause the Val() function to return NaN?

A user is reporting that this has occurred, but I can't find any test
expression to provide to the Val function which will return NaN. I have
requested the data so I can attempt to reproduce the error, but I don't know
if I will be able to get it.

I am using the Val function to obtain values from an alphanumeric field in
order to sort the rows in a datagrid accordingly.

Thanks
Bill

Val can take objects as a param. This will return a NaN, but I don't think
it applies to what you describe:

Val(Math.Sqrt(-1))

Mike
 
BillE said:
I am using vb.net 2005, windows forms.

What expression will cause the Val() function to return NaN?

A user is reporting that this has occurred, but I can't find any test
expression to provide to the Val function which will return NaN. I have
requested the data so I can attempt to reproduce the error, but I don't know
if I will be able to get it.

I am using the Val function to obtain values from an alphanumeric field in
order to sort the rows in a datagrid accordingly.

Thanks
Bill

Val can take objects as a param. This will return a NaN, but I don't think
it applies to what you describe:

Val(Math.Sqrt(-1))

Mike
 
The code below retrieves a dataset of employees, including an alphanumeric
string field named "ENum" (Employee number).

It then adds a column named valENum to the data table to contain the result
of the expression val(dr("ENum")).

The data view can then be sorted by the employee name column, or by the
valENum column.

The user reported the following error when the valENum column is populated:

"Arithmetic operation resulted in an overflow. Couldn't store <NaN> in
valENum column. Expected type is Int64"

Thanks for your help!

Bill

Dim dt As DataTable = ds.Tables(0)

dt.Columns.Add("valENum", GetType(Long))

For Each dr As DataRow In dt.Rows

dr("valENum") = Val(dr("ENum"))

Next

Dim dv As New DataView(dt)

Return dv
 
The code below retrieves a dataset of employees, including an alphanumeric
string field named "ENum" (Employee number).

It then adds a column named valENum to the data table to contain the result
of the expression val(dr("ENum")).

The data view can then be sorted by the employee name column, or by the
valENum column.

The user reported the following error when the valENum column is populated:

"Arithmetic operation resulted in an overflow. Couldn't store <NaN> in
valENum column. Expected type is Int64"

Thanks for your help!

Bill

Dim dt As DataTable = ds.Tables(0)

dt.Columns.Add("valENum", GetType(Long))

For Each dr As DataRow In dt.Rows

dr("valENum") = Val(dr("ENum"))

Next

Dim dv As New DataView(dt)

Return dv
 
I actually have a screen shot of the error I quoted in my last message,
which also has the code which generates the error.

Thanks for your help!
Bill

'get a dataset of all employees, including the alphanumeric field ENum
(Employee Number, i.e. '1234A')
'get the datatable
Dim dt As DataTable = ds.Tables(0)
'add a new column to the data table to store the Val(ENum)
dt.Columns.Add("valENum", GetType(Long))
'loop through the datatable and populate the valENum column
For Each dr As DataRow In dt.Rows
'the error occurs on the following assignment
dr("valENum") = Val(dr("ENum"))
Next
Dim dv As New DataView(dt)

Return dv

The user reported the following error when the valENum column is populated:

"Arithmetic operation resulted in an overflow. Couldn't store <NaN> in
valENum column. Expected type is Int64"



Thanks!
Bill
 
I actually have a screen shot of the error I quoted in my last message,
which also has the code which generates the error.

Thanks for your help!
Bill

'get a dataset of all employees, including the alphanumeric field ENum
(Employee Number, i.e. '1234A')
'get the datatable
Dim dt As DataTable = ds.Tables(0)
'add a new column to the data table to store the Val(ENum)
dt.Columns.Add("valENum", GetType(Long))
'loop through the datatable and populate the valENum column
For Each dr As DataRow In dt.Rows
'the error occurs on the following assignment
dr("valENum") = Val(dr("ENum"))
Next
Dim dv As New DataView(dt)

Return dv

The user reported the following error when the valENum column is populated:

"Arithmetic operation resulted in an overflow. Couldn't store <NaN> in
valENum column. Expected type is Int64"



Thanks!
Bill
 
Family said:
Val can take objects as a param. This will return a NaN, but I don't think
it applies to what you describe:
Val(Math.Sqrt(-1))

Very interesting Tom.

The NaN is coming from Math.Sqrt(-1) not Val itself, actually, it is
System.Double that is providing NaN and other error constants.
 
Family said:
Val can take objects as a param. This will return a NaN, but I don't think
it applies to what you describe:
Val(Math.Sqrt(-1))

Very interesting Tom.

The NaN is coming from Math.Sqrt(-1) not Val itself, actually, it is
System.Double that is providing NaN and other error constants.
 
Mike said:
Very interesting Tom.

The NaN is coming from Math.Sqrt(-1) not Val itself, actually, it is
System.Double that is providing NaN and other error constants.


Tom? :-)

Yes, I could have just as easily supplied double.NaN, I believe.

The point is that this is a way in which Val will return a NaN.
 
Mike said:
Very interesting Tom.

The NaN is coming from Math.Sqrt(-1) not Val itself, actually, it is
System.Double that is providing NaN and other error constants.


Tom? :-)

Yes, I could have just as easily supplied double.NaN, I believe.

The point is that this is a way in which Val will return a NaN.
 
Clearly, your exception is not related to the Val function because your
exception shows that the problem is to do with assigning the result of the
Val function to the valENum column.

The exception also shows that the valENum column is expection a value of
type Int64 (or Long).

The Val function is returning a value of type Double. This is the expected
behaviour for the Val function overload that takes a value of type Object as
the input parameter. (Whether you realise it or not, interrogating
dr("ENum") without casting returns a value of type Object).

The other clue is that the exception reports that the result of the call to
the Val function is 'NaN'. This is a field of the Double type and ties up
with what we already know about the return type of the Val function. It
means that a value is not a number.

To determine what input value is causing the Val function to return
Double.NaN, you need to add some diagnostic logic, for example:

Try
Dim _d As Double = Val(dr("ENum"))
dr("valENum") = _d
Catch _ex As Exception
' Show the value in dr("ENum") that is causing the Val function to
return Double.NaN
' Display the full stack trace
Console.WriteLine(_ex.ToString())
Console.WriteLine("Input from dr("ENum") = {0}, dr("ENum"))
Console.WriteLine("Output to _d = {0}, _d)
End Try
 
Clearly, your exception is not related to the Val function because your
exception shows that the problem is to do with assigning the result of the
Val function to the valENum column.

The exception also shows that the valENum column is expection a value of
type Int64 (or Long).

The Val function is returning a value of type Double. This is the expected
behaviour for the Val function overload that takes a value of type Object as
the input parameter. (Whether you realise it or not, interrogating
dr("ENum") without casting returns a value of type Object).

The other clue is that the exception reports that the result of the call to
the Val function is 'NaN'. This is a field of the Double type and ties up
with what we already know about the return type of the Val function. It
means that a value is not a number.

To determine what input value is causing the Val function to return
Double.NaN, you need to add some diagnostic logic, for example:

Try
Dim _d As Double = Val(dr("ENum"))
dr("valENum") = _d
Catch _ex As Exception
' Show the value in dr("ENum") that is causing the Val function to
return Double.NaN
' Display the full stack trace
Console.WriteLine(_ex.ToString())
Console.WriteLine("Input from dr("ENum") = {0}, dr("ENum"))
Console.WriteLine("Output to _d = {0}, _d)
End Try
 
Hello, Bill,

Re:
"Arithmetic operation resulted in an overflow. Couldn't store <NaN> in
valENum column. Expected type is Int64"

As Mike suggests, there is likely unexpected data in the "ENum" field.
My guess is that the error is generated within the assignment to
dr("valENum"). Val will return a Double (unless its argument is a Char)
and this must be converted to Int64 (i.e. Long) to fit in the new field.

Maybe the value returned by Val actually is NaN, but it may also be that
the error message returned by the implicit conversion is incorrect. I
suggest replacing Val with CLng. The explicit conversion may give you a
more meaningful indication of the real problem (and may be more
appropriate anyway).

Cheers,
Randy
 
Hello, Bill,

Re:
"Arithmetic operation resulted in an overflow. Couldn't store <NaN> in
valENum column. Expected type is Int64"

As Mike suggests, there is likely unexpected data in the "ENum" field.
My guess is that the error is generated within the assignment to
dr("valENum"). Val will return a Double (unless its argument is a Char)
and this must be converted to Int64 (i.e. Long) to fit in the new field.

Maybe the value returned by Val actually is NaN, but it may also be that
the error message returned by the implicit conversion is incorrect. I
suggest replacing Val with CLng. The explicit conversion may give you a
more meaningful indication of the real problem (and may be more
appropriate anyway).

Cheers,
Randy
 
Bill,

I think the mite is here:

dt.Columns.Add("valENum", GetType(Long))

that should be a:

dt.Columns.Add("valENum", GetType(Double))

because that is the result of VAL()

Do you have Options Strict On?

You should of gotten a compiler error with the assignmentL

dr("valENum") = Val(dr("ENum"))

--
 
Back
Top