Results 1 to 9 of 9

Thread: The day has come, where 9 is than 9

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    12

    The day has come, where 9 is than 9

    I was getting red in the face with this thing, trying to figure out
    why 9 wasn't equal to 9. Took me a while to figure it out, but I finally found out why. You have a look, it'll make you go nuts if you don't see it at first

    Code:
    Private Sub Command1_Click()
    'Dim your variables
    Dim i
    Dim x
    
    'Dim x. Count it with a calculator if you want, but x = 9.
    x = 387420489 ^ (1 / 9)
    
    'Finally, do a loop from 1 to 9. Once x = i (Or i = 9), it'll tell you it's equal. But it never does. Aparently, 9 is actually smaller than 9!
    
    For i = 1 To 9
        If x < i Then
            MsgBox "Smaller"
        ElseIf x = i Then
            MsgBox "Equal"
        Else
            MsgBox "Bigger"
        End If
    Next i
    End Sub
    Have fun
    Is that not the Beast you said you saw? The one from Hell, the one that strives to drown you in the deepest pits of darkness, the pits of insanity?

    My soul is resting yet my body alert; my instincts rule, my mind is shut; I am an animal, a beast on guard, hidden in my cave I cannot sleep, I just watch, watch your every movement, ready to strike at you when you expect it least. Look behind you, my friend, you might see my dead eyes staring through you, and you, too, might put your soul to rest, to be protected from the demon that's stalking you.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    ?x-i
    -1.77635683940025E-15
    Don't use floating points for precise calculations, especially not variants.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Round off.

    The problem is due to round off error when computing the exponential. 1/9 is not exact in either binary or decimal. The exponential function is likely to result in round off error even if the operands are integers.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  4. #4
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375

    Re: Round off.

    Originally posted by Guv
    The problem is due to round off error when computing the exponential. 1/9 is not exact in either binary or decimal. The exponential function is likely to result in round off error even if the operands are integers.
    Exactly. (1/9) cant be calculated exactly in binary...so the computer gets messed up. But it seems the windows calculator actually deals with the fraction so its fine.
    You just proved that sig advertisements work.

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    Nishantp: A calulator which got exactly 9 for the result probably had some other internal result which rounded to 9 when displayed.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  6. #6
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by Guv
    Nishantp: A calulator which got exactly 9 for the result probably had some other internal result which rounded to 9 when displayed.
    I dont know about the Windows Calculator but i know that some calculators are able to do calculations based on the actual fractions so i know its possible (they are probably programmed with methods that we humans use).
    You just proved that sig advertisements work.

  7. #7
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    X isn't really 9; its like 8.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999 or something and VB can't hold that so it rounds up. Either that or X is 9.00000000000000000000000000000000000000000000000000000000001 and the same thing happens.

    Fractions don't need special math to calculate them; they are really just divisions that aren't calculated.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  8. #8
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Well couldnt you hardcode some functions that do fractional calculations based on human techniques? I wrote something like that to get a huge number of decimals from a division. There SLOW but they work.
    You just proved that sig advertisements work.

  9. #9
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    Thats pretty much how they work yeh. And they are slow.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

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