Results 1 to 10 of 10

Thread: IF statement NOT WORKING! URGENT

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    South Africa
    Posts
    123

    Exclamation IF statement NOT WORKING! URGENT

    Hi Guys

    I know this should be simple but it just refuses to work,dont know what I'm missing.
    I have this If statement
    Code:
    IF AmountPaid >= Currentbalance Then
    My problem is that when the value for AmountPaid is equal to CurrentBalance the condition returns false,when it should be true.

    The values for both variables i'm using is 20 which is double, and the variables are passed ByReference into a sub.
    This is driving me crazy,PLEASE HELP,i need to solve this URGENTLY.

    THANKS
    Beware of the Dog

  2. #2
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    Have you tried:

    IF (AmountPaid > Currentbalance) OR (AmountPaid = Currentbalance) Then

    ?
    Do canibals not eat clowns because they taste funny?

  3. #3
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951
    Have you tried moving them ByValue? Or moving them to variables and using the variables ByValue?

  4. #4
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468
    Welcome to the world of floating point arithmetic - where nothing is what it seems.

    Your problem stems from the fact that decimal numbers aren't always exactly represented correctly in Doubles (or Singles). There are two ways to correctly check for equality:

    1) Test within a very small range.
    2) Round to a certain # of decimal places before testing.

    An example of #1:
    Code:
    If Abs(AmountPaid - Currentbalance) < 0.0001 Then
    An example of #2:
    Code:
    If Round(AmountPaid , 2) >= Round(Currentbalance, 2) Then

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    bpd is completely correct for floating point, but can I just ask why you are using it in the first place?

    It seems to me as though the Currency data type (which doesn't have these issues) would be more appropriate.

  6. #6
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Do other languages have these problems? Or is it just something that is isolated to VB?

  7. #7
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468
    Do other languages have these problems?
    Yes. I've run across this same issue in FORTRAN - a language designed primarily for numeric computations.

  8. #8
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Hmm, quite odd, I've never had such a problem as this in VB, but then again I try to limit the use of doubles/singles whenever possible.

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Since your working with monetary values, try CCur() >= CCur() first before rewritting all those variables.

  10. #10
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    Seeing that there is a slight chance that I actually wrote the part of the code that calcualtes the cost, interest and capital, in any order, you sould email me that section of the code. and I will fix it.

    First off - I am here to help you dude.... I cannot contact you by email because MF monitors the mail server. Wow. Big deal. I have lost your cell number so give me an sms or email me at mikethomson at websurfer.co.za. I am there to steal code either. I only use my own code as a rule, although there are a few really good coders on this forum that I really do trust!!!!

    PS: Dont believe what MF said - he was just really disappointed that I left. What sort of a compay do you work for that they will retrnch you rather than offer you better training??? I left because the huge program you are working on is single tier (20000+ lines of sinlge tier code!!!) with no planning and no structure. And the lack of progress on it was starting to make me look like an idiot, and this was not fair because I took over the code from two other developers who had no planning or documentation. I gave my recommendation to modulaizre the code but your managament did not agree with me. Without modularizing the code, and you working on the project, there was actually no room for me to work on it. It was impossible to split tasks. Another thing is that my being there was stinting your development.

    Secondly the app should rather have been written in php. This way it could have been easier than VB. I am now fully proficient in writing php applications, and have learnt spans about genuine MySQL security(setting database permissions for users that was not done initially). With my ability you could convert that app into a web based app and offer it locally on your LAN or just as easily load it onto your web server.

    After 6 months on my own I have really learnt how to modularize software and by doing this I have finally managed to reduce the time take for each module to about 7 days at run.

    Pretty soon I will be in Ladysmith and there are a few other excellant business opportunities there. Greed never drove me before but it is starting to become a factor.

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