division by zero

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rintrah
    New Member
    • Apr 2007
    • 1

    division by zero

    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
  • Tig201
    New Member
    • Mar 2007
    • 103

    #2
    use \ as apposed to /
    \ = Division (float)
    / = Division (integer)

    Comment

    • Tig201
      New Member
      • Mar 2007
      • 103

      #3
      Sorry I got dyslexic yours is correct
      / = Division (float)
      \ = Division (integer)

      Comment

      • Tig201
        New Member
        • Mar 2007
        • 103

        #4
        you need do dim as double not long

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by Rintrah
          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)

          Comment

          Working...