Results 1 to 9 of 9

Thread: VB does not calculate correctly

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    Brossard, Québec, Canada
    Posts
    241

    Angry

    I know my problem is probably due to my variable type but I have tried many thins and am not able to figure it out.

    My variable types:

    Dim Prime as Single
    Dim Percentage as Single

    in VB:

    Prime = 266.09
    Percentage = 0.1

    Prime = Prime * Percentage

    'Now here... vb puts something like 26.60899945761 in Prime
    'We all know that 10% of 266.09 = 26.609

    How come I dont get the right answer???

    please help me
    thank you

  2. #2
    Guest
    I tried that right now and got 26.609

  3. #3
    Guest
    By the way. Incase you need it, here is the exact code that I used.

    Code:
    Private Sub Command1_Click()
    
       Dim num As Single
       Dim Prime As Single
       num = 266.09
       Prime = 0.1
       blah = num * Prime
       MsgBox (blah)
    
    
    End Sub

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    When you calculate with floating points, you can't have the exact values returned, but you will have a good approximation with double...
    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.

  5. #5
    Guest
    It might just be the version of VB because it worked for me.


  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    No it isn't meg, floating points are always stored the same way, if it shows that number it doesn't mean that it shows the same in the memory. Next time you calculate with it you may notice the difference.
    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    Brossard, Québec, Canada
    Posts
    241

    Question Thanks for your help...

    Megatron: What is the variable type of BLAH ??

    Variant?
    I use only single types...


    as for calculating with floating points, isn't it still supposed to give the right answer?? I mean 266.09 * 0.1 is not hard to do.... a good old simple calculator does it right... why not VB

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Declare both variable as currency:

    Dim num@,Prime@
    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.

  9. #9
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    This may be way off base but.

    Hi,
    Do you remember a few years ago that some Intel microprocessors had a flaw that caused some calculation errors? I forget exactly which MPU it was but it was a Pentium. There were some tests you could perform to see if you had one of the defective ones.
    Just a thought.
    Al.
    A computer is a tool, not a toy.

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