C/C++ 取整函数ceil(),floor(),向上取整,向下取整

本文详细介绍了C/C++中的取整函数ceil()和floor()的用法,并通过实例展示了如何向上取整和向下取整。此外还涉及了除法和模运算的基本规则以及四舍五入的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:

C/C++ 取整函数ceil(),floor()

C/C++ 取整函数ceil(),floor()

#include <math.h> double floor(double  x ); 
float floorf(float x); 
long double floorl(long double x);

double floor(double x);

double ceil(double x);


使用floor函数。floor(x)返回的是小于或等于x的最大整数。
如:     floor(10.5) == 10    floor(-10.5) == -11


使用ceil函数。ceil(x)返回的是大于x的最小整数。
如:     ceil(10.5) == 11    ceil(-10.5) ==-10

    
floor()是向负无穷大舍入,floor(-10.5) == -11;
ceil()是向正无穷大舍入,ceil(-10.5) == -10


 1. /

    //Test "/"
    cout << "Test \"/\"!" << endl;
    cout << "7   / 2   = " << 7/2 << endl;      //3
    cout << "7   / 2.0 = " << 7/2.0 << endl;    //3.5
    cout << "7.0 / 2   = " << 7.0/2 << endl;    //3.5
    cout << "7.0 / 2.0 = " << 7.0/2.0 << endl; //3.5
    cout << "7   / 3   = " << 7/3 << endl;      //2
    cout << endl;

2. %
    //Test "%"
    cout << "Test \"%\"!" << endl;
    cout << "9   % 3   = " << 9%3 << endl;      //0
    cout << "9   % 4   = " << 9%4 << endl;      //1
    //cout << "9.0 % 3   = " << 9.0%3 << endl;
    //cout << "9   % 3.0 = " << 9%3.0 << endl;
    cout << endl;

3. 四舍五入
    //Test round()
    cout << "Test \"四舍五入\"!" << endl;
    double dRoundA = 1.4;
    double dRoundB = 1.6;
    double dRoundLowA = -1.4;
    double dRoundLowB = -1.6;
    double dRoundLowC = 0.0;
    cout << dRoundA << " = " << RoundEx(dRoundA) << endl;         //1
    cout << dRoundB << " = " << RoundEx(dRoundB) << endl;         //2
    cout << dRoundLowA << " = " << RoundEx(dRoundLowA) << endl;   //-1
    cout << dRoundLowB << " = " << RoundEx(dRoundLowB) << endl;   //-2
    cout << dRoundLowC << " = " << RoundEx(dRoundLowC) << endl;   //0
    cout << endl;

double RoundEx(const double& dInput)
{
    double dIn = dInput;
    if (dInput >= 0.0)    //???
    {
        return int(dIn + 0.5);
    } 
    else
    {
        return int(dIn - 0.5);
    }
}

4. ceil() 向上取整
    //Test ceil() 向上取整
    cout << "Test ceil() 向上取整!" << endl; 
    cout << "ceil 1.2 = " << ceil(1.2) << endl;      //2
    cout << "ceil 1.8 = " << ceil(1.8) << endl;      //2
    cout << "ceil -1.2 = " << ceil(-1.2) << endl;    //-1
    cout << "ceil -1.8 = " << ceil(-1.8) << endl;    //-1
    cout << "ceil 0.0 = " << ceil(0.0) << endl;      //0
    cout << endl;

5. floor() 向下取整
    //Test floor() 向下取整
    cout << "Test floor() 向下取整!" << endl;
    cout << "floor 1.2 = " << floor(1.2) << endl;    //1
    cout << "floor 1.8 = " << floor(1.8) << endl;    //1
    cout << "floor -1.2 = " << floor(-1.2) << endl; //-2
    cout << "floor -1.8 = " << floor(-1.8) << endl; //-2
    cout << "floor 0.0 = " << floor(0.0) << endl;    //0
    cout << endl;

16.1.21:标红的部分需要记住,便于理解记忆

### 如何通过飞牛代理访问 Docker 服务器 要实现通过飞牛代理访问 Docker 服务器,可以按照以下方法完成配置: #### 1. 安装并设置飞牛 APP 确保目标设备已安装最新版本的飞牛 APP,并成功登录账户。在应用内创建一个新的虚拟局域网络 (VLAN),并将该 VLAN 的 IP 地址分配给需要被访问的目标 Docker 主机[^4]。 #### 2. 配置 Docker 主机以支持远程访问 为了使 Docker 支持外部客户端连接,需修改其默认监听地址为 `0.0.0.0` 而不是仅限本地回环接口 (`localhost`)。编辑 `/etc/docker/daemon.json` 文件(如果不存在则新建),加入如下内容: ```json { "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"] } ``` 保存更改后重新加载服务配置以及重启 Docker 进程来生效新设定: ```bash sudo systemctl daemon-reload && sudo systemctl restart docker ``` #### 3. 开启防火墙端口放行 确认开放了用于接收来自互联网请求的服务端口号,在本例中即 TCP 协议下的 2375 号端口。可以通过命令行工具 ufw 或 iptables 实现此目的;这里给出基于 UFW(Uncomplicated Firewall) 的例子: ```bash sudo ufw allow 2375/tcp sudo ufw reload ``` #### 4. 使用飞牛建立安全通道 回到飞牛应用程序界面下找到刚才所设之 VLAN 条目,点击进入详情页面启用 Fn Connect 功能。此时会生成一条专属链接供其他成员加入同一私密空间共享资源。 #### 5. 测试连通性 最后一步是从另一台计算机尝试利用上述方式构建起来的安全隧道去触达远端运行中的容器实例。假设我们已经知道对方机器上的公网 IPv6 地址或者由飞牛提供的内部静态IP,则可以直接采用标准形式调用 API 接口发起交互动作比如列出当前所有活动状态的任务列表等等。 ```bash export DOCKER_HOST=tcp://<your-fn-connect-ip>:2375 docker ps ``` 以上步骤完成后应该能够顺利经由飞牛中介机制达成跨地域间无缝协作体验而无需担心传统 NAT 穿越难题所带来的困扰。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值