Int() and CInt(), unclear sometimes

Forum for discussion about the documentation project.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Int() and CInt(), unclear sometimes

Post by Tourist Trap »

Hi,

I understand that INT and CINT dont round a double value the same way. One is rounding to the floor value, the other to nearest integer value. For instance :

Code: Select all

? int(6324.555320336759)
'  = 6324

? cint(6324.555320336759)
' = 6325
'not like INT
There is something more confusing now. When we use CINT to convert from string, CINT now will behave like INT and be also rounding to floor.

Code: Select all

? cint("6324.555320336759")
' = 6324
'somehow similar to INT
Other thing, the doc says at CINT page :
If the argument is a string expression, it is converted to numeric by using ValInt or ValLng, depending on the size of the result type.
But if I compare the returned types, acording to TYPEOF, CINT with the example will be INTEGER, while VALINT be LONG, and VALLNG, be LONGINT.

Code: Select all

? cint("6324.555320336759")
' = 6324  INTEGER
#print typeof(cint("6324.555320336759"))
? valint("6324.555320336759")
' = 6324 LONG
#print typeof(valint("6324.555320336759"))
? vallng("6324.555320336759")
' = 6324 LONGINT
#print typeof(vallng("6324.555320336759"))
So CINT(string) outputs some difference compared to VALINT/VALLNG(string) after this example at least.

So it'a little complicated. A simple warning would already help. My suggestion would be to add something like that at INT page:
INT page

(additional remark)
Becareful, INT and CINT are not equivalent, see CINT for more informations.
The CINT page is more complicated so I don't know. I'm still playing with different scenarios to see what it gives.

And last thing. I guess that DIM AS INTEGER I = VALUE, uses CINT to coerce the value. But I'm not 100% sure.


Thanks.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Int() and CInt(), unclear sometimes

Post by fxm »

If the argument is a string expression, it is converted to numeric by using ValInt or ValLng, depending on the size of the result type.
Perhaps this sentence is too synthetic but it is nevertheless exact and precise:
- If the result type (an Integer) is 32-bit sized, Cint is converted to numeric by using ValInt : Cast(Integer, Valint(string_expression))
- If the result type (an Integer) is 64-bit sized, Cint is converted to numeric by using ValLng : Cast(Integer, ValLng(string_expression))
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Int() and CInt(), unclear sometimes

Post by Tourist Trap »

fxm wrote:
If the argument is a string expression, it is converted to numeric by using ValInt or ValLng, depending on the size of the result type.
Perhaps this sentence is too synthetic but it is nevertheless exact and precise:
- If the result type (an Integer) is 32-bit sized, Cint is converted to numeric by using ValInt : Cast(Integer, Valint(string_expression))
- If the result type (an Integer) is 64-bit sized, Cint is converted to numeric by using ValLng : Cast(Integer, ValLng(string_expression))
Ok now. Indeed "numeric" was too synthetic. Numeric can be many things. However I see now what it is about. If the integer is too big, it uses VALLNG, which is a good thing of course.
Post Reply