C primer plus 03-data type (two error-prone applets)

Source: Internet
Author: User
Tags float number
First Program

#include<stdio.h>int main(void){float a,b,c;b = 2.0e4 + 1.0;a = b - 1.0;c = b - 2.0e4;printf("%f %f %f\n",a,b,c);b = 2.0e10 + 1.0;a = b - 1.0;c = b - 2.0e10;printf("%f %f %f\n",a,b,c);b = 2.0e20 + 1.0;a = b - 1.0;c = b - 2.0e20;printf("%f %f %f\n",a,b,c);getchar();return 0;}

The running result is:
20000.000000 20001.000000 1.000000
20000000000.000000 20000000000.000000 0.000000
200000004008175470000.000000 200000004008175470000.000000 4008175468544.000000
The program is explained as follows:
The results of different compilers are different. The strange result is that the computer lacks enough digits in decimal order to perform the correct operation.
The first output is obviously the correct result.
The second output is incorrect. That's right, because their stored values have exceeded the maximum range that can be expressed. Therefore, for scientific and technological methods, only the index and base number are stored, the precision is obviously lost. Therefore, 20000000000.000000 20000000000.000000 0.000000 is output.
The third output, if correct calculation is required, the computer needs to store 21-bit numbers, but the float number only has 6.7-bit valid numbers, so an error occurs, however, if you change E20 to E4, all outputs are correct, because float can store 5 digits. I think it's all wrong because my system is a 32-bit system with a maximum value of 2 to the power of 32. When the data index is 20, it has exceeded the maximum floating point number that the computer system can represent) so there will be some cloud migration errors, and I don't understand either of them. Which of the following Eldest Brother knows how to give me some advice.
It should also be noted that the efficiency is not only slow but also low in precision when processing floating point data. Of course, we have developed a dedicated CPU for floating point operations, which improves the efficiency. You can use this program to test other data.

The second program:

#include<stdio.h>int main(void){float salary;printf("\aEnter your salary:");printf(" $_______\b\b\b\b\b\b\b");scanf("%f",&salary);printf("\n\t$%0.2f a month is $%0.2f a year .",salary,salary * 12);printf("\rGee!\n");return 0;}

The running result is as follows:
Enter your salary: $3000 ___
[This is a blank line]
Gee! $3000.00 a month is $36000.00 a year.
Press any key to continue
This program is very simple. Let me talk about it a bit, the \ B Return key, the \ r return character, and the \ t tab.

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.