当前位置:网站首页>The precision of C language printf output floating point numbers
The precision of C language printf output floating point numbers
2022-06-13 02:19:00 【Researcher-Du】
Believe to learn C People who speak English are right printf This function is very familiar , But many people may not be clear about some problems . Recently 《 numerical analysis 》, This makes me have a new understanding of numerical accuracy , Or that sentence, mathematics is the most straightforward weapon , There is no mathematical basis but to stay on the surface , It's hard to know why !
Let's look at the following cases :
float f = 0.3;
printf("%f",f);
Yes, you can output :0.300000
This is a C The default output of the language 6 position ,%f The output of makes you feel completely OK , wait , It doesn't seem right , It doesn't mean that computers can't accurately represent floating-point numbers ? How can this be expressed exactly ? wait a moment , Look at the following code snippet .
float f = 0.3;
printf("%.8f",f);
The output here is :0.30000001
Finally showed his feet , Sure enough, it's still not accurate , But why is this result ? So let's look down
double f = 0.3;
printf("%.8lf",f);
The output here is :0.30000000
That's strange , Here is the float Instead of double Why is it right ? Output more bits ?
double f = 0.3;
printf("%.18lf",f);
The output here is :0.299999999999999990
Now it's back to the imprecise value , Why is that ?
Let's talk about this problem :
We might as well put 0.3 Convert to binary to see what it looks like , Recommend a website that can realize multiple hexadecimal conversions :http://www.sojson.com/hexconvert.html
0.3 The binary result of is :0.0100110011001100110011001100110011001100110011001101
Of course, there are many more , To be exact, it should be an infinite decimal . according to IEEE754 standard , For single precision floating point numbers , The mantissa is 23 position , So intercept 23 Bit binary , Of course, the first one from left to right is not 0 The numbers start ,
therefore 0.3 Of float The mantissa of is :0.010011001100110011001101
Notice that the last one here is 1, instead of 0, In the original binary string is 0, The principle of rounding is adopted here , recall , stay 10 For example 0.57 The result of rounding to one decimal place is 0.6, That's because No 2 The decimal is 7, More than the 5 So in 1 position , In binary, the next bit is 1 still 0, If it is 1 Carry on . The... In the original binary string 24 Position as 1, So carry to 23 position , therefore 23 A into 1.
So let's move on 0.3 Of float The mantissa of is 0.010011001100110011001101 Convert to 10 Let's see .
The result is :0.300000011920928955078125
That's why
printf("%f",f); // You can get 0.300000
printf("%.8f",f); // You can get 0.30000001
You can output more digits :
printf("%.15f",f); // You can get 0.300000011920929
You can actually see that , Here the output is also rounded off 0.300000011920928955078125 Retain 15 One decimal place to get 0.300000011920929
As for the 4 Why is the result of double precision output less than 0.3, That's because its mantissa has 52 position , So when intercepting binary bits , because 53 Is it 0, therefore 52 The digits remain unchanged , Actually, it is smaller than the original number , because 53 There are still a lot of digits after bits 1 There is .
Then there is another question , Why?
double f = 0.3;
printf("%.8lf",f);
The output is :0.30000000
instead of :0.29999999
This is because C Language in output processing , Rounding is used again . That's why we use %f Output 0.3 Can see 0.30000 Why .
边栏推荐
- Sensorless / inductive manufacturing of brushless motor drive board based on stm32
- uniapp 预览功能
- 传感器:MQ-5燃气模块测量燃气值(底部附代码)
- Paper reading - beat tracking by dynamic programming
- [pytorch]fixmatch code explanation (super detailed)
- 华为设备配置IP和虚拟专用网混合FRR
- How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]
- AutoX. JS invitation code
- Paper reading - group normalization
- synchronized下的 i+=2 和 i++ i++执行结果居然不一样
猜你喜欢

Why is "iFLYTEK Super Brain 2030 plan" more worthy of expectation than "pure" virtual human

What did Hello travel do right for 500million users in five years?

1、 Set up Django automation platform (realize one click SQL execution)

What are the differences in cache/tlb?

I didn't expect that the index occupies several times as much space as the data MySQL queries the space occupied by each table in the database, and the space occupied by data and indexes. It is used i

Yovo3 and yovo3 tiny structure diagram

Chapter7-13_ Dialogue State Tracking (as Question Answering)

Application circuit and understanding of BAT54C as power supply protection

Ruixing coffee moves towards "national consumption"

Solution of depth learning for 3D anisotropic images
随机推荐
Mac下搭建MySQL环境
Understand HMM
How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]
华为设备配置私网IP路由FRR
1000 fans ~
Solution of depth learning for 3D anisotropic images
[keras] generator for 3D u-net source code analysis py
ROS learning-6 detailed explanation of publisher programming syntax
传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)
华为设备配置IP和虚拟专用网混合FRR
[keras learning]fit_ Generator analysis and complete examples
反爬虫策略(ip代理、设置随机休眠时间、哔哩哔哩视频信息爬取、真实URL的获取、特殊字符的处理、时间戳的处理、多线程处理)
About the fact that I gave up the course of "Guyue private room course ROS manipulator development from introduction to actual combat" halfway
[analysis notes] source code analysis of siliconlabs efr32bg22 Bluetooth mesh sensorclient
[pytorch] kaggle image classification competition arcface + bounding box code learning
Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation
[open source] libinimini: a minimalist ini parsing library for single chip computers
Paper reading - jukebox: a generic model for music
When AI meets music, iFLYTEK music leads the industry reform with technology
华为设备配置双反射器优化虚拟专用网骨干层