Asked by:
Performing calculation within VB.net program and getting -1.#IND as result

-
text/sourcefragment 7/27/2013 12:06:46 AM Joanie Brows 0
My program seemed to be working until I checked text output file and seen "NaN" in certain places. I wasn't sure why that was so went back into my program where that calculation was being performed and stopped it on the line where "NaN' was being calculated. When I put my mouse over the variable holding that value (wIndex) I see -1#IND.
This is the line of code that performs the calculation:
Dim wIndex As Double = Format(((H13 / 100) / (H6 / 100) * 100), "###.000")
Both variables being used ('H13' and 'H6') are zero for the record being read here.
When I do the calculation by hand, I get '0'.
So, like I said earlier, when I output this to a .txt file, I see "NaN", but what's even stranger (to me anyway - lol), is that when i output this to an Excel file, I get '65535'. I'm not sure if this means anything, but 65535 is the second to last line of the Excel file (the last like is 65536, the maximum number of lines that can exist in an Excel file prior to MS Office 2007, I'm running 2003).
Any guidance is greatly appreciated :)
Thanks!
Joanie
.
-Joni :)
Question
All replies
-
text/html 7/27/2013 12:18:00 AM Frank L. Smith 0
Joni,
Is Option Strict set to on?
(shaking my head nooooo)
Unless I'm missing something, you're delcaring a double then trying to do string manipulation in the middle of it.
Get your value type (the double), then use the .ToString overload or the String.Format AFTER the calculation has been performed.
Please call me Frank :)
-
text/html 7/27/2013 12:36:09 AM leshay 0
-
text/html 7/27/2013 12:44:01 AM Frank L. Smith 0
Hi
When you did the calculation by hand, you say the two variable values were 0.
If they were zero, then your calculation is trying to perform a divide by zero is it not?
Regards Les, Livingston, Scotland
Most likely so - and I was hoping to get there, but first she's essentially declaring a variable then telling it that it's equal to a string -- which obviously it's not.
The program needs to test each of the variables, then if it passes testing for being non-zero, perform the calculation and return a string showing it however she wants to.
Putting it all in one statement like that will make it very difficult to do such testing.
Please call me Frank :)
-
text/html 7/27/2013 12:47:09 AM Paul Ishak 065535 is the Max value for an unsigned short currently. Not sure off the top of my head, but I believe that used to be the max value of an unsigned integer in earlier versions of windows.
“If you want something you've never had, you need to do something you've never done.”
Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
-
text/sourcefragment 7/27/2013 1:51:35 AM Joanie Brows 0
Thank you both for your response... I'm trying to take it all in a figure out what I should be doing here.
Les, yes I suppose you're correct. I am trying to divide by 0. It would be (0 /100) / (0/100) * 100.
Frank.. how've you been :)
I've tried changing it to this:
Dim XwIndex As Double = ((H13 / 100) / (H6 / 100) * 100) Dim wIndex As String = XwIndex.ToString("###.000")
I'm still getting 'NaN' in my text file though. Is this what you meant?
In all the cases I've checked up on in my output file, where ever I see "NaN" should be '0'.
I've tried doing this and all the 'NaN's are gone:
Dim wIndex As Double = Format(((H13 / 100) / (H6 / 100) * 100), "###.000") If (Double.IsNaN(wIndex)) Then wIndex = 0 End If
Would it be terrible if I did it this way?
-Joni :)
-
text/html 7/27/2013 1:58:25 AM Frank L. Smith 0
How have I been?
I'm horrible, I tell ya, horrible! ;-)
To this though - consider creating a function which will pass out a string.
Pass in two arguments (H13 and H6), test if either are zero and if so, pass out the string "N/A" or whatever suits you, and if nonzero, perform the calculation and pass out the formatted string of the result the way you want.
******
You never answered my last e-mail about XML ... even though you started it! ;-)
Please call me Frank :)
-
text/sourcefragment 7/27/2013 3:44:13 AM Joanie Brows 0
Thanks Frank.. I see what you're saying.
My thing is this though - the program I'm writing is actually a rewrite of an older program written by someone else. The I already have an example of what the output should be so it's easy for me to check if my output is correct or not. In all the instances that I come across 'NaN' in my output, the original output that I'm checking against shows '0'.
So, in your opinion, would it be okay for me to do it as I showed in my previous post?
Dim wIndex As Double = Format(((H13 / 100) / (H6 / 100) * 100), "###.000") If (Double.IsNaN(wIndex)) Then wIndex = 0 End If
Instead of passing back an 'N/A', I would just be setting it to '0'.
What do you think???
-Joni :)
-
text/sourcefragment 7/27/2013 9:55:56 AM leshay 1
Hi
Try this:
Dim H13 As Double = 1 Dim H6 As Double = 1 Dim XwIndex As Double = 0 If Not H6 = 0 Then XwIndex = ((H13 / 100) / (H6 / 100) * 100) Else XwIndex = 0 End If Dim wIndex As String = XwIndex.ToString("##0.000")
Regards Les, Livingston, Scotland
-
text/sourcefragment 7/27/2013 10:36:16 AM Frank L. Smith 0
Thanks Frank.. I see what you're saying.
My thing is this though - the program I'm writing is actually a rewrite of an older program written by someone else. The I already have an example of what the output should be so it's easy for me to check if my output is correct or not. In all the instances that I come across 'NaN' in my output, the original output that I'm checking against shows '0'.
So, in your opinion, would it be okay for me to do it as I showed in my previous post?
Dim wIndex As Double = Format(((H13 / 100) / (H6 / 100) * 100), "###.000") If (Double.IsNaN(wIndex)) Then wIndex = 0 End If
Instead of passing back an 'N/A', I would just be setting it to '0'.
What do you think???
-Joni :)
You're still setting a type double to a string.
Go with what Les just posted or if you want to keep it even more simple, use a Try/Catch and in the Catch, return zero.
Please call me Frank :)
-
text/sourcefragment 7/27/2013 2:33:19 PM dbasnett 1
Zero in either variable is an issue. Here is a function that might help.
Private Function foo(H13 As Double, H6 As Double) As String If H13 = 0 OrElse H6 = 0 Then Return "0" Else Dim XwIndex As Double = (H13 / 100) / (H6 / 100) * 100 Return XwIndex.ToString("###.000") End If End Function
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein
-
text/html 7/29/2013 2:26:14 AM Carl Cai - MSFT 0
Hi Joanie Brows,
I am writing to check the status of the issue on your side.
If these suggestions don't work,please give your positive response.Thanks!
Carl Cai
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.