Division in VB.NET - vb.net

What's the difference between / and \ for division in VB.NET?
My code gives very different answers depending on which I use. I've seen both before, but I never knew the difference.

There are two ways to divide numbers. The fast way and the slow way. A lot of compilers try to trick you into doing it the fast way. C# is one of them, try this:
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine(1 / 2);
Console.ReadLine();
}
}
Output: 0
Are you happy with that outcome? It is technically correct, documented behavior when the left side and the right side of the expression are integers. That does a fast integer division. The IDIV instruction on the processor, instead of the (infamous) FDIV instruction. Also entirely consistent with the way all curly brace languages work. But definitely a major source of "wtf happened" questions at SO. To get the happy outcome you would have to do something like this:
Console.WriteLine(1.0 / 2);
Output: 0.5
The left side is now a double, forcing a floating point division. With the kind of result your calculator shows. Other ways to invoke FDIV is by making the right-side a floating point number or by explicitly casting one of the operands to (double).
VB.NET doesn't work that way, the / operator is always a floating point division, irrespective of the types. Sometimes you really do want an integer division. That's what \ does.

10 / 3 = 3.333
10 \ 3 = 3 (the remainder is ignored)

/ Division
\ Integer Division

10 / 3 = 3.33333333333333, assigned to integer = 3
10 \ 3 = 3, assigned to integer = 3
20 / 3 = 6.66666666666667, assigned to integer = 7
20 \ 3 = 6, assigned to integer = 6
Code for the above:
Dim a, b, c, d As Integer
a = 10 / 3
b = 10 \ 3
c = 20 / 3
d = 20 \ 3
Debug.WriteLine("10 / 3 = " & 10 / 3 & ", assigned to integer = " & a)
Debug.WriteLine("10 \ 3 = " & 10 \ 3 & ", assigned to integer = " & b)
Debug.WriteLine("20 / 3 = " & 20 / 3 & ", assigned to integer = " & c)
Debug.WriteLine("20 \ 3 = " & 20 \ 3 & ", assigned to integer = " & d)

Related

Using factorials to find combinations

So, this is a two part question but based on the same project. I am trying to write a small program that can illustrate how a computer can quickly crack a password, using a brute force attack. It only has three inputs: A check box to denote if it should use integers, a check box to denote if it should use letters, and a textbox to enter the password to be cracked. It then outputs the number of combinations. Here is my code:
dim a,b,c,d,P as double
'Using the following formula:
'P(n,r) = n!/(r!(n-r)!)
'Let's assume we are just using numbers, so n = 10
'r = the count of characters in the textbox.
a = factorial(n)
b = factorial(r)
c = (n - r)
d = factorial(c)
P = a / (b * d)
Output = "With a password of " & r & " characters and " & n & " possible values, the number of combinations are " & P
Me.RichTextBox1.Text = Output & vbCrLf
Function factorial(ByVal n As Integer) As Integer
If n <= 1 Then
Return 1
Else
Return factorial(n - 1) * n
End If
End Function
So, let's assume I'm only looking at the characters 0-9, with the following number of characters in a password, I get:
P(10,1) = 10!/(1! * (10-1)!) = 10
P(10,2) = 10!/(2! * (10-2)!) = 45
P(10,3) = 10!/(3! * (10-3)!) = 120
P(10,4) = 10!/(4! * (10-4)!) = 210
P(10,5) = 10!/(5! * (10-5)!) = 252
P(10,6) = 10!/(6! * (10-6)!) = 210
P(10,7) = 10!/(6! * (10-7)!) = 120
You can see the number of combinations goes down, once it gets past 5. I assume this is right, but wanted to check before I present this. Is this because the total number in the pool remains the same, while the sample increases?
My second question is about how to consider a password to crack that repeats numbers. Again, let's assume that we are just pulling from digits 0-9. If the sample size it two (lets say 15), then there are 45 possible combinations, right? But, what if they put in 55? Are there still 45 combinations? I suppose the computer still needs to iterate over every possible combination, so it would still be considered 45 possibilities?

Rounding a variable to an integer

I have a calculation like this: 3 * 12300 / 160. The result is: 230.625. But I just want the integer part, 230.
In C, this can be done using something like this: int MyVar = (int)3*12300/160;
Is there a way in VBA (With MS-Access) for force the result to be an integer?
You can round down using the Int or Fix functions.
Since you know the result you want is a whole number, you should store the result in a variable of type Long (or Integer if you're absolutely certain it will always be smaller than 32768).
Dim l As Long
l = Int(3 / 160 * 12300) ' <~~~~ Yes, I switched the numbers around on purpose!*
MsgBox "l = " & l
* Why did I switch the numbers around? Because the expression 3 * 12300 / 160 will throw an error in VBA. Read here why: Overflow when multiplying Integers and assigning to Long

VB.NET doesn't round numbers correctly?

I'm testing the speed of some functions so I made a test to run the functions over and over again and I stored the results in an array. I needed them to be sorted by the size of the array I randomly generated. I generate 100 elements. Merge sort to the rescue! I used this link to get me started.
The section of code I'm focusing on:
private void mergesort(int low, int high) {
// check if low is smaller then high, if not then the array is sorted
if (low < high) {
// Get the index of the element which is in the middle
int middle = low + (high - low) / 2;
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
}
}
which translated to VB.NET is
private sub mergesort(low as integer, high as integer)
' check if low is smaller then high, if not then the array is sorted
if (low < high)
' Get the index of the element which is in the middle
dim middle as integer = low + (high - low) / 2
' Sort the left side of the array
mergesort(low, middle)
' Sort the right side of the array
mergesort(middle + 1, high)
' Combine them both
merge(low, middle, high)
end if
end sub
Of more importance the LOC that only matters to this question is
dim middle as integer = low + (high - low) / 2
In case you wanna see how merge sort is gonna run this baby
high low high low
100 0 10 0
50 0 6 4
25 0 5 4
12 0 12 7
6 0 10 7
3 0 8 7
2 0 :stackoverflow error:
The error comes from the fact 7 + (8 - 7) / 2 = 8. You'll see 7 and 8 get passed in to mergesort(low, middle) and then we infinite loop. Now earlier in the sort you see a comparison like this again. At 5 and 4. 4 + (5 - 4) / 2 = 4. So essentially for 5 and 4 it becomes 4 + (1) / 2 = 4.5 = 4. For 8 and 7 though it's 7 + (1) / 2 = 7.5 = 8. Remember the numbers are typecasted to an int.
Maybe I'm just using a bad implementation of it or my typecasting is wrong, but my question is: Shouldn't this be a red flag signaling something isn't right with the rounding that's occuring?
Without understanding the whole algorithm, note that VB.NET / is different than C# /. The latter has integer division by default, if you want to truncate decimal places also in VB.NET you have to use \.
Read: \ Operator
So i think that this is what you want:
Dim middle as Int32 = low + (high - low) \ 2
You are correct in your diagnosis: there's something inconsistent with the rounding that's occurring, but this is entirely expected if you know where to look.
From the VB.NET documentation on the / operator:
Divides two numbers and returns a floating-point result.
This documentation explicitly states that , if x and y are integral types, x / y returns a Double. So, 5 / 2 in VB.NET would be expected to be 2.5.
From the C# documentation on the / operator:
All numeric types have predefined division operators.
And further down the page:
When you divide two integers, the result is always an integer.
In the case of C#, if x and y are integers, x / y returns an integer (rounded down). 5 / 2 in C# is expected to return 2.

Calculate possible equations that produce a given integer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an integer of x and I am trying to find what equation is possible to produce that integer. This is for the purpose of script obfuscation. I have solved this problem for addition, subtraction, division, square roots, xor, and hex. Most of my answers involve using an inverse equation to solve. I am looking to solve it for:
Multiplication
Modulus
Exponents
Scientific Notation
Logarithm
Any others I did not mention
Here is the Function that I started:
Public Function IntegerBreak(PickedInteger)
Randomize()
Dim Switch, Entropy, Result As Integer
Switch = Int(6 * Rnd()) + 1
Entropy = Int(100 * Rnd()) + 1
Select Case Switch
Case 1
'addition
Result = PickedInteger - Entropy
Return Result & " + " & Entropy & " = " & PickedInteger
Case 2
'subtraction
Result = PickedInteger + Entropy
Return Result & " - " & Entropy & " = " & PickedInteger
Case 3
'division
Result = PickedInteger * Entropy
Return Result & " / " & Entropy & " = " & PickedInteger
Case 4
'squares
Result = PickedInteger ^ 2
Return "Sqr (" & Result & ") = " & PickedInteger
Case 5
'xor
Result = PickedInteger Xor Entropy
Return Result & " Xor " & Entropy & " = " & PickedInteger
Case 6
'hex
Return "&H" & Conversion.Hex(PickedInteger) & " = " & PickedInteger
End Select
End Function
Sample Output:
-47 + 57 = 10
89 - 79 = 10
260 / 26 = 10
Sqr(100) = 10
12 Xor 6 = 10
&HA = 10
EDIT: My specific question is how to find out:
(z*y=x, z%y=x, z^y=x, zLogy=x, or 3.123^10=x)
What is stopping me from continuing is I do not have a solid background in mathematics, I ran into issues for multiplication because there can be multiple answers on how you calculate an equation:
(2*6=12, 3*4=12)
Using the inverse method gave me decimal numbers. I would like any advice for calculating Multiplication, Modulus, Exponents, Scientific Notation, and Logarithms.
Also what is some good information for learning obfuscation?
Answers are acceptable in vb.net or vbscript or mathematical pseudocode.
Pointing me in the right direction would be appreciated.
My specific question is how to find out:
(z*y=x, z%y=x, z^y=x, zLogy=x, or 3.123^10=x)
For multiplication, divide both sides by y and you get:
z = x / y
Note that even if x and y are both integers, you may get a fractional z (eg. 2 / 7).
For modulus, note that the result of z % y cannot be greater than or equal to y. So choosing an arbitrary y probably won't be useful, since depending on the value chosen you may not be able to reach x at all. You could do it like this:
z = x + rnd()*y
as long as you choose a y value greater than x. Note that this definitely only works for integers.
For power, if x ^ y = z then:
x = z ^ (1/y)
(Solving y ^ x = z for x is completely different.)
Your logarithm example doesn't make a lot of sense because log is a unary operation, which means it only operates on one number instead of two. Unless you actually mean "log base y", in which case for log{y} x = z:
x = y ^ z

Optimisation of Law of Cosines in VB.net

I'm using the law of cosines in a program and it seems to be a slow point of my code. This is the line of code I have:
Ans = Math.Sqrt(A ^ 2 + B ^ 2 - 2 * A * B * Math.Cos(C - D))
Where A to D are double variables that change every time called. This function seems to take around 2000 ticks to run. I've looked at using the small angle approximation which is where if (C-D) is small enough you can use cos(C-D) = 1 - ((C-D)^2)/2. Unfortunately this turned out to be slower overall than the original code. I've looked at any sort of relationship that can be used to simplify the calculation but A and C are related in a complex way and B and D are related in the same way, there's no relationship between A and B or between C and D.
I've thought about using a lookup function for all the values of (C-D) but my accuracy is currently to at least 6 significant figures and I would prefer to stay at that level as that's the accuracy of my input data, in short this means around a million vaules in the look up and this is only 1 section of the function. I've thought about having a look up for all four values (A, B, C and D) but I'm not sure how to implement that.
I've also already multithreaded this application and attempted use of GPGPU (GPGPU ended up being slower due to the time spent loading in and out of GPU memory).
So, my question is how do I speed up this function.
Thanks in advanced!
The following runs in less than 1/3 the time
ans = Math.Sqrt(a * a + b * b - 2 * a * b * Math.Cos(c - d))
Here's the code that proves it:
Dim sw1 As New Stopwatch
Dim sw2 As New Stopwatch
Dim ans, a, b, c, d As Double
a = 5
b = 10
c = 4
d = 2
sw1.Start()
For x As Integer = 1 To 10000
ans = Math.Sqrt(a ^ 2 + b ^ 2 - 2 * a * b * Math.Cos(c - d))
Next
sw1.Stop()
sw2.Start()
For y As Integer = 1 To 10000
ans = Math.Sqrt(a * a + b * b - 2 * a * b * Math.Cos(c - d))
Next
sw2.Stop()
Console.WriteLine(sw1.ElapsedTicks)
Console.WriteLine(sw2.ElapsedTicks)
Console.WriteLine(sw2.ElapsedTicks * 100 / sw1.ElapsedTicks)