Windows Vista Forums

Function Int fails ?

  1. #1


    massimoxr1 Guest

    Function Int fails ?

    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!

      My System SpecsSystem Spec

  2. #2


    mayayana Guest

    Re: Function Int fails ?

    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!


      My System SpecsSystem Spec

  3. #3


    Richard Mueller [MVP] Guest

    Re: Function Int fails ?


    <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
    --



      My System SpecsSystem Spec

  4. #4


    massimoxr1 Guest

    Re: Function Int fails ?

    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.

      My System SpecsSystem Spec

  5. #5


    ekkehard.horner Guest

    Re: Function Int fails ?

    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.

      My System SpecsSystem Spec

  6. #6


    Richard Mueller [MVP] Guest

    Re: Function Int fails ?


    <massimoxr1@xxxxxx> wrote in message
    news:4e4f6b9d-5adc-439a-8dac-3b4de1686c0b@xxxxxx

    > 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
    --



      My System SpecsSystem Spec

  7. #7


    Richard Mueller [MVP] Guest

    Re: Function Int fails ?


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

    >
    > <massimoxr1@xxxxxx> wrote in message
    > news:4e4f6b9d-5adc-439a-8dac-3b4de1686c0b@xxxxxx

    >> 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
    --



      My System SpecsSystem Spec

Function Int fails ?

Similar Threads
Thread Forum
function to delete a function
How would a function be written that given a name of a function, would delete that function? I tried with no success function rif { ri...
PowerShell
function not working dispite working outside the function block
I've writen the following function function getusername($a,$b,$c) { $firstletter = $a.substring(0,1) $lastletters = $b.substring(0,3)...
PowerShell
Function - XML
I've created this function to recieve an XML Document from one of our Web servers. The $Quote.Quote.BestBuyPrice returns the value of...
PowerShell
Complete PC Backup fails, restore fails differently!
Using Windows Vista Ultimate: I lost over a day as the 'Complete PC Backup' function advised me that I had a Device IO error. However, the...
Vista performance & maintenance
Contact List Fails to Function
I recently received a new Dell Computer with Vista. I successfully imported my contacts list from Outlook and can see the contact info. But, when I...
Vista mail
Windows Explorer fails and fails and fails...
My customer has had Vista for three months. He called me. Vista is in an endless loop. The Windows Explorer fails repeatedly and restarts. If...
Vista General
BUG: Redirecting function contents to a file truncates function lines at the width of the console
In my profile.ps1 file, I have various functions with line widths greater than 80 columns. My console is 80 columns. If I use the following: ...
PowerShell