作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将除以某个正常数的实数值与最近的较小整数对齐,而不管它们的符号如何。示例是(这里的反斜杠代表我想要的运算符)
21,5 \ 2 = 10
-21,5 \ 2 = -11
52,3 \ 2 = 26
-52,3 \ 2 = -27
是否有一个简短的操作符可以做到这一点?通常的“斜杠”(“/”)运算符在 C++ 中向零舍入(前段时间已成为标准)(例如 -52.6/2 = -26)。
最佳答案
std::floor
将解决您的问题。
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
float i = -21.5,b=2;
int c = std::floor(i/b);
cout << c << endl;
i = 21.5,b=2;
c = std::floor(i/b);
cout << c << endl;
int a = 11,b1 =2;
c = std::floor(a/b1);
cout << c << endl;
a = -11;
b =2.1;
c = std::floor(a/b);
cout << c << endl;
return 0;
}
输出:
-11
10
5
-6
关于c++ - 向下舍入而不是截断的除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39612730/
我是一名优秀的程序员,十分优秀!