QB64 Phoenix Edition
Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16)
+--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13)
+---- Forum: Keyword of the Day! (https://qb64phoenix.com/forum/forumdisplay.php?fid=49)
+---- Thread: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG (/showthread.php?tid=3661)

Pages: 1 2


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - Cobalt - 05-04-2025

Does QB64 behave like the original QB45 commands in these situations? If so then all is good. If not then it needs fixed to behave as QB45 would.


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - SMcNeill - 05-04-2025

From what I can tell we seem to mimic QB45 perfectly.  It's just we haven't really documented this behavior and explained it very well for people in the past.  The wiki is woefully lacking important details about exactly how people should expect CINT, CLNG, and _ROUND to work.


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - mdijkens - 05-04-2025

I think my point got lost in translation  Rolleyes


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - Steffan-68 - 05-04-2025

(05-04-2025, 04:44 PM)SMcNeill Wrote: From what I can tell we seem to mimic QB45 perfectly.  It's just we haven't really documented this behavior and explained it very well for people in the past.  The wiki is woefully lacking important details about exactly how people should expect CINT, CLNG, and _ROUND to work.
But you should also say that this is only the case with one decimal place.
from 0.55 the behavior is changed .

Code: (Select All)

For i = 1 To 10
    x = i + .55
    Print x, CInt(x)
Next

Code: (Select All)

1.55     2
2.55     3
3.55     4
4.55     5
5.55     6
6.55     7
7.55     8
8.55     9
9.55     10
10.55    11



RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - Cobalt - 05-04-2025

(05-04-2025, 04:44 PM)SMcNeill Wrote: From what I can tell we seem to mimic QB45 perfectly.  It's just we haven't really documented this behavior and explained it very well for people in the past.  The wiki is woefully lacking important details about exactly how people should expect CINT, CLNG, and _ROUND to work.

This is all QB45 help contains on CINT, do we really need more?

[Image: QB45Help.jpg]

I guess we could add this example to it.

[Image: QB45-Help2.jpg]


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - SMcNeill - 05-04-2025

(05-04-2025, 05:13 PM)Steffan-68 Wrote:
(05-04-2025, 04:44 PM)SMcNeill Wrote: From what I can tell we seem to mimic QB45 perfectly.  It's just we haven't really documented this behavior and explained it very well for people in the past.  The wiki is woefully lacking important details about exactly how people should expect CINT, CLNG, and _ROUND to work.
But you should also say that this is only the case with one decimal place.
from 0.55 the behavior is changed .

Code: (Select All)

For i = 1 To 10
    x = i + .55
    Print x, CInt(x)
Next

Code: (Select All)

1.55     2
2.55     3
3.55     4
4.55     5
5.55     6
6.55     7
7.55     8
8.55     9
9.55     10
10.55    11

That's why I mentioned this:

value < 0.5 rounds down.
value = 0.5 rounds to the nearest even integer.
value > 0.5 rounds up.

X.49999999999999 rounds down.
x.50000000000001 rounds up.


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - madscijr - 05-04-2025

We have cint and clng (and c__ for lots of types) in VBA & vbscript for casting values. 
I realize these are for rounding and may not be the exact equivalent, but they will come in handy. Thanks for sharing. 

Now, what is KotD?


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - SMcNeill - 05-04-2025

(05-04-2025, 08:47 PM)madscijr Wrote: We have cint and clng (and c__ for lots of types) in VBA & vbscript for casting values. 
I realize these are for rounding and may not be the exact equivalent, but they will come in handy. Thanks for sharing. 

Now, what is KotD?

Keyword of the Day.   The whole series of posts that are in this subforum.  Big Grin


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - PhilOfPerth - 05-04-2025

@SMcNeill:
This stream has already achieved one major goal: to raise awareness of the unexpected results we may get when using Integers

Your final short snippet sums up everything (at least, for me)!

value < 0.5 rounds down.
value = 0.5 rounds to the nearest even integer.
value > 0.5 rounds up.

X.49999999999999 rounds down.
x.50000000000001 rounds up.

If that's what happens in every case - pos or neg - then the world is sweet!


RE: Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG - SMcNeill - 05-04-2025

(05-04-2025, 10:38 PM)PhilOfPerth Wrote: @SMcNeill:
This stream has already achieved one major goal: to raise awareness of the unexpected results we may get when using Integers

Your final short snippet sums up everything (at least, for me)!

value < 0.5 rounds down.
value = 0.5 rounds to the nearest even integer.
value > 0.5 rounds up.

X.49999999999999 rounds down.
x.50000000000001 rounds up.

If that's what happens in every case - pos or neg - then the world is sweet!

Note that this is what we see in CINT, CLNG, _ROUND.  Other commands don't have this same behavior.

Any single, double, or float assigned to an integer obeys these same rules of rounding, as you can see from the below.  (Pay close attention to the .5 entries and see how one rounds up and the other rounds down, as they're both rounding to the closest even integer.

Code: (Select All)
Dim i As Integer
For x = 0 To 2 Step .1
    i = CInt(x)
    Print x, i,
    Select Case _ToStr$(i)
        Case Is < _ToStr$(x): Print "Round Down"
        Case Is = _ToStr$(x): Print "Equal"
        Case Is > _ToStr$(x): Print "Round up"
    End Select
Next

Also note that other rounding type commands don't obey this pattern or rule, such as INT (always rounds down), CEIL (always rounds up), FIX (truncates).   What I'm mentioning here is for CINT, CLNG, _ROUND, and direct assignment of floating point values to integer types.