Function Int fails ?

M

massimoxr1

Hi to all,
this is the code in my file test.vbs :

Dim Result

Result = (2170.12 / 9.08) ' is 239
msgbox("(2170.12 / 9.08) =**" & Result & "**")
msgbox("Int(Result) =**" & Int(Result) & "**") ' Ok

Result = (2179.2 / 9.08) ' is 240
msgbox("(2179.2 / 9.08) =**" & Result & "**")
msgbox("Int(Result) =**" & Int(Result) & "**") ' Error! Why 239 ?

Result = (2188.28 / 9.08) ' is 241
msgbox("(2188.28 / 9.08) =**" & Result & "**")
msgbox("Int(Result) =**" & Int(Result) & "**") ' Ok

Any ideas?
Thanks!

System One

M

mayayana

Int rounds the number. If you switch the
slashes to backslashes you'll get yet another
result. That's what's supposed to happen. An
integer is a whole number.

> this is the code in my file test.vbs :
>
> Dim Result
>
> Result = (2170.12 / 9.08) ' is 239
> msgbox("(2170.12 / 9.08) =**" & Result & "**")
> msgbox("Int(Result) =**" & Int(Result) & "**") ' Ok
>
> Result = (2179.2 / 9.08) ' is 240
> msgbox("(2179.2 / 9.08) =**" & Result & "**")
> msgbox("Int(Result) =**" & Int(Result) & "**") ' Error! Why 239 ?
>
> Result = (2188.28 / 9.08) ' is 241
> msgbox("(2188.28 / 9.08) =**" & Result & "**")
> msgbox("Int(Result) =**" & Int(Result) & "**") ' Ok
>
>
> Any ideas?
> Thanks!

System One

R

Richard Mueller [MVP]

<massimoxr1@xxxxxx> wrote in message
news:ac348705-c661-43f1-89ce-96f49507f88a@xxxxxx

> Hi to all,
> this is the code in my file test.vbs :
>
> Dim Result
>
> Result = (2170.12 / 9.08) ' is 239
> msgbox("(2170.12 / 9.08) =**" & Result & "**")
> msgbox("Int(Result) =**" & Int(Result) & "**") ' Ok
>
> Result = (2179.2 / 9.08) ' is 240
> msgbox("(2179.2 / 9.08) =**" & Result & "**")
> msgbox("Int(Result) =**" & Int(Result) & "**") ' Error! Why 239 ?
>
> Result = (2188.28 / 9.08) ' is 241
> msgbox("(2188.28 / 9.08) =**" & Result & "**")
> msgbox("Int(Result) =**" & Int(Result) & "**") ' Ok
>
>
> Any ideas?
> Thanks!
The behaviour is expected. All math internally is in binary and the results
is often inexact. Internally, the result is closer to 239.9999999999999.
Note that Int((2179.2 / 9.08) + .0000000000001) is 240.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

System One

M

massimoxr1

ok, and what about the function CInt ?

Result = (2179.2 / 9.08) ' is 240
msgbox("(2179.2 / 9.08) =**" & Result & "**")
msgbox("CInt(Result) =**" & CInt(Result) & "**") 'returs 240

CInt seems to return the right value.

System One

E

ekkehard.horner

massimoxr1@xxxxxx schrieb:

> ok, and what about the function CInt ?
>
> Result = (2179.2 / 9.08) ' is 240
> msgbox("(2179.2 / 9.08) =**" & Result & "**")
> msgbox("CInt(Result) =**" & CInt(Result) & "**") 'returs 240
>
>
> CInt seems to return the right value.
Straight from the Docs (CInt Function):

CInt differs from the Fix and Int functions, which truncate,
rather than round, the fractional part of a number. When the
fractional part is exactly 0.5, the CInt function always rounds
it to the nearest even number. For example, 0.5 rounds to 0,
and 1.5 rounds to 2.

System One

R

Richard Mueller [MVP]

<massimoxr1@xxxxxx> wrote in message

> ok, and what about the function CInt ?
>
> Result = (2179.2 / 9.08) ' is 240
> msgbox("(2179.2 / 9.08) =**" & Result & "**")
> msgbox("CInt(Result) =**" & CInt(Result) & "**") 'returs 240
>
>
> CInt seems to return the right value.
CInt rounds, while Int (and also Fix) does not.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

System One

R

Richard Mueller [MVP]

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
message news:uX0YMH\$HJHA.4936@xxxxxx

>
> <massimoxr1@xxxxxx> wrote in message

>> ok, and what about the function CInt ?
>>
>> Result = (2179.2 / 9.08) ' is 240
>> msgbox("(2179.2 / 9.08) =**" & Result & "**")
>> msgbox("CInt(Result) =**" & CInt(Result) & "**") 'returs 240
>>
>>
>> CInt seems to return the right value.
>
> CInt rounds, while Int (and also Fix) does not.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
Also, CInt can only handle values between -32,768.5 and 32,767.5 (the result
must be datatype integer). The Int and Fix functions can handle any numeric
values.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--