This should be easy

Sharky Forums


Results 1 to 9 of 9

Thread: This should be easy

  1. #1
    Expensive Sushi blowwad's Avatar
    Join Date
    Aug 2001
    Location
    lakewood, wa, u.s.a.
    Posts
    15

    Post This should be easy

    Just a small question:
    I have just began learning the C language and have come upon a question:
    In a program I have written to convert the temperature from Faranheite to Centigrade something curious has come up, when I set the equation of my conversion to look like this:
    celcius = (5/9) * (frnht - 32)
    my results are always zero. However, when I re-arrange it to look like this:
    celcius = 5 * (frngt - 32)/9
    my results are accurate. Both statements are mathamatically correct but only one works. The question I have is are mathmatic equations in the C language dependant on the variable being directly involved in the actions of the numbers?

  2. #2
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203

    Post

    Originally posted by blowwad:
    A) celcius = (5/9) * (frnht - 32)
    B) celcius = 5 * (frngt - 32)/9

    There is nothing so odd as having the variables involved. What's happening is that you're doing integer math in A). The result of dividing the integer 5 by the integer 9 is the integer 0.

    In B), frnht is prolly a float, so the subtraction result is a float. The division by 9 is a float, and the result of the multiplication is a float. Thus, you get the expected result.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  3. #3
    Expensive Sushi blowwad's Avatar
    Join Date
    Aug 2001
    Location
    lakewood, wa, u.s.a.
    Posts
    15

    Post

    My bad, I should have mentioned that the variables are declared as float.

  4. #4
    Expensive Sushi blowwad's Avatar
    Join Date
    Aug 2001
    Location
    lakewood, wa, u.s.a.
    Posts
    15

    Post

    wait, revalation comming...
    so what you are saying is that even with constants the result of the division is *******? does that include a simple division of two constant *******s to a float variable?

  5. #5
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203

    Post

    That's right. The result of (5/9) is 0 and it's upgraded to a float as the result of the next multiply.

    If you were to do (5./9) you'll get the right answer because 5.0/9 = 0.555556

    Your variables are floats, but the constants are still integers unless you include the decimal.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  6. #6
    Expensive Sushi blowwad's Avatar
    Join Date
    Aug 2001
    Location
    lakewood, wa, u.s.a.
    Posts
    15

    Post

    Thank-You.

  7. #7
    Stormtrooper Mod Pinky's Avatar
    Join Date
    Oct 2000
    Posts
    2,971

    Post

    You may also want to create constants with an actual type by using a little C++:

    Code:
    const float CELSIUS 32;   (or whatever)
    That probably isn't a huge deal for this prog, but it's just for future reference. You could also use type casting.

    ------------------
    Embrace this moment. Remember we are eternal. All this pain is an illusion.

    [the man] [the real man] [the specs]

    Goodbye, OT

    Proofread carefully to see if you any words out.

    [the rules]

  8. #8
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    Originally posted by Pinky:
    You may also want to create constants with an actual type by using a little C++:

    Code:
    const float CELSIUS 32;   (or whatever)
    That probably isn't a huge deal for this prog, but it's just for future reference. You could also use type casting.

    I'd probably keep the numbers hardcoded in an inline function or a macro, because they never change, they probably will only be used in one place, and they are two numbers that will always used together.

    Examples of why all three are important for hardcoding:

    1. The number Pi. It may change to increase precision, so #defining it is better.

    2. The number 0x13h, used for screen modes. Will never change, but it will probably be used in several places, so #defining it is better.

    3. Not sure about another two numbers that are used together, but it's obvious that defining 32 and 5/9 separately is a bit weird, since one does not make much sense without the other.

    But basically, I'd agree with the other author; always use #defined numbers (or consts) except when using 1, 0, or very rare cases like this one.

    One more note. 5.0/9.0 actually SHOULD be #defined, just to speed things up. Doing anything in runtime that can be preprocessed is a bad thing.
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

  9. #9
    Stormtrooper Mod Pinky's Avatar
    Join Date
    Oct 2000
    Posts
    2,971

    Post

    Thanks for the info, Zoma!

    ------------------
    Embrace this moment. Remember we are eternal. All this pain is an illusion.

    [the man] [the real man] [the specs]

    Goodbye, OT

    Proofread carefully to see if you any words out.

    [the rules]

Posting Permissions

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