Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integer Division.

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
0
0
GB
I have a small question, can anyone explain :

int(29.5/15) = 1

(29.5\15) = 2

thank in advance ...
 
int(29.5/15) - Normal divison, then truncated
The bracketed part is normal division. The number is then truncated, not rounded to an integer value.

(29.5\5) - Integer division
The order of events for this is:
1. Inputs rounded to integers
2. Division carried out
3. Result truncated to integer

(truncated means everything after the decimal point is cut-off rather than rounded-up)

Hope this helped
 
Technically int(x) returns the "highest integer which does not algebraically exceed x". So:

For positive numbers int(29.5) = 29

For negative numbers int(-29.5) = -30
 
Unfortunatley, VB has a variety of ways of calculating integer results from floating point operations. I say 'unfortunate' because, as you have discovered, they can give different results. And there are differences between the implicit conversions and the explicit conversions. The following minor example illustrates some of this further:
[tt]
Private Sub Command1_Click()

' Explicit conversions
Print Int(29.5 / 15)
Print Int(29.4 / 15)
Print
Print CInt(29.5 / 15)
Print CInt(29.4 / 15)
Print
' Implicit conversions
Print (29.5 \ 15)
Print (29.4 \ 15)
End Sub



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top