Computer Science Canada Problem: Value is too large! |
Author: | rjavaid [ Sat Feb 06, 2010 11:19 pm ] |
Post subject: | Problem: Value is too large! |
Hi guys, seemed easy enough when I first started to program it. It's been a little more than an hour now, trying to figure out how to correct these few lines of code. The problem is that the Energy being calculated ---> Energy = Mass * (C ^ 2) ---> is coming out to a very high value, and apparently VB doesn't like that. Here's my code, and help would be greatly appreciated! By the way, I'm using VB Express ------------------------------------------------------------------------------------------------------------------------------------- Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Mass As Integer Dim Energy As Integer Dim Bulbs As Integer Dim C As Double 'C is the speed of light C = 300000000.0 ^ 2 Mass = TextBox1.Text ' User inputs a Mass in kg Energy = Mass * (C ^ 2) ' Calculates the amount of Energy in Joules TextBox2.Text = Energy Bulbs = Energy / 360000 ' Calculates the number of 100W light bulbs that can be powered TextBox3.Text = Bulbs End Sub End Class |
Author: | chrisbrown [ Sun Feb 07, 2010 12:09 am ] |
Post subject: | RE:Problem: Value is too large! |
First, you are squaring C twice. That won't fix your error though. The value of C^2 is more than an integer can handle, so a variable that is assigned to some multiple of C^2 needs to be able to handle numbers beyond the integer range. |
Author: | Insectoid [ Sun Feb 07, 2010 12:44 am ] |
Post subject: | RE:Problem: Value is too large! |
I don't know if VB has a 'Long' data type, but that's what Java uses for huge numbers. You could also do clever math to shrink the numbers (divide C by 10000 or more) and then multiply the final answer by 10000. Or whatever would make the answer correct. |
Author: | syntax_error [ Sun Feb 07, 2010 2:04 am ] |
Post subject: | RE:Problem: Value is too large! |
VB does support long and double if needed |
Author: | faethor [ Sun Feb 07, 2010 10:24 am ] |
Post subject: | Re: Problem: Value is too large! |
Here, you are doing a calculation with Joules. To produce a smaller number, calculate it in Electron Volts. The conversion factor is 1J = 1.602?10^-19. The reason why you do c^2 is to make it a relativistic unit. |
Author: | william 01 [ Thu Jan 06, 2011 9:39 pm ] |
Post subject: | Re: Problem: Value is too large! |
what vb are you using? ![]() |
Author: | Insectoid [ Thu Jan 06, 2011 9:54 pm ] |
Post subject: | RE:Problem: Value is too large! |
This thread is nearly a year old. OP hasn't posted more than once. Go away. |