-
Jun 26th, 2014, 02:18 PM
#1
Thread Starter
Junior Member
How to add up a list of values from a calculation w/0 having them displayed anywhere
I posed a question previously about adding up a list of values from a textbox. I was able to get it from someone's code, but after thinking about it I'd rather not have any kind of box displaying the list of solutions to the original calculation. How would I go about this?
Any and all help is greatly appreciated.
-
Jun 26th, 2014, 02:22 PM
#2
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
where do you want to put the result?
-
Jun 26th, 2014, 02:36 PM
#3
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Don't use a text box... use a variable.
-tg
-
Jun 26th, 2014, 02:37 PM
#4
Thread Starter
Junior Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Originally Posted by vbfbryce
where do you want to put the result?
Oops. Forgot to mention that. I'd like the result displayed in a textbox, or something similar. Not a popup.
-
Jun 26th, 2014, 02:39 PM
#5
Thread Starter
Junior Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Originally Posted by techgnome
Don't use a text box... use a variable.
-tg
I probably should have also mentioned that I have minimal VB knowledge.
-
Jun 26th, 2014, 03:21 PM
#6
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Instead of storing the values in a textbox store them in a list (of T) where T is the type of the values you are working with.
If you are using a fairly recent version of VS try something like, ....
Code:
' list to hold values
Private values As New List(Of Decimal)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' add some values to list
values.Add(1.23D)
values.Add(4.56D)
' show sum in textbox
CTODprime.Text = values.Sum.ToString
End Sub
-
Jun 26th, 2014, 04:19 PM
#7
Thread Starter
Junior Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Originally Posted by Edgemeal
Instead of storing the values in a textbox store them in a list (of T) where T is the type of the values you are working with.
If you are using a fairly recent version of VS try something like, ....
Code:
' list to hold values
Private values As New List(Of Decimal)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' add some values to list
values.Add(1.23D)
values.Add(4.56D)
' show sum in textbox
CTODprime.Text = values.Sum.ToString
End Sub
If the values are coming from a calculation (360 values) would the code be: ?
-
Jun 26th, 2014, 04:43 PM
#8
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Originally Posted by EricB78
If the values are coming from a calculation (360 values) would the code be: ?
If the values are from a string, for example text from a textbox then I'd probably convert them to a value type (same as the list is declared - decimal or whatever) and then add them to the list, if you use tryparse on the text you can then avoid empty, non-numerical entries by checking if result is zero. There may be better ways to do what you need, see if this helps... if not explain in detail what you have and need help with.
Code:
Private values As New List(Of Decimal)
Private Sub BtnAddValue_Click(sender As System.Object, e As System.EventArgs) Handles BtnAddValue.Click
Dim dec As Decimal
' convert entered text (from TextBox1) to Decimal type
Decimal.TryParse(TextBox1.Text, dec)
If dec = 0 Then ' do not accept zero or invalid entries.
MessageBox.Show("zero or invalid enrty")
Else
' add value to list
values.Add(dec)
End If
'optional: show current sum in textbox
CTODprime.Text = values.Sum.ToString
End Sub
Last edited by Edgemeal; Jun 26th, 2014 at 04:52 PM.
Reason: typo
-
Jun 26th, 2014, 04:45 PM
#9
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Not quite. Values will only accept type Decimal, as it is currently defined. You seemed to be using trig functions (based on one variable mentioned in the other thread), in which case I think Double would be a better type. Decimal is very precise, whereas Double has a trivial imprecision in the last place or two, but trig functions may all return Double natively, and Double is a bit faster. Therefore, in most cases, math should be done using Doubles. The big exception is that anything with money should be done using Decimal. For display purposes, it doesn't really matter, as you can format either one however you prefer.
So, if you have your equation in a method, for example:
Dim someReturnValue As Double = SomeCalculation(someArguments, someMoreArguments)
then you could add someReturnValue to values:
values.Add(someReturnValue)
or you could even make it a single line:
values.Add(SomeCalculation(someArguments, someMoreArguments))
My usual boring signature: Nothing
-
Oct 17th, 2014, 03:33 PM
#10
Thread Starter
Junior Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Sorry for the people that tried to help and my lack of response. I had hernia surgery and stepped away from working on this for a while. However, I'm back and trying to get this to work. So, here is the code that I have in VB for the desired list of values and then the summation of the values after:
Code:
For θi As Decimal = -180 To 180 Step 1
rp = (1.828 / (2 * Math.PI * (σ) ^ 2)) * (K1Local ^ 2 * (Math.Cos(θi * Math.PI / 180 / 2) * (3 * Math.Sin(θi * Math.PI / 180 / 2) ^ 2 + (1 - 2 * nu) ^ 2)) + K2Local ^ 2 * (3 + Math.Sin(θi * Math.PI / 180 / 2) ^ 2 * ((1 - 2 * nu) ^ 2 - 9 * Math.Cos(θi * Math.PI / 180 / 2) ^ 2)) + K1Local * K2Local * Math.Sin(θi * Math.PI / 180) * (3 * Math.Cos(θi * Math.PI / 180) - (1 - 2 * nu) ^ 2))
τ = K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2)))
B = τ / Math.Abs(τ) * 2 * rp * (σ / 2) * Math.PI * (1 - nu ^ 2) / elasticmodulus
der1 = (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi + 1) * Math.PI / 180 / 2)) ^ 2 * Math.Sin((θi + 1) * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi + 1) * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin((θi + 1) * Math.PI / 180 / 2) ^ 2)))) - (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2))))
der0 = (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2)))) - (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi - 1) * Math.PI / 180 / 2)) ^ 2 * Math.Sin((θi - 1) * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi - 1) * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin((θi - 1) * Math.PI / 180 / 2) ^ 2))))
cross = (1 - (Math.Abs(der1 * der0) / (der1 * der0))) / 2
BiSinθ = Math.Sin(θi * Math.PI / 180) * B * cross
BiSintheta.AppendText(FormatNumber(BiSinθ, 15) & vbCrLf)
BiCosθ = Math.Cos(θi * Math.PI / 180) * B * cross
BiCostheta.AppendText(FormatNumber(BiCosθ, 15) & vbCrLf)
Dim decTotal As Decimal = 0D
Dim dec As Decimal
For Each item In BiSintheta.Lines
Decimal.TryParse(item, dec)
decTotal += dec
Next
CTODprime.Text = decTotal.ToString
Dim decTotal2 As Decimal = 0D
Dim dec2 As Decimal
For Each item2 In BiCostheta.Lines
Decimal.TryParse(item2, dec2)
decTotal2 += dec2
Next
CTSDprime.Text = decTotal2.ToString
Next
So, the two lists are the BiSin(theta) and BiCos(theta) and they are being used to calculate CTODprime and CTSDprime. Ideally, I would like to not have the two lists and just have the calculations for CTODprime and CTSDprime done from the other equations.
-
Oct 27th, 2014, 02:06 PM
#11
Thread Starter
Junior Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
-
Oct 27th, 2014, 03:10 PM
#12
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
I looked over the earlier posts, and I'm not sure what the question is. It looks like you will be calculating a lot of BiSin(theta) and BiCos(theta), but I'm not sure what to make of what you are doing with it. It looks like you are taking a perfectly good Double and appending each one to a textbox, then taking the text from the textbox, converting to Decimal (I still think that Double would be better), and summing them. However, you are doing that sum in an inner loop inside an outer loop. Is that right?
In any case, rather than appending to a textbox, make a List(of Double) for the BiSin(theta) and BiCos(theta). You can use the .Add method to add the new values to the appropriate list, and I believe that the List has a .Sum extension method such that you can get rid of that inner loop entirely. Oddly, doing the loop may be faster than using List.Sum, but you'd never see the difference, so you might as well use List.Sum. What has me a bit disturbed is that the way you have it now, the first iteration of the outer loop would put one BiSin(theta) into one textbox and one BiCos(theta) into a different textbox. The sum of that one line would be the first value. In the next iteration, you add a second value to each textbox, and the sum is now the sum of the two values, and so on. If that is really what you want to be doing, then you don't need the Lists, either, because you can just have a single variable for BiSin and BiCos, and as you calculate each new value, you'd add it to the appropriate variable.
My usual boring signature: Nothing
-
Oct 27th, 2014, 03:47 PM
#13
Hyperactive Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
For example, using your code and the suggestions of A) changing the numberical data to doubles and B) using a List(Of Double), here is what the new loop would look like:
Code:
Dim BiSintheta As New List(Of Double)
Dim BiCostheta As New List(Of Double)
For θi As Double = -180.0 To 180.0 Step 1.0
rp = (1.828 / (2 * Math.PI * (σ) ^ 2)) * (K1Local ^ 2 * (Math.Cos(θi * Math.PI / 180 / 2) * (3 * Math.Sin(θi * Math.PI / 180 / 2) ^ 2 + (1 - 2 * nu) ^ 2)) + K2Local ^ 2 * (3 + Math.Sin(θi * Math.PI / 180 / 2) ^ 2 * ((1 - 2 * nu) ^ 2 - 9 * Math.Cos(θi * Math.PI / 180 / 2) ^ 2)) + K1Local * K2Local * Math.Sin(θi * Math.PI / 180) * (3 * Math.Cos(θi * Math.PI / 180) - (1 - 2 * nu) ^ 2))
τ = K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2)))
B = τ / Math.Abs(τ) * 2 * rp * (σ / 2) * Math.PI * (1 - nu ^ 2) / elasticmodulus
der1 = (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi + 1) * Math.PI / 180 / 2)) ^ 2 * Math.Sin((θi + 1) * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi + 1) * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin((θi + 1) * Math.PI / 180 / 2) ^ 2)))) - (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2))))
der0 = (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2)))) - (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi - 1) * Math.PI / 180 / 2)) ^ 2 * Math.Sin((θi - 1) * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi - 1) * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin((θi - 1) * Math.PI / 180 / 2) ^ 2))))
cross = (1 - (Math.Abs(der1 * der0) / (der1 * der0))) / 2
BiSinθ = Math.Sin(θi * Math.PI / 180) * B * cross
BiSintheta.Add(BiSinθ)
BiCosθ = Math.Cos(θi * Math.PI / 180) * B * cross
BiCostheta.Add(BiCosθ)
Next
Me.CTODprime.Text = FormatNumber(BiSintheta.Sum(), 15)
Me.CTSDprime.Text = FormatNumber(BiCostheta.Sum(), 15)
Notice that I changed BiSintheta and BiCostheta to List(Of Double) instead of ListBoxes so that it A) doesn't display anything on the screen until the final lines when it sets the TextBoxes and B) to hold the individual calculations as it's going through the main loop. Also, I don't actually do the summation until after the loop has calculated all of the numbers in the loop and added those values to the two List(Of Double).
-
Oct 27th, 2014, 04:38 PM
#14
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
From your code, it looks like you're trying to store all the values in a circle. If that's so then try something like this:
Code:
Private Function GetEllipsePath(ByVal location As PointF, ByVal size As SizeF) As PointF()
Dim ellipsePath(100) As PointF
For i As Integer = 0 To 100
Dim angle As Double = CDbl(i / 100 * Math.PI * 2)
ellipsePath(0) = New PointF(CSng(location.X + CDbl(Math.Cos(angle)) * size.Width), CSng(location.Y + Math.Sin(angle) * size.Height))
Next
Return ellipsePath
End Function
It returns an array of PointFs.
-
Oct 28th, 2014, 11:15 AM
#15
Thread Starter
Junior Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Originally Posted by Shaggy Hiker
I looked over the earlier posts, and I'm not sure what the question is. It looks like you will be calculating a lot of BiSin(theta) and BiCos(theta), but I'm not sure what to make of what you are doing with it. It looks like you are taking a perfectly good Double and appending each one to a textbox, then taking the text from the textbox, converting to Decimal (I still think that Double would be better), and summing them. However, you are doing that sum in an inner loop inside an outer loop. Is that right?
In any case, rather than appending to a textbox, make a List(of Double) for the BiSin(theta) and BiCos(theta). You can use the .Add method to add the new values to the appropriate list, and I believe that the List has a .Sum extension method such that you can get rid of that inner loop entirely. Oddly, doing the loop may be faster than using List.Sum, but you'd never see the difference, so you might as well use List.Sum. What has me a bit disturbed is that the way you have it now, the first iteration of the outer loop would put one BiSin(theta) into one textbox and one BiCos(theta) into a different textbox. The sum of that one line would be the first value. In the next iteration, you add a second value to each textbox, and the sum is now the sum of the two values, and so on. If that is really what you want to be doing, then you don't need the Lists, either, because you can just have a single variable for BiSin and BiCos, and as you calculate each new value, you'd add it to the appropriate variable.
Well, this is for a thesis project for grad school. I have very limited VB knowledge, but this is the only free program I could get to do the necessary work. Honestly, I don't really know the difference between decimal and double.
The BiSin(theta) and BiCos(theta) need to stay seperate because one is used to calculate the CTODprime and the other is used to calculate the CTSDprime.
Thank you all for the input and help. I'm going to try to implement the tips/ideas you guys have given me so far.
-
Oct 28th, 2014, 11:31 AM
#16
Thread Starter
Junior Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Originally Posted by Pyth007
For example, using your code and the suggestions of A) changing the numberical data to doubles and B) using a List(Of Double), here is what the new loop would look like:
Code:
Dim BiSintheta As New List(Of Double)
Dim BiCostheta As New List(Of Double)
For θi As Double = -180.0 To 180.0 Step 1.0
rp = (1.828 / (2 * Math.PI * (σ) ^ 2)) * (K1Local ^ 2 * (Math.Cos(θi * Math.PI / 180 / 2) * (3 * Math.Sin(θi * Math.PI / 180 / 2) ^ 2 + (1 - 2 * nu) ^ 2)) + K2Local ^ 2 * (3 + Math.Sin(θi * Math.PI / 180 / 2) ^ 2 * ((1 - 2 * nu) ^ 2 - 9 * Math.Cos(θi * Math.PI / 180 / 2) ^ 2)) + K1Local * K2Local * Math.Sin(θi * Math.PI / 180) * (3 * Math.Cos(θi * Math.PI / 180) - (1 - 2 * nu) ^ 2))
τ = K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2)))
B = τ / Math.Abs(τ) * 2 * rp * (σ / 2) * Math.PI * (1 - nu ^ 2) / elasticmodulus
der1 = (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi + 1) * Math.PI / 180 / 2)) ^ 2 * Math.Sin((θi + 1) * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi + 1) * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin((θi + 1) * Math.PI / 180 / 2) ^ 2)))) - (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2))))
der0 = (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2)) ^ 2 * Math.Sin(θi * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos(θi * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin(θi * Math.PI / 180 / 2) ^ 2)))) - (K1Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi - 1) * Math.PI / 180 / 2)) ^ 2 * Math.Sin((θi - 1) * Math.PI / 180 / 2) + K2Prime / ((2 * Math.PI * r) ^ 0.5) * (Math.Cos((θi - 1) * Math.PI / 180 / 2) * (1 - 3 * (Math.Sin((θi - 1) * Math.PI / 180 / 2) ^ 2))))
cross = (1 - (Math.Abs(der1 * der0) / (der1 * der0))) / 2
BiSinθ = Math.Sin(θi * Math.PI / 180) * B * cross
BiSintheta.Add(BiSinθ)
BiCosθ = Math.Cos(θi * Math.PI / 180) * B * cross
BiCostheta.Add(BiCosθ)
Next
Me.CTODprime.Text = FormatNumber(BiSintheta.Sum(), 15)
Me.CTSDprime.Text = FormatNumber(BiCostheta.Sum(), 15)
Notice that I changed BiSintheta and BiCostheta to List(Of Double) instead of ListBoxes so that it A) doesn't display anything on the screen until the final lines when it sets the TextBoxes and B) to hold the individual calculations as it's going through the main loop. Also, I don't actually do the summation until after the loop has calculated all of the numbers in the loop and added those values to the two List(Of Double).
This worked perfectly. Thank you.
-
Oct 28th, 2014, 04:17 PM
#17
Hyperactive Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Originally Posted by EricB78
Well, this is for a thesis project for grad school. I have very limited VB knowledge, but this is the only free program I could get to do the necessary work. Honestly, I don't really know the difference between decimal and double.
Basically computers are binary machines so everything that they do has to be in a binary format. So when you write 1+3, the computer first converts this into binary, does the addition, and then converts the answer back into a base-10 format. However there are some numbers that cannot be translated into base-2 accurately. For example, 0.3 cannot be expressed in binary; rather it tends to be something like 0.30000000000000004. This is known as the rounding error.
The rounding error is the "bad" part about floating points: they are not as accurate as other data-types. However the "good" part is that the decimal point "floats" in the number depending upon the amount of precision required. As such floating point numbers have a large range: for a Double (a double precision floating point number), the numbers can range between 10-308 to 10308, meaning it can be used to represent the distance between two galaxies (a light-year is about 1015 meters) down to the distance between two atoms (bond-lengths are usually a few hundred picometers, or 10-12 meters) using meters as the base unit of measurement. Because of this, floating point can be "moved" to such a position as to lessen the effects of the rounding error as long as the scale of numbers is relatively constant -- measuring the distance between galaxies in picometers would probably run into errors since the scale is too broad for the unit of measurement, whereas working with molecular geometries in picometers would have very little error evident as the errors would be lessened to insignificance. Basically a floating point is represented as having some bits for the significand and other bits for the exponent (sort of like scientific notation). (Here is an excellent article that isn't too technical describing floating points and their IEEE standard: http://en.wikipedia.org/wiki/Floating_point)
On the other hand, if you don't need to have such a large range of possible numbers to represent, but are more concerned with accuracy, then a fixed-point data-type like Decimal is preferred. This is because fixed-point types use a set number of bits to represent the whole number and a different set to represent the fractional part. As such, both the whole parts and the fractional parts can be represented as integral values which can accurately be converted to a binary number (20 = 1 so any integral decimal value can be converted to binary using binary addition). The trade-off is that A) it takes longer to perform fixed-point calculations and B) the fixed number of positions after the decimal point decreases the range of significant digits that can be represented (it's range can go as small as 10-28 to as large as 1028).
-
Oct 28th, 2014, 05:25 PM
#18
Thread Starter
Junior Member
Re: How to add up a list of values from a calculation w/0 having them displayed anywh
Originally Posted by Pyth007
Basically computers are binary machines so everything that they do has to be in a binary format. So when you write 1+3, the computer first converts this into binary, does the addition, and then converts the answer back into a base-10 format. However there are some numbers that cannot be translated into base-2 accurately. For example, 0.3 cannot be expressed in binary; rather it tends to be something like 0.30000000000000004. This is known as the rounding error.
The rounding error is the "bad" part about floating points: they are not as accurate as other data-types. However the "good" part is that the decimal point "floats" in the number depending upon the amount of precision required. As such floating point numbers have a large range: for a Double (a double precision floating point number), the numbers can range between 10 -308 to 10 308, meaning it can be used to represent the distance between two galaxies (a light-year is about 10 15 meters) down to the distance between two atoms (bond-lengths are usually a few hundred picometers, or 10 -12 meters) using meters as the base unit of measurement. Because of this, floating point can be "moved" to such a position as to lessen the effects of the rounding error as long as the scale of numbers is relatively constant -- measuring the distance between galaxies in picometers would probably run into errors since the scale is too broad for the unit of measurement, whereas working with molecular geometries in picometers would have very little error evident as the errors would be lessened to insignificance. Basically a floating point is represented as having some bits for the significand and other bits for the exponent (sort of like scientific notation). (Here is an excellent article that isn't too technical describing floating points and their IEEE standard: http://en.wikipedia.org/wiki/Floating_point)
On the other hand, if you don't need to have such a large range of possible numbers to represent, but are more concerned with accuracy, then a fixed-point data-type like Decimal is preferred. This is because fixed-point types use a set number of bits to represent the whole number and a different set to represent the fractional part. As such, both the whole parts and the fractional parts can be represented as integral values which can accurately be converted to a binary number (2 0 = 1 so any integral decimal value can be converted to binary using binary addition). The trade-off is that A) it takes longer to perform fixed-point calculations and B) the fixed number of positions after the decimal point decreases the range of significant digits that can be represented (it's range can go as small as 10 -28 to as large as 10 28).
So let's say that I'm programming this to have user inputs (which will most likely be in integers or at worst one or two decimal places), should I have those as decimal or double? Calculated outputs all in double?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|