Computed values don't tie up

Discussion in 'C Programming' started by James Kuyper, Apr 3, 2012.

  1. James Kuyper

    James Kuyper Guest

    On 04/03/2012 03:29 PM, rkfb wrote:
    > I'm reading 'C Programming: A Modern Approach' by KN King.
    >
    > If I run celcius.c (as below) and enter a value of 32 (or 32.0) I am given
    > the equivalent celcius temperature as -2.2. Surely it should be zero?
    >
    > /* Converts a Fahrenheit temperature to Celcius */
    >
    > #include <stdio.h>
    >
    > #define FREEZING_PT 32.0f
    > #define SCALE_FACTOR (5.0f / 9.0f)
    >
    > int main(void)
    > {
    > float fahrenheit, celcius;
    >
    > printf("Enter Fahrenheit temperatue: ");
    > scanf("%f", &fahrenheit);


    Like most I/O functions, scanf() can fail. You should always check to
    see whether or not it did fail. If so, there's no guarantee that
    'fahrenheit' contains a representation of a valid floating point value;
    it could contain a signaling NaN, for instance, so you should only
    execute the rest of your program is scanf() succeeded.

    > celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
    >
    > printf("Celcius equivalent: ".1f", celcius);


    This line has one too many double quote characters; your code as
    presented here should not have compiled without diagnostics, so this
    can't be your actual program. Please cut and paste the actual text of
    your program; that will make it much easier to figure out what's going
    wrong.
    Also, if the output from your program does not end with a new-line
    character, the behavior is undefined; on many systems that's not a
    problem, but even where it is permitted, I think it would be better to
    append a '\n' to the printf() format string.

    When I correct the issues I've raised above in the obvious ways, and
    type in 32, it prints out 0; so that's another reason to think that this
    in not exactly your actual program.

    > return 0;
    > }
     
    James Kuyper, Apr 3, 2012
    #1
    1. Advertising

  2. James Kuyper

    rkfb Guest

    I'm reading 'C Programming: A Modern Approach' by KN King.

    If I run celcius.c (as below) and enter a value of 32 (or 32.0) I am given
    the equivalent celcius temperature as -2.2. Surely it should be zero?

    /* Converts a Fahrenheit temperature to Celcius */

    #include <stdio.h>

    #define FREEZING_PT 32.0f
    #define SCALE_FACTOR (5.0f / 9.0f)

    int main(void)
    {
    float fahrenheit, celcius;

    printf("Enter Fahrenheit temperatue: ");
    scanf("%f", &fahrenheit);

    celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;

    printf("Celcius equivalent: ".1f", celcius);

    return 0;
    }

    --
    Robert
     
    rkfb, Apr 3, 2012
    #2
    1. Advertising

  3. James Kuyper

    BartC Guest

    "rkfb" <> wrote in message
    news:...
    > I'm reading 'C Programming: A Modern Approach' by KN King.
    >
    > If I run celcius.c (as below) and enter a value of 32 (or 32.0) I am given
    > the equivalent celcius temperature as -2.2. Surely it should be zero?


    > celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
    >
    > printf("Celcius equivalent: ".1f", celcius);


    Display the fahrenheit value as well. To check if it is really 32.0. And
    perhaps FREEZING_PT and SCALE_FACTOR (should be 0.555..) while you're about
    it.

    --
    Bartc
     
    BartC, Apr 3, 2012
    #3
  4. James Kuyper

    Ike Naar Guest

    On 2012-04-03, Robert <> wrote:
    > #include <stdio.h>
    > #define FREEZING_PT 36.0f


    #define FREEZING_PT 32.0f
     
    Ike Naar, Apr 3, 2012
    #4
  5. James Kuyper

    BartC Guest

    "Robert" <> wrote in message
    news:...

    > #define FREEZING_PT 36.0f


    Try 32.0.

    --
    Bartc
     
    BartC, Apr 3, 2012
    #5
  6. James Kuyper

    James Kuyper Guest

    On 04/03/2012 04:52 PM, Ike Naar wrote:
    > On 2012-04-03, Robert <> wrote:
    >> #include <stdio.h>
    >> #define FREEZING_PT 36.0f

    >
    > #define FREEZING_PT 32.0f


    And that is a prime example of why it's always best to cut and paste the
    actual code that has problems, rather than re-typing it.
     
    James Kuyper, Apr 3, 2012
    #6
  7. James Kuyper

    BartC Guest

    "James Kuyper" <> wrote in message
    news:...
    > On 04/03/2012 04:52 PM, Ike Naar wrote:
    >> On 2012-04-03, Robert <> wrote:
    >>> #include <stdio.h>
    >>> #define FREEZING_PT 36.0f

    >>
    >> #define FREEZING_PT 32.0f

    >
    > And that is a prime example of why it's always best to cut and paste the
    > actual code that has problems, rather than re-typing it.


    (I actually got a value of 35.96 for the freezing point, when trying to work
    out what would give an answer of -2.2. But I thought, how likely would it be
    to get that wrong...)

    --
    Bartc
     
    BartC, Apr 3, 2012
    #7
  8. James Kuyper

    Robert Guest

    James Kuyper wrote:

    [...]
    >
    > This line has one too many double quote characters; your code as
    > presented here should not have compiled without diagnostics, so this
    > can't be your actual program. Please cut and paste the actual text of
    > your program; that will make it much easier to figure out what's going
    > wrong.
    > Also, if the output from your program does not end with a new-line
    > character, the behavior is undefined; on many systems that's not a
    > problem, but even where it is permitted, I think it would be better to
    > append a '\n' to the printf() format string.
    >
    > When I correct the issues I've raised above in the obvious ways, and
    > type in 32, it prints out 0; so that's another reason to think that this
    > in not exactly your actual program.
    >
    >> return 0;
    >> }


    (Copy/paste)

    /* Converts a Fahrenheit temperature to Celcius */

    #include <stdio.h>
    #define FREEZING_PT 36.0f
    #define SCALE_FACTOR (5.0f / 9.0f)

    int main(void)
    {
    float fahrenheit, celcius;

    printf("Enter fahrenheit temperature: ");
    scanf("%f", &fahrenheit);

    celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;

    printf("Celcius equivalent: %.1f\n", celcius);

    return 0;
    }

    sorry...I did just retype it out in my original post, this is the actual
    code. I copied it straight from the book into Emacs. I'm on Slackware64
    13.37 and have run it within Emacs, within KDevelop and also in xterm. It's
    always -2.2. I've now made sure it has a newline at the end and have added
    one to printf:

    /* Converts a Fahrenheit temperature to Celcius */

    #include <stdio.h>
    #define FREEZING_PT 36.0f
    #define SCALE_FACTOR (5.0f / 9.0f)

    int main(void)
    {
    float fahrenheit, celcius;

    printf("Enter fahrenheit temperature: \n");
    scanf("%f", &fahrenheit);

    celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;

    printf("Celcius equivalent: %.1f\n", celcius);

    return 0;
    }

    it still gives 2.2.



    --
    Robert
     
    Robert, Apr 3, 2012
    #8
  9. James Kuyper

    Robert Guest

    BartC wrote:

    > "rkfb" <> wrote in message
    > news:...
    >> I'm reading 'C Programming: A Modern Approach' by KN King.
    >>
    >> If I run celcius.c (as below) and enter a value of 32 (or 32.0) I am
    >> given the equivalent celcius temperature as -2.2. Surely it should be
    >> zero?

    >
    >> celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
    >>
    >> printf("Celcius equivalent: ".1f", celcius);

    >
    > Display the fahrenheit value as well. To check if it is really 32.0. And
    > perhaps FREEZING_PT and SCALE_FACTOR (should be 0.555..) while you're
    > about it.
    >


    I'll try BartC but I'm on page 25 and this is only the third program I've
    worked through.

    --
    Robert
     
    Robert, Apr 3, 2012
    #9
  10. James Kuyper

    Robert Guest

    Robert wrote:

    [...]
    > it still gives 2.2.


    -2.2


    --
    Robert
     
    Robert, Apr 3, 2012
    #10
  11. James Kuyper

    Robert Guest

    BartC wrote:

    > "Robert" <> wrote in message
    > news:...
    >
    >> #define FREEZING_PT 36.0f

    [various]
    > Try 32.0.


    Well how embarrassing! So it wasn't even a C question after all, just my
    inability to follow the book correctly. Thank you all for your help, sorry
    to trouble you in the first place.

    --
    Robert
     
    Robert, Apr 3, 2012
    #11
  12. James Kuyper

    Kleuske Guest

    On Tue, 03 Apr 2012 19:29:28 +0000, rkfb saw fit to publish the following:

    > I'm reading 'C Programming: A Modern Approach' by KN King.
    >
    > If I run celcius.c (as below) and enter a value of 32 (or 32.0) I am
    > given the equivalent celcius temperature as -2.2. Surely it should be
    > zero?
    >
    > /* Converts a Fahrenheit temperature to Celcius */
    >
    > #include <stdio.h>
    >
    > #define FREEZING_PT 32.0f
    > #define SCALE_FACTOR (5.0f / 9.0f)
    >
    > int main(void)
    > {
    > float fahrenheit, celcius;
    >
    > printf("Enter Fahrenheit temperatue: "); scanf("%f", &fahrenheit);
    >
    > celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
    >
    > printf("Celcius equivalent: ".1f", celcius);
    >
    > return 0;
    > }


    If the values for fahrenheit and FREEZING_PT are very close, the subtraction
    may degenerate your result, i.e. it looses any significance. This is made
    worse by the fact that you use a float, which, generally, does not have enough
    precision. Consider using doubles instead.

    This is not a problem of 'C', but rears it's ugly head in any language every
    time you subtract floating point numbers which are close together. Beware.

    I'm not sure that explains your results, though. The deviation seems a bit on
    the big side.

    --

    War isn't a good life, but it's life.
    -- Kirk, "A Private Little War", stardate 4211.8
     
    Kleuske, Apr 4, 2012
    #12
  13. James Kuyper

    Geoff Guest

    On Tue, 03 Apr 2012 21:37:08 +0000, Robert <>
    wrote:

    >James Kuyper wrote:
    >
    >[...]
    >>
    >> This line has one too many double quote characters; your code as
    >> presented here should not have compiled without diagnostics, so this
    >> can't be your actual program. Please cut and paste the actual text of
    >> your program; that will make it much easier to figure out what's going
    >> wrong.
    >> Also, if the output from your program does not end with a new-line
    >> character, the behavior is undefined; on many systems that's not a
    >> problem, but even where it is permitted, I think it would be better to
    >> append a '\n' to the printf() format string.
    >>
    >> When I correct the issues I've raised above in the obvious ways, and
    >> type in 32, it prints out 0; so that's another reason to think that this
    >> in not exactly your actual program.
    >>
    >>> return 0;
    >>> }

    >
    >(Copy/paste)
    >
    >/* Converts a Fahrenheit temperature to Celcius */
    >
    >#include <stdio.h>
    >#define FREEZING_PT 36.0f
    >#define SCALE_FACTOR (5.0f / 9.0f)
    >
    >int main(void)
    >{
    > float fahrenheit, celcius;
    >
    > printf("Enter fahrenheit temperature: ");
    > scanf("%f", &fahrenheit);
    >
    > celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
    >
    > printf("Celcius equivalent: %.1f\n", celcius);
    >
    > return 0;
    >}
    >


    Others have pointed out the error.

    To check this program's zero intercept and scale factor use these data
    points:

    32.0 F = 0.0 C
    -40.0 F = -40.0 C
    212.0 F = 100.0 C
     
    Geoff, Apr 4, 2012
    #13
  14. Kleuske <> writes:

    > On Tue, 03 Apr 2012 19:29:28 +0000, rkfb saw fit to publish the following:
    >
    >> I'm reading 'C Programming: A Modern Approach' by KN King.
    >>
    >> If I run celcius.c (as below) and enter a value of 32 (or 32.0) I am
    >> given the equivalent celcius temperature as -2.2. Surely it should be
    >> zero?
    >>
    >> /* Converts a Fahrenheit temperature to Celcius */
    >>
    >> #include <stdio.h>
    >>
    >> #define FREEZING_PT 32.0f
    >> #define SCALE_FACTOR (5.0f / 9.0f)
    >>
    >> int main(void)
    >> {
    >> float fahrenheit, celcius;
    >>
    >> printf("Enter Fahrenheit temperatue: "); scanf("%f", &fahrenheit);
    >>
    >> celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
    >>
    >> printf("Celcius equivalent: ".1f", celcius);
    >>
    >> return 0;
    >> }

    >
    > If the values for fahrenheit and FREEZING_PT are very close, the subtraction
    > may degenerate your result, i.e. it looses any significance. This is made
    > worse by the fact that you use a float, which, generally, does not have enough
    > precision. Consider using doubles instead.
    >
    > This is not a problem of 'C', but rears it's ugly head in any language every
    > time you subtract floating point numbers which are close together. Beware.
    >
    > I'm not sure that explains your results, though. The deviation seems a bit on
    > the big side.


    It's been explained elsewhere, but it can't be loss of significance
    because the numbers involved in the subtraction can be represented
    exactly as floats. Also, your remark "the deviation seems a bit on the
    big side" is something of an understatement!

    --
    Ben.
     
    Ben Bacarisse, Apr 4, 2012
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Du

    Computed columns in datagrid

    Raymond Du, Dec 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,607
    Joe Fallon
    Dec 2, 2004
  2. John Dalberg
    Replies:
    1
    Views:
    435
    Bruce Barker
    Feb 3, 2006
  3. Replies:
    2
    Views:
    428
  4. Sohail Somani
    Replies:
    5
    Views:
    662
    Sohail Somani
    May 7, 2009
  5. botfood

    tie() with DB_File not tie()ing ?

    botfood, Apr 24, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    475
    botfood
    Apr 26, 2006
Loading...

Share This Page