By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,801 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

division by zero

P: 1
Hello everyone this is my first post.

My problem is dividing a variable with another variable when the last variable is a decimal. The program returns "division with zero" even though its not zero at all.

Public firstvariable As Long
Public secondvariable As Long
laks = 100
discolaks = 0.3
willcausethebug = firstvariable / secondvariable
Apr 25 '07 #1
Share this Question
Share on Google+
4 Replies


Tig201
100+
P: 103
use \ as apposed to /
\ = Division (float)
/ = Division (integer)
Apr 25 '07 #2

Tig201
100+
P: 103
Sorry I got dyslexic yours is correct
/ = Division (float)
\ = Division (integer)
Apr 25 '07 #3

Tig201
100+
P: 103
you need do dim as double not long
Apr 25 '07 #4

Expert 5K+
P: 8,434
Hello everyone this is my first post.

My problem is dividing a variable with another variable when the last variable is a decimal. The program returns "division with zero" even though its not zero at all.

Public firstvariable As Long
Public secondvariable As Long
laks = 100
discolaks = 0.3
willcausethebug = firstvariable / secondvariable
Ok, at first glance there are two bugs here.
  1. The variables you used are not the ones you defined.
    Since secondvariable was never assigned a value, of course it is zero.
    You should always keep the "require explicit variable declaration" option turned on to prevent this sort of bug. If it's off, whenever you refer to a variable VB will just go ahead and create it for you. Usually meaning that if you use the wrong name it will take ages to work out what's wrong. If it's on, this code will simply refuse to compile, with the message "variable not defined".
  2. Assuming you meant to use secondvariable, the Long data type only holds whole numbers. So the value would have been rounded to the nearest whole number (zero) anyway.
    As Tig201 suggested, use a data type which can handle decimal places.
    I'd recommend Single rather than Double, unless you need a lot of decimal places. the documentation will tell you the limitations of the various data types. It's generally best to use the smallest one that will work, though there are exceptions (Eg. it's generally better to use Long than Integer)
Apr 25 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.