Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extended KotD #25, #26, and #27: INTEGER DIVISION, CINT, and CLNG
#11
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.
Reply
#12
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.
Reply
#13
I think my point got lost in translation  Rolleyes
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#14
(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
Reply
#15
(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]
Reply
#16
(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.
Reply
#17
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?
Reply
#18
(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
Reply
#19
@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!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#20
(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.
Reply




Users browsing this thread: RhoSigma, 2 Guest(s)