1

I'm trying to convert this ASP.net(vb) script to pure Javascript and I'm running into this problem. The script I'm converting has the following:

Dim F as Integer
Dim Theta(100) as Double

F ends up with the value: 1 Theta ends up with the following Value:

[0, 1.2669233316773538, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Then some math calculations are done and inside the formula the Theta array is used like so:

Cos(Theta(F / 2))

Doing simple math here, F / 2, (or 1 / 2) = 0.5 There is no array key 0.5 so in my Javascript code its like saying:

Math.cos(Theta[0.5])

Which ends up doing:

Math.cos(undefined)

Which of course breaks the math.

My question is simple. I do not have an IIS server or any way to make modifications to the vb.net script so that I can add debugging output to see how its working. I'm basically doing this blind and with very little knowledge of any .net languages nor ASP.

Since F is an Integer, is dividing F in the vb.net script going to yield a whole number? Is the rounding forced due to the type of F?

Like, in this script, which one of these is happening?: Theta[0] (concatenated) or Theta[1] (rounded) or Theta[0.5] (same as javascript)?

Help is greatly appreciated!

Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
Rick Kukiela
  • 1,135
  • 1
  • 15
  • 34
  • [This](http://en.wikibooks.org/wiki/Visual_Basic_.NET/Arithmetic_operators#Division) should answer your question: `/` is floating division, ` \ ` is integral division. (Can't make the backslash pretty >.< ) Thus, `1 / 2` is `0.5`. – Amadan Jan 07 '13 at 09:47
  • Your reference does not show what would happen if you did: Dim x as Integer and then did floating point division: x = 3/4 - Also the question is not how the result variable is declared. In my question F is an integer and F / 2 is used as an array index. How can vb have decimal array indices? – Rick Kukiela Jan 07 '13 at 10:06
  • You're right, it doesn't. The division proceeds as I say; but since array indices must be integral, it will get rounded, so `0.5` becomes `1` as an index. – Amadan Jan 07 '13 at 10:11

1 Answers1

2

Have a look at this answer: VB.NET vs C# integer division

VB.NET has two operator for the division:

  • "/" division: the result will be rounded
  • "\" Integer division: the result will be truncated

In your case as your script uses "/", the result will be 1 (1 / 2 = 0.5 rounded to 1).

Your JavaScript

Math.cos(Theta[0.5])

should become

Math.cos(Theta[Math.round(0.5)])
Community
  • 1
  • 1
Gabriel
  • 1,572
  • 1
  • 17
  • 26