Results 1 to 15 of 15

Thread: Why is this false?

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2001
    Posts
    51

    Why is this false?

    If I use this code it comes out false. Why?

    (0.0087 * 100) = 0.87

    If I encapsulate each side of the equation with Val() it comes out true.

    Is it something to do with a floating decimal point?

    Help

  2. #2

    Thread Starter
    Member
    Join Date
    Oct 2001
    Posts
    51

    oh and ..

    For the record the following code comes out true,
    (0.0077 * 100) = 0.77

    Why is this?

  3. #3
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I guess it might be because the 0.87 is being represented as a single, and the multiplication returns a double. As a matter of fact, if you do this, you get true:
    VB Code:
    1. CSng(0.0087 * 100) = 0.87
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  4. #4
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Actually... the multiplication returns a double, because 0.0087 is saved in a Double variable.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    As well... you'll get True with this code:

    VB Code:
    1. CSng(0.0087) * 100 = 0.87
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2001
    Posts
    51

    Well what about this?

    Actually if you try this, you can avoid all confusion about data types and still get a false.

    Private Sub Command1_Click()
    Dim dblNumber1 As Double
    Dim dblNumber2 As Double

    dblNumber1 = 0.87
    dblNumber2 = 0.0087 * 100

    Debug.Print "========================================"
    Debug.Print "dblNumber1 = " & dblNumber1
    Debug.Print "dblNumber2 = " & dblNumber2

    Debug.Print "dblNumber1 = dblNumber2: " & (dblNumber1 = dblNumber2)


    End Sub

    It is pretty weird

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Maybe the different mantissa is the problem. Check this out:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim dblNumber1 As Double
    5.     Dim dblNumber2 As Double
    6.    
    7.     dblNumber1 = 0.87
    8.     dblNumber2 = CSng(0.0087) * 100
    9.    
    10.     Debug.Print "========================================"
    11.     Debug.Print "dblNumber1 = " & dblNumber1
    12.     Debug.Print "dblNumber2 = " & dblNumber2
    13.    
    14.     Debug.Print "dblNumber1 = dblNumber2: " & (dblNumber1 = dblNumber2)
    15.  
    16. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  8. #8
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Get this:

    Call MsgBox((8.7 * 10 ^ -3 * 100) = 0.87)

    is true...
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  9. #9
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    It's not that weird. The problem is that you're losing some decimals when perfoming those calculations. For example:

    0.87510 = 0.1112

    what it's equal to:

    0.875 = 2-1 + 2-2 + 2-3 = 0.5 + 0.25 + 0.125
    (power of two... because it's binary notation)

    The problem is that the number 0.0087 cannot be represented exactly as 0.0087 because you would need a lot of addends which you will lose some of them (for sure) due to the double precision.

    I hope this clarifies a little.... because it's not that easy to explain.

    Edit: I wrote the exponents as superscript numbers...
    Last edited by Mc Brain; Aug 19th, 2002 at 10:39 PM.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    I figured as much, but this is something I would classify as a quirk, not a feature...
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  11. #11
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    ok...let me see this...


    Why?...why would you like to COMPARE a Math operation with it's actual result?
    and being 0.87 the actual result of (0.0087 * 100) ...It'll of course will give you a TRUE result...
    because [B]You are comparing one VALUE against another VALUE

    Isn't obvious that this (0.0087 * 100) = 0.87 is not an Math operation?, but a tricky question...
    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2001
    Posts
    51
    OK McBrain you've earned you name, that makes sense.

    I looked up some information about Mantissas and indeed that appears to be the answer.

    dblNumber1 and dblNumber2's exponent and the mantissa are different, though they represent the same number. That is why Val() works and a simple = does not.

    I hope everyone at NASA is aware of this.

    Thanks

  13. #13
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    err...actually
    rereading...it's the representation of a Math statement...

    just like 1 = 1 that of course it's TRUE!!
    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

  14. #14
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    Microsoft tells not to compare floating point values for the equality... because it loses some precision when you display a floating point in limited digits..i.e.
    0.87 might be 0.8700000000121111 for the registers but it displays to be 0.87

  15. #15
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by jhunt
    OK McBrain you've earned you name, that makes sense.

    I looked up some information about Mantissas and indeed that appears to be the answer.

    dblNumber1 and dblNumber2's exponent and the mantissa are different, though they represent the same number. That is why Val() works and a simple = does not.

    I hope everyone at NASA is aware of this.

    Thanks
    You're welcome.... and I also hope NASA is aware of it.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width