Approximation and accuracy-answers to exercises in mad man C 15 (chapter 3 Exercise 5)

Source: Internet
Author: User

A farmer left 15 cows after his death. He wrote in his suicide note: "Wife: give half of the cows and half of the cows; Eldest Son: give half of the cows and half of the cows; sub-child: give half of the remaining cattle plus half of the head; long female: Give the remaining half. "The programming girl got a few cows.

This is a simple elementary arithmetic problem:
Half of a 15-headed ox is 7 and 1/2, plus half of the ox is 8, which is the result of his wife. Seven remaining
Half of the seven cows are 3 and 1/2, and the other half is 4, which is the result of the eldest son. 3 remaining
Half of the three cows are 1 and 1/2, and the other half is 2. This is the sub-income. 1 remaining
Therefore, the female's income is 1.

However, if you write the following code, you can get up to 60 points.

# Include <stdio. h> # include <stdlib. h> # define zongshu 15. // Total number: 15 cows left # define fenpei_bl. 5 // allocation ratio: half # define ewai_tj. 5 // Add: int main (void) {double qizi, Zhangzi, cizi, zhangnv; // double shengyu = zongshu; // The remaining qizi = shengyu * fenpei_bl + ewai_tj; // the wife's shengyu-= qizi; // the remaining number of Zhangzi = shengyu * fenpei_bl + ewai_tj; // shengyu-= Zhangzi obtained by the eldest son; // the remaining number cizi = shengyu * fenpei_bl + ewai_tj; // The shengyu-= cizi obtained by the second son; // the remaining number is zhangnv = shengyu; // female: the last remaining printf ("female gets % f \ n", zhangnv); System ("pause"); Return 0 ;}

Output: female gets 1.000000 cows

Because, first, this result only indicates that the ox number obtained by the female is approximately 1; second, the Code does not actually implement the preceding arithmetic operation. For example
Qizi = shengyu * fenpei_bl + ewai_tj;
The meaning is only an approximate operation of some approximate values, rather than an exact operation in the previous arithmetic operation. Because of its nature and general situation, real-time floating-point data is only an approximate representation of the real number, which means that real-time floating-point operations are only an approximate operation. In this topic, the accuracy of the approximation is very high, and the calculation result is exactly the same as that of the exact result. If the program is regarded as a precise simulation of the PEN calculation process, it is clear that the previous Code does not meet the requirements.
In a computer, only the integer type represents an approximate subset of an integer set. Therefore, if you want to accurately simulate the pen calculation process, you can only use the integer type. However, the pen calculation process involves scores. In mathematics, scores are also accurate representations, but in C language, there is no corresponding "score type ".
What if there is no corresponding data type? The answer is simple: This data type is created without this type. It is the feature and charm of C language that provides a vast space for creativity, and it is just one of the fun of programming.
Because the score is composed of two parts: numerator and denominator, while numerator and denominator are integers, we can use two integer data to represent the score. For such data, the C language does not provide direct computation, and this "score" operation needs to be simulated using the computation provided by the C language.
For example, if a/B + C/D is calculated, it cannot be completed through a "+" operation, the numerator "B * C + D * C" and the denominator "A * C" of the sum can only be calculated twice ".
The Code obtained in this way is

# Include <stdio. h> # include <stdlib. h> # define zongshu_fz 15 // total number of molecules # define zongshu_fm 1 // denominator of the total number # define fenpei_bl_fz 1 // allocation ratio of molecules # define fenpei_bl_fm 2 // denominator of the allocation ratio # define ewai_tj_fz 1 // additional molecules added # define ewai_tj_fm 2 // additional denominator int main (void) {int qizi_fm, cosine, // denominator and numerator zhangzi_fm, zhangzi_fz, // denominator and numerator cizi_fm, cizi_fz, // denominator and numerator zhangnv_fm, zhangnv_fz; // denominator and numerator int shengyu_fm = zongshu_fm, // The denominator shengyu_fz = zongshu_fz; // The remaining number of molecules qizi_fz = shengyu_fz * ewai_tj_fm // the wife's income + shengyu_fm * Comment * comment; qizi_fm = shengyu_fm * fenpei_bl_fm * ewai_tj_fm; token = shengyu_fz * qizi_fm-token * shengyu_fm; // the remaining number shengyu_fm * = qizi_fm; token = token * ewai_tj_fm // The eldest son's income + shengyu_fm * token; zhangzi_fm = shengyu_fm * fenpei_bl_fm * ewai_tj_fm; shengyu_fz = shengyu_fz * zhangzi_fm-zhangzi_fz * shengyu_fm; // The remaining shengyu_fm * = zhangzi_fm; cizi_fz = shengyu_fz * ewai_tj_fm // sub-income + shengyu_fm * fenpei_bl_fm * fenpei_bl_fz; cizi_fm = shengyu_fm * fenpei_bl_fm * ewai_tj_fm; shengyu_fz = shengyu_fz * cizi_fm-cizi_fz * shengyu_fm; // remaining shengyu_fm * = cizi_fm; zhangnv_fz = shengyu_fz; // zhangnv_fm = shengyu_fm; printf ("female gets % d again % d/% d Head ox \ n", shengyu_fz/shengyu_fm, shengyu_fz % shengyu_fm, shengyu_fm); System ("pause "); return 0 ;}

Output: female gets 1 and 0/16384 cows

This is a precise result.
[Note: after learning the control statement and function theory, the next code can be further improved .]

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.