• Status: Solved
• Priority: Medium
• Security: Public
• Views: 136

Division by zero error

I get a "division by zero" error on this line:

M0_50 = MRest \ CDec(0.5)

M0_50 is declared as Integer and MRest as Double

Any idea ?
0
Junni
• 3
• 3
• 2
• +3
1 Solution

Senior .Net ConsultantCommented:
the \ is the integer division. You may want to use /.

or since you divide by .5, you may multiply by 2 to get the same result.
0

Author Commented:
the M0_50 should be rounded down, not up..they told me that the \ does that, and the / rounds up !??
0

Senior .Net ConsultantCommented:
\ is an integer division (you won't get any decimals with it)

/ is a real division.

maybe you should use m0_50 = clng(MRest)
0

Author Commented:
the M0_50 should be rounded down, not up..they told me that the \ does that, and the / rounds up !??
0

Author Commented:
what does the clng(MRest) ?
0

Senior .Net ConsultantCommented:
it converts MRest as a long (it removes the decimals).
0

Commented:
M0_50 = CInt(MRest / CDec(0.5))

0

Commented:
M0_50 = CInt(MRest / CDec(0.5)) ' use this if you want the value to round to the closes integer

M0_50 = Int(MRest / CDec(0.5)) ' use this if you want only the integer part of the division, decimal will be ignored.
0

Commented:
easier, and faster:

M0_50 = CInt(MRest * 2.)
0

Commented:
"They told me", sounds like homework.
You have to tell what you want the algoritm to do?

Emorau has best answer so far...
0

Commented:
M0_50 = CInt(MRest * 2.0 -0.5)

will round down

mlmcc
0

Featured Post

• 3
• 3
• 2
• +3
Tackle projects and never again get stuck behind a technical roadblock.