Rick Kukiela
Rick Kukiela

Reputation: 1264

vb.net integer division

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!

Upvotes: 1

Views: 471

Answers (1)

Gabriel
Gabriel

Reputation: 1652

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)])

Upvotes: 2

Related Questions