Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2005
    Location
    Kuala Lumpur, MY
    Posts
    108

    Unanswered: simple mathematic calculation

    hie all

    i created a test table and inserted '54' into nm1 column and '62' into nm2 column

    i wanted to do a calculation which is 'nm2-nm1/nm1' in other words (62-54)/62 which should return 0.12

    but its returning some other result...

    any help ?

  2. #2
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1
    What result did you get? Not 61 by any chance?

    If you look carefully at your calculation you will see it is actually 62-54/54, which is evaluated as 62-(54/54) which is 61.

  3. #3
    Join Date
    Sep 2004
    Location
    London, UK
    Posts
    565
    nm2-nm1/nm1 is not the same as (62-54)/62! Check the rules for arithmetic precedence.

    This will be the same in other languages, for example (just for fun) in Korn shell:

    Code:
    /Users/williamr: float nm1=54
    /Users/williamr: float nm2=62
    /Users/williamr: echo $(( nm2-nm1/nm1 ))
    61
    /Users/williamr: echo $(( (nm2-nm1)/nm2 ))
    0.129032258065
    Update: Tony beat me to it.

    I've just noticed that your formula varies:

    Quote Originally Posted by shatishr
    i created a test table and inserted '54' into nm1 column and '62' into nm2 column.

    i wanted to do a calculation which is 'nm2-nm1/nm1' in other words (62-54)/62
    If nm1=54 and nm2=62, shouldn't that be (62-54)/54?

    Code:
    /Users/williamr: echo $(( (nm2-nm1)/nm1 ))
    0.148148148148
    Last edited by WilliamR; 01-21-06 at 12:27.

Posting Permissions

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