Thread: Why does parenthesis around a fraction (5/9) break my program? Beginner!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    To elaborate, it's because of order of operations, and the fact that, when an expression has mixed integers and floating-point operands, the integers are promoted into floats to do the math.

    Code:
    (fDegrees-32)*5/9;
    In this case, fDegrees is a float, so 32 is converted to 32.0 and the result is a float. Next, the float is multiplied by 5, so 5 is promoted to a float, too. Finally, 9 is promoted to a float as floating-point division is done.

    Code:
    (fDegrees-32)*(5/9);
    Here fDegrees-32 is still a float, but the parentheses mean that 5/9 is evaluated before the multiplication. Since 5 and 9 are integer literals, it does integer division, and 5/9 = 0 in integer division.

    The simplest way to fix this is to use floating-point literals such as (5.0f/9.0f)

    You also likely want atof() not atoi() as you're trying to parse floating-point values, not integers.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  2. #2
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by Cat View Post
    To elaborate, it's because of order of operations, and the fact that, when an expression has mixed integers and floating-point operands, the integers are promoted into floats to do the math.

    Code:
    (fDegrees-32)*5/9;
    In this case, fDegrees is a float, so 32 is converted to 32.0 and the result is a float. Next, the float is multiplied by 5, so 5 is promoted to a float, too. Finally, 9 is promoted to a float as floating-point division is done.

    Code:
    (fDegrees-32)*(5/9);
    Here fDegrees-32 is still a float, but the parentheses mean that 5/9 is evaluated before the multiplication. Since 5 and 9 are integer literals, it does integer division, and 5/9 = 0 in integer division.

    The simplest way to fix this is to use floating-point literals such as (5.0f/9.0f)

    You also likely want atof() not atoi() as you're trying to parse floating-point values, not integers.
    I was going to point out he was using the wrong formula and that he should also be using atof but you beat me to it. For the OP though, here are some other formulas you can use as well.

    C = (F - 32) * 5 / 9 (this is the one you're trying to use)
    C = (F - 32) / (9 / 5) (notice how this one requires the parenthesis around the 9/5. The division of 9/5 must get divided into whatever F-32 is. We don't want F-32 getting divided by just 9).
    C = (F - 32) / 1.8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to convert from decimal to fraction
    By acho.arnold in forum C Programming
    Replies: 16
    Last Post: 07-12-2013, 04:10 PM
  2. Fraction Program...
    By hottiefee in forum C++ Programming
    Replies: 15
    Last Post: 01-18-2011, 09:04 PM
  3. Replies: 1
    Last Post: 05-29-2010, 05:24 AM
  4. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  5. Problem with reducing fraction program
    By Turtal in forum C Programming
    Replies: 12
    Last Post: 12-07-2007, 02:25 AM