Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Kleuske <kleuske@nowhere.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Computed values don't tie up |
| Date | 2012-04-03 23:09 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <jlfvv2$ep4$2@dont-email.me> (permalink) |
| References | <alpine.LNX.2.00.1204031912440.4639@rebelCode.slacknet> |
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
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web