当前位置:网站首页>"C language" deep entry rounding & four functions
"C language" deep entry rounding & four functions
2022-07-28 00:53:00 【Chenze】
Hello Chenze, give me more advice

Preface
This blog will give you an in-depth understanding of what rounding is . I mentioned " depth " You can see that this article is still very detailed in explaining rounding, and there must be some knowledge points in it , But don't think that depth is to design many difficult knowledge points . In fact, they are all relatively easy knowledge points , Then don't talk too much nonsense and get to the point
integer
About " integer " It seems that we are both familiar with and unfamiliar with this word , Familiar because we often use rounding when programming . Strangeness is because I haven't thoroughly understood this rounding , Next, let's focus on rounding as a discussion .
integer ⇢ Rounding literally means very easy to understand, which is nothing more than rounding an integer 2.5 It is a floating-point number. If we round it, it is 2 The integer of . The code example is as follows ↓
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> int main(void) { int a = 2.99; int b = -2.99; printf("a=%2d\n", a); printf("b=%2d\n", b); return 0; }Running results
a = 2
b = -2
explain ⇢ In the above code, we can know that it is not according to our mathematical rounding, but no matter how many decimal places you have , This is also the reason why I take the decimal point of the variable as big as possible, that is, I want you to understand this concept very intuitively . In a word : The number after the decimal point is omitted directly .
summary ⇢「C Language 」 Rounding defaults to "0 Round to ".
⒈trunc - 0 Round to
trunc The header file for is ⇢ #include<math.h>
Expand your knowledge ⇢ about 「C Language 」 In fact, it has a rounding function. I believe many friends don't know 「C Language 」 To have this trunc Rounding function .
trunc The parameters are as follows ↓
double trunc ( double x); float truncf ( float x); long double truncl (long double x);The code example is as follows ↓
#include<stdio.h> #include<math.h> int main(void) { printf("%2f\n", trunc(2.99)); printf("%2f\n", trunc(-2.99)); printf("%2d\n", (int)trunc(2.99)); printf("%2d\n", (int)trunc(-2.99)); return 0; }Running results
2
-2
2
-2
notes ⇢ Here we need to note that the return value of the function is of floating-point type , If you use plastic printing, you need to strongly convert the type to (int)
explain ⇢ stay C The default language is 0 To round up .
⒉floor - Floor rounding
floor The header file for is ⇢ #include<math.h>
floor The parameters are as follows ↓
double floor (double x);The code example is as follows ↓
#include<stdio.h> #include<math.h> int main(void) { printf("%2d\n", (int)floor(2.99)); printf("%2d\n", (int)floor(-2.99)); printf("%2d\n", (int)floor(2.99)); printf("%2d\n", (int)floor(-2.99)); return 0; }Running results
2
-3
2
-3
notes ⇢ We need to remember that it's going to -∞ Close in the middle , From the running results of the above code, I believe you can also see that it is all the same -∞ Close in the middle .
⒊ceil- Infinite rounding
ceil The header file for is ⇢ #include<math.h>
ceil The parameters are as follows ↓
double ceil (double x);The code example is as follows ↓
#include<stdio.h> #include<math.h> int main(void) { printf("%2d\n", (int)ceil(2.99)); printf("%2d\n", (int)ceil(-2.99)); printf("%2d\n", (int)ceil(2.99)); printf("%2d\n", (int)ceil(-2.99)); return 0; }Running results
3
-2
3
-2
notes ⇢ We need to remember that it goes +∞ Close in the middle , From the running results of the above code, I believe you can also see that it is all the same +∞ Close in the middle .
⒋round- rounding
round The header file for is ⇢ #include<math.h>
round The parameters are as follows ↓
double round (double x); float roundf (float x); long double roundl (long double x);The code example is as follows ↓
#include<stdio.h> #include<math.h> int main(void) { printf("%2d\n", (int)round(2.99)); printf("%2d\n", (int)round(-2.01)); printf("%2d\n", (int)round(2.01)); printf("%2d\n", (int)round(-2.99)); return 0; }Running results
3
-3
2
-3
notes ⇢ This is the way of rounding in our mathematics .
summary - The final choice depends on your application scenario ~
* Last * ⇢ give the thumbs-up + Focus on + Collection == Learn to
边栏推荐
- The Canadian court found Meng Wanzhou guilty of "dual criminality", and the extradition procedure will continue!
- Point divide and conquer analysis
- Basic elementary function
- Jerry Zhi doesn't play hidden audio files [article]
- Interesting Huffman tree
- At least 42 employees are infected with novel coronavirus! Nokia announces closure of telecom equipment plant in India
- ASML launched the first generation HMI multi beam detector: the speed is increased by 600%, which is suitable for 5nm and more advanced processes
- Map set
- scrollview、tableView嵌套解决方案
- R language evaluates the relative importance of the predictive factors (variables, characteristics) of the regression model, scales the predictive variables of the regression model, and then construct
猜你喜欢

激活最大化
![[leetcode] 547. Number of provinces (medium)](/img/15/d49d18151c47e318fe7acabdd616e6.png)
[leetcode] 547. Number of provinces (medium)

Basic operations of MySQL database (I) --- Based on Database

Selection of FFT sampling frequency and sampling points

mysql分表之后怎么平滑上线?

map集合

A few lines of code can easily realize the real-time reasoning of paddleocr. Come and get!
![[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development](/img/17/15c759aadafc335028d37380903ee7.jpg)
[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development

Read cmake in one article

Ali Er Mian: why do we need to separate databases and tables?
随机推荐
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]
threejs个人笔记
程序员成长第三十篇:你真的懂反馈吗?
Intel AI practice day issue 56 | explore new trends in industry development
Rendering problems
数据可视化-《白蛇2:青蛇劫起》(3)
Network equipment hard core technology insider firewall and security gateway (12) the mystery of zero contact office
Network device hard core technology insider firewall and security gateway (IX) virtualization artifact (II)
Ali Er Mian: why do we need to separate databases and tables?
The server is poisoned - the dish is the original sin
Jerry's Bluetooth can only link back to the last device [article]
In the first quarter of 2020, the wearable market shipped 72.6 million units, with apple occupying nearly 30% of the market share
Recurrence of fastjson historical vulnerabilities
Can TSMC Samsung build a production line without American equipment for Huawei?
Jerry caused other messages to accumulate in the message pool [article]
加拿大法院认定孟晚舟“双重犯罪”成立,引渡程序将继续进行!
Arm releases the new a78/g78/n78 kernel! There is also a cortex-x Series CPU that supports customization
Possible reasons why there is no voltage in the corresponding channel, but the ADC value is changing greatly and is not equal to 0
Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
KMP review + AC automata Prequel

