当前位置:网站首页>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 .
边栏推荐
- Huawei equipment is configured with IP and virtual private network hybrid FRR
- 如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
- Huawei equipment is configured with CE dual attribution
- Leetcode 93 recovery IP address
- 记录:如何解决MultipartFile类的transferTo()上传图片报“系统找不到指定的路径“问题【亲测有效】
- Barrykay electronics rushes to the scientific innovation board: it is planned to raise 360million yuan. Mr. and Mrs. Wang Binhua are the major shareholders
- Understand speech denoising
- Restrict cell input type and display format in CXGRID control
- ROS learning-8 pit for custom action programming
- 1000粉丝啦~
猜你喜欢

如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
![Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]](/img/39/d5c4d424a160635791c4645d6f2e10.png)
Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]

Port mapping between two computers on different LANs (anydesk)

ROS learning -5 how function packs with the same name work (workspace coverage)
![[work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip](/img/d1/c4776e3db1b7560331fa569c40831a.jpg)
[work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip

4.11 introduction to firmware image package
![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]](/img/c5/f17333cdb72a1ce09aa54e38dd0a8c.png)
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]

STM32 sensorless brushless motor drive
![[pytorch]fixmatch code explanation (super detailed)](/img/22/66703bea0f8ee40eceb0687fcb3ad2.jpg)
[pytorch]fixmatch code explanation (super detailed)

Chapter7-11_ Deep Learning for Question Answering (2/2)
随机推荐
Port mapping between two computers on different LANs (anydesk)
STM32 steering gear controller
Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path
Barrykay electronics rushes to the scientific innovation board: it is planned to raise 360million yuan. Mr. and Mrs. Wang Binhua are the major shareholders
Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
The execution results of i+=2 and i++ i++ under synchronized are different
Huawei equipment is configured with IP and virtual private network hybrid FRR
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
LeetCode每日一题——890. 查找和替换模式
Installing Oracle with docker for Mac
Luzhengyao, who has entered the prefabricated vegetable track, still needs to stop being impatient
SWD debugging mode of stm32
Basic exercises of test questions letter graphics ※
Basic exercise of test questions Yanghui triangle (two-dimensional array and shallow copy)
Common web page status return code crawler
ROS learning-7 error in custom message or service reference header file
Test questions basic exercise 01 string
Number of special palindromes in basic exercise of test questions
[51nod.3210] binary Statistics (bit operation)
Review the history of various versions of ITIL, and find the key points for the development of enterprise operation and maintenance