Computed values don't tie up

J

James Kuyper

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.
 
R

rkfb

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;
}
 
B

BartC

rkfb said:
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.
 
J

James Kuyper

#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.
 
B

BartC

James Kuyper said:
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...)
 
R

Robert

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.

(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.
 
R

Robert

BartC said:
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.
 
R

Robert

BartC said:
#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.
 
K

Kleuske

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.
 
G

Geoff

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.

(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
 
B

Ben Bacarisse

Kleuske said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top