Question on the FAQ

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Topi Linkala

    Question on the FAQ

    Question 3.15:

    Why does the code

    double degC, degF;
    degC = 5 / 9 * (degF - 32);

    keep giving me 0?


    Would the following rearrangement solve the problem?

    degC = 5 * (degF - 32) / 9;


    Topi Linkala
    --
    "The whole problem with the world is that fools and fanatics are
    always so certain of themselves, but wiser people so full of doubts."
    - Bertrand Russell
    "How come he didn't put 'I think' at the end of it?" - Anonymous
  • Robert Gamble

    #2
    Re: Question on the FAQ

    On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwro te:
    Question 3.15:
    >
    Why does the code
    >
    double degC, degF;
    degC = 5 / 9 * (degF - 32);
    >
    keep giving me 0?
    >
    Would the following rearrangement solve the problem?
    >
    degC = 5 * (degF - 32) / 9;
    >
    Did you read the answer to the question (<http://c-faq.com/expr/
    truncation1.htm l>)?

    --
    Robert Gamble

    Comment

    • Topi Linkala

      #3
      Re: Question on the FAQ

      Robert Gamble wrote:
      On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwro te:
      >
      >>Question 3.15:
      >>
      >>Why does the code
      >>
      >>double degC, degF;
      >>degC = 5 / 9 * (degF - 32);
      >>
      >>keep giving me 0?
      >>
      >>Would the following rearrangement solve the problem?
      >>
      >>degC = 5 * (degF - 32) / 9;
      >>
      >
      >
      Did you read the answer to the question (<http://c-faq.com/expr/
      truncation1.htm l>)?
      Yes I read and it says:

      "If both operands of a binary operator are integers, C performs an
      integer operation,..."

      But in my rewriting there are no binary operation with integer values.
      That's why I'm asking if it would work.

      Topi
      --
      "The whole problem with the world is that fools and fanatics are
      always so certain of themselves, but wiser people so full of doubts."
      - Bertrand Russell
      "How come he didn't put 'I think' at the end of it?" - Anonymous

      Comment

      • Robert Gamble

        #4
        Re: Question on the FAQ

        On Apr 13, 3:48 pm, Topi Linkala <n...@iki.fiwro te:
        Robert Gamble wrote:
        On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwro te:
        >
        >Question 3.15:
        >
        >Why does the code
        >
        >double degC, degF;
        >degC = 5 / 9 * (degF - 32);
        >
        >keep giving me 0?
        >
        >Would the following rearrangement solve the problem?
        >
        >degC = 5 * (degF - 32) / 9;
        >
        Did you read the answer to the question (<http://c-faq.com/expr/
        truncation1.htm l>)?
        >
        Yes I read and it says:
        >
        "If both operands of a binary operator are integers, C performs an
        integer operation,..."
        >
        But in my rewriting there are no binary operation with integer values.
        That's why I'm asking if it would work.
        >
        Sorry, I missed the fact that degF was a double. Yes, your example
        will work properly since the division will be happening on floating
        point values.

        --
        Robert Gamble

        Comment

        • Joe Wright

          #5
          Re: Question on the FAQ

          Topi Linkala wrote:
          Robert Gamble wrote:
          >
          >On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwro te:
          >>
          >>Question 3.15:
          >>>
          >>Why does the code
          >>>
          >>double degC, degF;
          >>degC = 5 / 9 * (degF - 32);
          >>>
          >>keep giving me 0?
          >>>
          >>Would the following rearrangement solve the problem?
          >>>
          >>degC = 5 * (degF - 32) / 9;
          >>>
          >>
          >>
          >Did you read the answer to the question (<http://c-faq.com/expr/
          >truncation1.ht ml>)?
          >
          Yes I read and it says:
          >
          "If both operands of a binary operator are integers, C performs an
          integer operation,..."
          >
          But in my rewriting there are no binary operation with integer values.
          That's why I'm asking if it would work.
          >
          Topi
          The statement..

          degC = 5 / 9 * (degF - 32);

          ...can't work because 5 / 9 is (int) zero. Also degF is not initialized.

          degC = (degF - 32) * 5 / 9;

          should work better for you.

          --
          Joe Wright
          "Everything should be made as simple as possible, but not simpler."
          --- Albert Einstein ---

          Comment

          • Topi Linkala

            #6
            Re: Question on the FAQ

            Joe Wright wrote:
            Topi Linkala wrote:
            >
            >Robert Gamble wrote:
            >>
            >>On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwro te:
            >>>
            >>>Question 3.15:
            >>>>
            >>>Why does the code
            >>>>
            >>>double degC, degF;
            >>>degC = 5 / 9 * (degF - 32);
            >>>>
            >>>keep giving me 0?
            >>>>
            >>>Would the following rearrangement solve the problem?
            >>>>
            >>>degC = 5 * (degF - 32) / 9;
            >>>>
            >>>
            >>>
            >>Did you read the answer to the question (<http://c-faq.com/expr/
            >>truncation1.h tml>)?
            >>
            >>
            >Yes I read and it says:
            >>
            >"If both operands of a binary operator are integers, C performs an
            >integer operation,..."
            >>
            >But in my rewriting there are no binary operation with integer values.
            >That's why I'm asking if it would work.
            >>
            >Topi
            >
            >
            The statement..
            >
            degC = 5 / 9 * (degF - 32);
            >
            ..can't work because 5 / 9 is (int) zero. Also degF is not initialized.
            >
            degC = (degF - 32) * 5 / 9;
            >
            should work better for you.
            >
            I don't think so because there's no reason to believe that the
            multiplication is done before the division, but in my example that
            doesn't matter as one of the operands is double in any case.

            Topi
            --
            "The whole problem with the world is that fools and fanatics are
            always so certain of themselves, but wiser people so full of doubts."
            - Bertrand Russell
            "How come he didn't put 'I think' at the end of it?" - Anonymous

            Comment

            • Robert Gamble

              #7
              Re: Question on the FAQ

              On Apr 13, 4:53 pm, Topi Linkala <n...@iki.fiwro te:
              Joe Wright wrote:
              Topi Linkala wrote:
              >
              Robert Gamble wrote:
              >
              >On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwro te:
              >
              >>Question 3.15:
              >
              >>Why does the code
              >
              >>double degC, degF;
              >>degC = 5 / 9 * (degF - 32);
              >
              >>keep giving me 0?
              >
              >>Would the following rearrangement solve the problem?
              >
              >>degC = 5 * (degF - 32) / 9;
              >
              >Did you read the answer to the question (<http://c-faq.com/expr/
              >truncation1.ht ml>)?
              >
              Yes I read and it says:
              >
              "If both operands of a binary operator are integers, C performs an
              integer operation,..."
              >
              But in my rewriting there are no binary operation with integer values.
              That's why I'm asking if it would work.
              >
              Topi
              >
              The statement..
              >
              degC = 5 / 9 * (degF - 32);
              >
              ..can't work because 5 / 9 is (int) zero. Also degF is not initialized.
              >
              degC = (degF - 32) * 5 / 9;
              >
              should work better for you.
              >
              I don't think so because there's no reason to believe that the
              multiplication is done before the division, but in my example that
              doesn't matter as one of the operands is double in any case.
              Actually, the multiplication is guaranteed to be performed before the
              division because they both have the same precedence and are left-to-
              right associative.

              --
              Robert Gamble

              Comment

              • Flash Gordon

                #8
                Re: Question on the FAQ

                Topi Linkala wrote, On 13/04/08 20:48:
                Robert Gamble wrote:
                >
                >On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwro te:
                >>
                >>Question 3.15:
                >>>
                >>Why does the code
                >>>
                >>double degC, degF;
                >>degC = 5 / 9 * (degF - 32);
                >>>
                >>keep giving me 0?
                >>>
                >>Would the following rearrangement solve the problem?
                >>>
                >>degC = 5 * (degF - 32) / 9;
                >>
                >Did you read the answer to the question (<http://c-faq.com/expr/
                >truncation1.ht ml>)?
                >
                Yes I read and it says:
                >
                "If both operands of a binary operator are integers, C performs an
                integer operation,..."
                Which is why it does not work.
                But in my rewriting there are no binary operation with integer values.
                Which is why it would work.
                That's why I'm asking if it would work.
                You obviously did not make it clear enough to Robert that you had read
                it. Don't worry about it.
                --
                Flash Gordon

                Comment

                • CBFalconer

                  #9
                  Re: Question on the FAQ

                  Topi Linkala wrote:
                  >
                  Question 3.15:
                  >
                  Why does the code
                  double degC, degF;
                  degC = 5 / 9 * (degF - 32);
                  keep giving me 0?
                  Think about it. 5 and 9 are ints. What is the value of 5/9?. How
                  many times can you subtract 9 from 5 and have the result
                  non-negative? What is the result of that multiplied by (degF -32)?
                  >
                  Would the following rearrangement solve the problem?
                  degC = 5 * (degF - 32) / 9;
                  That depends on the type of degF. For double, yes. Much simpler
                  to use 5/9.0.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.

                  ** Posted from http://www.teranews.com **

                  Comment

                  • Ian Collins

                    #10
                    Re: Question on the FAQ

                    CBFalconer wrote:
                    Topi Linkala wrote:
                    >Question 3.15:
                    >>
                    >Why does the code
                    > double degC, degF;
                    > degC = 5 / 9 * (degF - 32);
                    >keep giving me 0?
                    >
                    Think about it. 5 and 9 are ints. What is the value of 5/9?. How
                    many times can you subtract 9 from 5 and have the result
                    non-negative? What is the result of that multiplied by (degF -32)?
                    >
                    >Would the following rearrangement solve the problem?
                    > degC = 5 * (degF - 32) / 9;
                    >
                    That depends on the type of degF.
                    If you read the OP, degF was declared as double.

                    --
                    Ian Collins.

                    Comment

                    • Andrey Tarasevich

                      #11
                      Re: Question on the FAQ

                      Topi Linkala wrote:
                      ...
                      But what stops the compiler to calculate the 5/9 first as it is a
                      constant expression and can be done on compile time?
                      Nothing really. In the expresison 'E * 5/9' the compiler is free to
                      calculate '5/9' in advance. What the compiler _cannot_ do is to destroy
                      the semantics if the full expression (derived from the grammar) and
                      evaluate '5/9' as an integral division. If the compiler want to
                      pre-evaluate '5/9', it can do that, but it has to make sure that the
                      result is precise enough so that the whole thing works "as if" it
                      evaluated 'E * 5' first and then divided the result by '9'.
                      AFAIK only guaranteed sequencing on expressions are:
                      >
                      1. precedence
                      Precedence does not guarantee any sequencing. If defines the semantics
                      of the expression, i.e. it disambiguates the result. It tells you that
                      '2 + 2 * 2' is 6 and not 8. But the compiler is perfectly free to forget
                      about these 2's and obtain that final 6 as '37 / 5 + 1' if it is so
                      inclined.
                      2. comma operation
                      3. boolean operations || and &&
                      4. ?:
                      These three do indeed introduce real sequencing.
                      So an expression a*b/c can be calculated (a*b)/c or a*(b/c).
                      Yes, it can be calculated as 'a*(b/c)' as long as the result is the same
                      as '(a*b)/c'. If on the given platform 'a*(b/c)' give a different
                      result, then it the compiler will have no choice but calculate it as
                      '(a*b)/c'.

                      --
                      Best regards,
                      Andrey Tarasevich

                      Comment

                      Working...