Maybe a simple question but I still need some help with a formula clarification:
Could somebody help me with this:
TempCalc = CInt(TextBox3.Value) * (16*POWER(SQRT(CInt(TextBox1.Value)*(1-CInt(Textbox1.Value)))/(CInt(TextBox1.Value)*CInt(TextBox2.Value);2))
what is wrong, is the syntax totally off?
Thanks in advance
The function POWER and SQRT do not exist in VBA.
Power(x, y) can be replaced with x ^ y
sqrt is sqr in VBA
So the following should work:
TempCalc = CLng(TextBox3.Value) * (16 * ((Sqr(CLng(TextBox1.Value) * 1 - CLng(TextBox1.Value))) / (CLng(TextBox1.Value) * CLng(TextBox2.Value))) ^ 2)
And I recommend to convert the integers to Long using CLng as there is no benefit in using Integer.
So to make it easier to debug I recommend to split the calculation:
Dim SqrtVal As Double
SqrtVal = Sqr(CLng(TextBox1.Value) * 1 - CLng(TextBox1.Value))
Dim DivVal As Double
DivVal = (CLng(TextBox1.Value) * CLng(TextBox2.Value))
Dim PowerVal As Double
PowerVal = (SqrtVal / DivVal) ^ 2
Dim TempCalc As Double
TempCalc = CLng(TextBox3.Value) * 16 * PowerVal
Related
I've done some research and the general advice is changing the datatype of the variable holding the expression to Long or Ulong but in either case, I'm still getting the same error. I also tried enclosing the expression with CInt() (or CLong()) to force it to cut out it's decimal portion to reduce the length of the output of the expression but neither is working. It's all pretty confusing. Any help will be deeply appreciated. The code block triggering the error is as follows;
Vsf(i) = CInt((((0.91544 - 0.00166 * Angle(i) - 0.000002 * W - 0.054248 *
Superelevation(i) - Sidefrictionfactor) / 0.013939) * Radius(i)) ^ 0.5)
Vro(i) = CInt((((1.04136 - 0.004528 * Angle(i) - 0.000004 * W - 0.338711 * Superelevation(i) - rolloverthreshold) / 0.014578) * Radius(i)) ^ 0.5)
Vmin(i) = CInt(Math.Min(Vsf(i), Vro(i)))
I declared VSf(), Vro() and Vmin() all as integer arrays. I even enclosed the computation expression in a CInt() in hopes that it would convert the result of Vro (which was triggering the arithmetic overflow error) to an integer and hence not have to deal with decimals which would lead to more digits. Yet when I hover over Vro(i), I see a 4 digit integer with decimals. Not sure why that's happening.
I broke the formula down to try and see where the problem was occurring. Everything buzzed along until the very end. If the value to be raised to .5 is a negative number, the square root does not produce a number. I would perform the steps of the formula without the ^.5 and then check for a negative number.
Private Sub OPCode()
Dim Angle = 90.2
Dim Radius = 100.2
Dim Superelevation = 37.8
Dim Sidefrictionfactor = 0.003
Dim W = 0.00325
Dim Vsf As Double
Dim AngleMultiplication = 0.00166 * Angle
Dim WMultiplication = 0.000002 * W
Dim SuperelavationMultiplication = 0.054248 * Superelevation
Dim SubtractResult = 0.91544 - AngleMultiplication - WMultiplication - SuperelavationMultiplication - Sidefrictionfactor
Dim DivisionResult = SubtractResult / 0.013939
Dim MultiplyByRadius = DivisionResult * Radius
Debug.Print(MultiplyByRadius.ToString) 'With my made up values I get a negative number
Vsf = MultiplyByRadius ^ 0.5 'You cannot get the square root of a negative number
Debug.Print(Vsf.ToString) 'NaN
Dim intVsf = CInt(Vsf) 'The Arithematic overflow error occurs here
End Sub
I have this code I sorta put together from a few different sites.
It works perfect at creating the bitangent... but for what ever reason. the tangent is thrashed.. In other words and to make this more clear, the code is generating BAD tangents!!
The tangent is usually pointing down, kinda opposite the normal. I'm expecting it to be aligned with the V axis of the texture coordinate..
The strange thing is, I use the cross of the normal and tangent to get the bitangent, which is fine.
I have tried flipping the coordinates, the UVs.. just about everything..
Im not great in math so.. can some one take a look at my code and see if it looks "legit"? Thank you!
Private Sub ComputeTangentBasis(ByRef p0 As vertex_data, ByRef p1 As vertex_data, ByRef p2 As vertex_data)
Dim tangent, bitangent As Vector3D
Dim n As Vector3D
n.X = p0.nx
n.Y = p0.ny
n.Z = p0.nz
'convert to vector3d type... they are WAY easier to do complex math with!!
Dim v0 = convert_vector3d(p0)
Dim v1 = convert_vector3d(p1)
Dim v2 = convert_vector3d(p2)
Dim edge1 = v1 - v2
Dim edge2 = v2 - v0
Dim deltaU1 = (p1.u - p0.u) * -0.1
Dim deltaU2 = (p2.u - p0.u) * -0.1
Dim deltaV1 = (p1.v - p0.v) * -0.1
Dim deltaV2 = (p2.v - p0.v) * -0.1
Dim dividend = (deltaU1 * deltaV2) - (deltaU2 - deltaV1)
Dim f As Double
If dividend = 0.0 Then
f = 1.0
End If
tangent.X = f * ((deltaV2 * edge1.X) - (deltaV1 * edge2.X))
tangent.Y = f * ((deltaV2 * edge1.Y) - (deltaV1 * edge2.Y))
tangent.Z = f * ((deltaV2 * edge1.Z) - (deltaV1 * edge2.Z))
bitangent = Vector3D.CrossProduct(tangent, n)
'
p0.t.x = tangent.X
p0.t.y = tangent.Y
p0.t.z = tangent.Z
p0.bt.x = bitangent.X
p0.bt.y = bitangent.Y
p0.bt.z = bitangent.Z
'
p1.t.x = tangent.X
p1.t.y = tangent.Y
p1.t.z = tangent.Z
p1.bt.x = bitangent.X
p1.bt.y = bitangent.Y
p1.bt.z = bitangent.Z
'
p2.t.x = tangent.X
p2.t.y = tangent.Y
p2.t.z = tangent.Z
p2.bt.x = bitangent.X
p2.bt.y = bitangent.Y
p2.bt.z = bitangent.Z
End Sub
Here's a pic of the problem.. Norms are red.. Tangents Green... BiTangents.. blueish.
You will likely need to recalculate the tangent from cross-product of the normal and bitangent. This ensures that the normal, tangent and bitangent are orthagonal to one another.
In most implementations I've seen, the first calculation for a perpendicular axis to the normal is a placeholder as there are an infinite number of possible axes to choose from, and as such requires it to be recalculated to be orthagonal.
In your code you don't take dividend into the calculus, except for the case is 0.
Any two points in a triangle form a tangent or a bitangent. There are infinite possibilities. If you want to align T and B with some texture coordinates you need more maths.
With little effort you would have found this: How to calculate Tangent and Binormal?
Ok... I found 2 problems..
Dim edge1 = v1 - v2 should be Dim edge1 = v1 - v0
I looked in to what Ripi2 commented about.. I watched the video I got this from over again.. I messed a part where he changed the code.. He removed dividend and the if statement and replaced it with this line:
Dim f As Single = 1.0! / ((deltaU1 * deltaV2) - (deltaU2 * deltaV1))
Now it creates prefect Tangents and BiTangents.
I added normalization using these 2 lines.
tangent /= tangent.Length
bitangent /= bitangent.Length
I hope this helps anyone out there.
I'm hoping someone can clarify what I'm doing wrong here. I am trying to make a form with 2 labels - Celsius and Fahrenheit with 2 respective textbooks for the Value and 1 button - Convert to display the conversion of Celsius into Fahrenheit. The following Code I use keeps running me into Strict Options errors, 2 for Option Strict On disallows implicit conversions from 'Object' to 'String' and 2 for Option Strict On disallows implicit conversion from 'String' to 'Double'. I can't seem to find a way to please the strict options.
Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
Dim celsius As String
Dim answer As String
Dim fahrenheit As String
celsius = txtCelsius.Text
fahrenheit = txtFahrenheit.Text
If String.IsNullOrEmpty(txtFahrenheit.Text) Then
answer = celsius * 9 / 5 + 32
txtFahrenheit.Text = Int(answer)
End If
If String.IsNullOrEmpty(txtCelsius.Text) Then
answer = (fahrenheit - 32) * 5 / 9
txtCelsius.Text = Int(answer)
With Option Strict On
You need to do conversion yourself
I have edited your code, Try this
Dim celsius As String
Dim answer As String
Dim fahrenheit As String
celsius = txtCelsius.Text
fahrenheit = txtFahrenheit.Text
If String.IsNullOrEmpty(txtFahrenheit.Text) Then
answer = CStr(CDbl(celsius) * 9 / 5 + 32)
txtFahrenheit.Text = answer
End If
If String.IsNullOrEmpty(txtCelsius.Text) Then
answer = CStr((CDbl(fahrenheit) - 32) * 5 / 9)
txtCelsius.Text = answer
End If
You have many implicit conversions that can be fixed/made explicit:
You have implicit conversion in the celsius * 9 / 5 + 32and (fahrenheit - 32) * 5 / 9.
celcius and farhenheit are strings, but you're using it as a numbers.
You also have when you put the result into answer:
answer = celsius * 9 / 5 + 32
answer is a string but you're assigning the result of a calculation. It properly should be a double or similar datatype and not a string.
And then when you put Int(answer) into a textfield.
first answer is still a string, but Int() takes a number (double) if I remember right. Then you take that result and put automatically into a string:
txtCelsius.Text = Int(answer)
how to fix this error. . when user input whole number and multipy it there's no problem in this part of my code
but when user input a fractional number(negative or positive) like "1/2,-2/3 ETC" there is an error
the error is pointing in: new1 = CDbl(txta1.Text) * CDbl(txtb2.Text) * CDbl(txtc3.Text)
Error Message: Conversion from string "1/2" to type 'Double' is not valid.
view plaincopy to clipboardprint?
Dim new1, new2, new3, new4, new5, new6, add1, add2, minus1 As Double
new1 = CDbl(txta1.Text) * CDbl(txtb2.Text) * CDbl(txtc3.Text)
new2 = CDbl(txta2.Text) * CDbl(txtb3.Text) * CDbl(txtc1.Text)
new3 = CDbl(txta3.Text) * CDbl(txtb1.Text) * CDbl(txtc2.Text)
new4 = CDbl(txtc1.Text) * CDbl(txtb2.Text) * CDbl(txta3.Text)
new5 = CDbl(txtc2.Text) * CDbl(txtb3.Text) * CDbl(txta1.Text)
new6 = CDbl(txtc3.Text) * CDbl(txtb1.Text) * CDbl(txta2.Text)
Please activate Option strict ON, which will help you to prevent rookie mistakes like trying to use a textfield like txta1.text as a number:
http://support.microsoft.com/kb/311329
You have to try to parse a value from text to a number like so:
Dim number1 As Double
If Double.TryParse(txta1.Text, number1) Then
// do something
Else
Console.WriteLine("{0} is outside the range of a Double.", _
value)
// report error
End If
Otherwise it is very hard to debug your code.
As to fractions: I would be very hard to parse a fraction reliably by hand. I would somethink prebuild, like a mathematic expression library. Take a look: https://ncalc.codeplex.com/
"1/2" is a string and must be parsed and converted into a numeric by your code.
Here is an example.
For use with simple fractions (as shown in your example), decimals and negative numbers.
Note: You should add error checking. For brevity, I did not include any error checking.
Dim strInput As String = "-1/2"
Dim dblValue As Double = Nothing
If strInput.Contains("/") Then ' A fraction
Dim strArray() As String = strInput.Split(CChar("/"))
dblValue = CDbl(strArray(0)) / CDbl(strArray(1))
Else
dblValue = CDbl(strInput)
End If
Console.WriteLine(dblValue)
Hi I have the following snippet in java I need to convert to vb.net
float position =(value - startValue) / (middleValue - startValue);
Color4f result = new Color4f();
result.interpolate(startColor, middleColor, position);
return result;
Does anyone know how I can do the color4f.interpolate in vb.net?
Have found an article here for c# which use lambda operators but I have no idea what they mean and how to implement them in vb.net
Stackoverflow link to c# article (Color Interpolation Between 3 Colors in .NET)
And what is the best alternative for color4f in vb.net?
Thanks.
This function seems to work more or less:
Public Function interPolateColor(ByVal firstcolor As Color, ByVal secondcolor As Color, ByVal alpha As Double) As Color
Dim R As Double = ((1 - alpha) * Convert.ToInt32(firstcolor.R)) + (alpha * Convert.ToInt32(secondcolor.R))
Dim B As Double = ((1 - alpha) * Convert.ToInt32(firstcolor.B)) + (alpha * Convert.ToInt32(secondcolor.B))
Dim G As Double = ((1 - alpha) * Convert.ToInt32(firstcolor.G)) + (alpha * Convert.ToInt32(secondcolor.G))
Dim A As Byte = 255
Return Color.FromArgb(A, Convert.ToByte(R), Convert.ToByte(G), Convert.ToByte(B))
End Function