The VB6's INT function has a strange behavior

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Loche
    New Member
    • Oct 2014
    • 4

    The VB6's INT function has a strange behavior

    Dim iContatore As Integer
    Dim nCifre As Integer

    iContatore = 10
    nCifre = Int(Log(iContat ore) / Log(10))
    Debug.Print nCifre

    It gives me '0' instead of '1'. WHY??
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    What's your log function look like?

    Comment

    • Loche
      New Member
      • Oct 2014
      • 4

      #3
      Log(iContatore) gives me 1 and also Log(10). So if I write
      nCifre=Log(iCon tatore)/Log(10) I get 1!

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That's not right. The log function uses e as the base. So Log(10) returns 2.3025850929940 456840179914546 844.... So what you have is a float divided by a float. The problem with float division is that it is not precise. The answer you get when dividing it will be somewhere between slightly less than 1 and slightly more than one. In your case, it is returning slightly less than one so when that is converted to an integer, it comes out to zero.

        Comment

        • Loche
          New Member
          • Oct 2014
          • 4

          #5
          Sorry Rabbit, you are right: vb6's Log function uses e base!
          Thank you for the answer, but is there a way to get the right result?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Round the result to the decimal precision you need for other calculations you might need to do.

            Comment

            • Loche
              New Member
              • Oct 2014
              • 4

              #7
              Thank you very much.

              Comment

              Working...