Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #19284 > unrolled thread
| Started by | rkfb <rkfb@rebelCode.slacknet> |
|---|---|
| First post | 2012-04-03 19:29 +0000 |
| Last post | 2012-04-04 04:42 +0100 |
| Articles | 15 — 9 participants |
Back to article view | Back to comp.lang.c
Computed values don't tie up rkfb <rkfb@rebelCode.slacknet> - 2012-04-03 19:29 +0000
Re: Computed values don't tie up James Kuyper <jameskuyper@verizon.net> - 2012-04-03 14:57 -0400
Re: Computed values don't tie up Robert <rkfb1965@googlemail.com> - 2012-04-03 21:37 +0000
Re: Computed values don't tie up Robert <rkfb1965@googlemail.com> - 2012-04-03 21:39 +0000
Re: Computed values don't tie up Ike Naar <ike@iceland.freeshell.org> - 2012-04-03 20:52 +0000
Re: Computed values don't tie up James Kuyper <jameskuyper@verizon.net> - 2012-04-03 16:56 -0400
Re: Computed values don't tie up "BartC" <bc@freeuk.com> - 2012-04-03 22:11 +0100
Re: Computed values don't tie up "BartC" <bc@freeuk.com> - 2012-04-03 21:53 +0100
Re: Computed values don't tie up Robert <rkfb1965@googlemail.com> - 2012-04-03 22:56 +0000
Re: Computed values don't tie up Geoff <geoff@invalid.invalid> - 2012-04-03 20:05 -0700
Re: Computed values don't tie up Martin Ambuhl <mambuhl@earthlink.net> - 2012-04-04 00:38 -0400
Re: Computed values don't tie up "BartC" <bc@freeuk.com> - 2012-04-03 21:04 +0100
Re: Computed values don't tie up Robert <rkfb1965@googlemail.com> - 2012-04-03 21:38 +0000
Re: Computed values don't tie up Kleuske <kleuske@nowhere.net> - 2012-04-03 23:09 +0000
Re: Computed values don't tie up Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-04-04 04:42 +0100
| From | rkfb <rkfb@rebelCode.slacknet> |
|---|---|
| Date | 2012-04-03 19:29 +0000 |
| Subject | Computed values don't tie up |
| Message-ID | <alpine.LNX.2.00.1204031912440.4639@rebelCode.slacknet> |
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
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-04-03 14:57 -0400 |
| Message-ID | <4F7B482B.2040109@verizon.net> |
| In reply to | #19284 |
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;
> }
[toc] | [prev] | [next] | [standalone]
| From | Robert <rkfb1965@googlemail.com> |
|---|---|
| Date | 2012-04-03 21:37 +0000 |
| Message-ID | <9u15bsF7g3U1@mid.individual.net> |
| In reply to | #19287 |
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
[toc] | [prev] | [next] | [standalone]
| From | Robert <rkfb1965@googlemail.com> |
|---|---|
| Date | 2012-04-03 21:39 +0000 |
| Message-ID | <9u15gnF7g3U3@mid.individual.net> |
| In reply to | #19306 |
Robert wrote: [...] > it still gives 2.2. -2.2 -- Robert
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Date | 2012-04-03 20:52 +0000 |
| Message-ID | <slrn3vfsjnmop9.cpi.ike@iceland.freeshell.org> |
| In reply to | #19306 |
On 2012-04-03, Robert <rkfb1965@googlemail.com> wrote: > #include <stdio.h> > #define FREEZING_PT 36.0f #define FREEZING_PT 32.0f
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-04-03 16:56 -0400 |
| Message-ID | <4F7B63ED.3090002@verizon.net> |
| In reply to | #19310 |
On 04/03/2012 04:52 PM, Ike Naar wrote: > On 2012-04-03, Robert <rkfb1965@googlemail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-04-03 22:11 +0100 |
| Message-ID | <jlfp3h$gv0$1@dont-email.me> |
| In reply to | #19312 |
"James Kuyper" <jameskuyper@verizon.net> wrote in message news:4F7B63ED.3090002@verizon.net... > On 04/03/2012 04:52 PM, Ike Naar wrote: >> On 2012-04-03, Robert <rkfb1965@googlemail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-04-03 21:53 +0100 |
| Message-ID | <jlfo0b$9mv$1@dont-email.me> |
| In reply to | #19306 |
"Robert" <rkfb1965@googlemail.com> wrote in message news:9u15bsF7g3U1@mid.individual.net... > #define FREEZING_PT 36.0f Try 32.0. -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Robert <rkfb1965@googlemail.com> |
|---|---|
| Date | 2012-04-03 22:56 +0000 |
| Message-ID | <9u1a01F8g7U1@mid.individual.net> |
| In reply to | #19311 |
BartC wrote: > "Robert" <rkfb1965@googlemail.com> wrote in message > news:9u15bsF7g3U1@mid.individual.net... > >> #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
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2012-04-03 20:05 -0700 |
| Message-ID | <udenn7ls59hsp244mm5k6am4i3mv0ml5e5@4ax.com> |
| In reply to | #19306 |
On Tue, 03 Apr 2012 21:37:08 +0000, Robert <rkfb1965@googlemail.com>
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
[toc] | [prev] | [next] | [standalone]
| From | Martin Ambuhl <mambuhl@earthlink.net> |
|---|---|
| Date | 2012-04-04 00:38 -0400 |
| Message-ID | <SJCdnY5g5rPDTebSnZ2dnUVZ_tGdnZ2d@earthlink.com> |
| In reply to | #19306 |
Robert wrote on Tuesday, April 03, 2012 05:37 PM (comp.lang.c):
> (Copy/paste)
>
> /* Converts a Fahrenheit temperature to Celcius */
>
> #include <stdio.h>
> #define FREEZING_PT 36.0f
^^^^
Did you mean this?
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-04-03 21:04 +0100 |
| Message-ID | <jlfl5o$jn6$1@dont-email.me> |
| In reply to | #19284 |
"rkfb" <rkfb@rebelCode.slacknet> wrote in message
news:alpine.LNX.2.00.1204031912440.4639@rebelCode.slacknet...
> 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
[toc] | [prev] | [next] | [standalone]
| From | Robert <rkfb1965@googlemail.com> |
|---|---|
| Date | 2012-04-03 21:38 +0000 |
| Message-ID | <9u15esF7g3U2@mid.individual.net> |
| In reply to | #19300 |
BartC wrote:
> "rkfb" <rkfb@rebelCode.slacknet> wrote in message
> news:alpine.LNX.2.00.1204031912440.4639@rebelCode.slacknet...
>> 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
[toc] | [prev] | [next] | [standalone]
| From | Kleuske <kleuske@nowhere.net> |
|---|---|
| Date | 2012-04-03 23:09 +0000 |
| Message-ID | <jlfvv2$ep4$2@dont-email.me> |
| In reply to | #19284 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-04-04 04:42 +0100 |
| Message-ID | <0.1b49c6372ec9f7615cb6.20120404044225BST.87mx6sgo2m.fsf@bsb.me.uk> |
| In reply to | #19318 |
Kleuske <kleuske@nowhere.net> 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web