Author |
Message |
rjavaid
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Sun Feb 07, 2010 2:04 am Post subject: RE:Problem: Value is too large! |
|
|
VB does support long and double if needed |
|
|
|
|
![](images/spacer.gif) |
faethor
![](http://compsci.ca/v3/uploads/user_avatars/5014376814b622a109c1f7.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
william 01
![](http://compsci.ca/v3/uploads/user_avatars/14952085584d66bafdeb2f9.jpg)
|
Posted: Thu Jan 06, 2011 9:39 pm Post subject: Re: Problem: Value is too large! |
|
|
what vb are you using? ![Question Question](http://compsci.ca/v3/images/smiles/icon_question.gif) |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
|