当我遇到以下问题时,我在C中编写此代码.
#include <stdio.h>
int main()
{
int i=2;
int j=3;
int k,l;
float a,b;
k=i/j*j;
l=j/i*i;
a=i/j*j;
b=j/i*i;
printf("%d %d %f %f\n",k,l,a,b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我为什么代码为第一个和第三个变量(k和a)返回零?
可能重复:
int除以unsigned int导致翻转
嗨,我正在做以下事情:
struct coord{
int col;
};
int main(int argc, char* argv[]) {
coord c;
c.col = 0;
std::vector<coord> v;
for(int i = 0; i < 5; i++){
v.push_back(coord());
}
c.col += -13;
cout << " c.col is " << c.col << endl;
cout << " v size is " << v.size() << endl;
c.col /= v.size();
cout << c.col << endl;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
c.col is -13
v size is 5
858993456
Run Code Online (Sandbox Code Playgroud)
但是,如果我将分割线更改为c.col /= ((int)v.size());I,则获得预期的输出:
c.col …Run Code Online (Sandbox Code Playgroud) 我有以下代码INT_MIN/-1.我希望这可能是INT_MAX + 1(或翻转时为0).但是,我得到的实际结果是INT_MIN.这是我的测试代码:
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <stdio.h>
#include <limits.h>
using namespace std;
int main()
{
int min=INT_MIN;
int res=min/-1;
printf("result: %i\n", res);
printf("max: %i min: %i\n", INT_MAX, INT_MIN);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此实现是特定的和/或未定义的行为吗?
在我的ASP.NET项目中,我做了一个使用Application来保存投票的调查页面.制作百分比金额时遇到问题.我尝试了很多东西.这是我的代码中有问题的部分:
double x = (count / sum) ;
double f = (count1 / sum) ;
double g = (count2 / sum) ;
double h = (count3 / sum) ;
if (sum > 0)
{
a = (int)x * 100;
b = (int)f * 100;
c = (int)g * 100;
d = (int)h * 100;
}
Run Code Online (Sandbox Code Playgroud)
我使用断点并发现问题出现在双变量中:(count/sum)无论如何都等于0.
我想在 System Verilog 中使用real数据类型使用浮点数。我尝试了以下代码,但似乎不起作用。我正在到达2.000000我期望的地方2.500000。
模块:
module real_check(input [31:0]a, [31:0]b,
output real c);
assign c = a/b;
endmodule
Run Code Online (Sandbox Code Playgroud)
试验台:
module tb_real_check();
real c;
reg[31:0] a, b;
real_check dut(a, b, c);
initial
begin
a <= 5;
b <= 2;
#50;
$display("Answer : %f ", c);
end
endmodule
Run Code Online (Sandbox Code Playgroud) floating-point verilog division integer-division system-verilog
假设我有以下列表实现:
list=^listelement
listelement=record
w:integer;
next:list;
end;
Run Code Online (Sandbox Code Playgroud)
列表表示十进制写的大数(列表1 - > 2 - > 3表示数字123).
我想要做的是将这样的数字转换为二进制表示.因此,最直接的方法是将数字除以2
问题是我很难用2算法实现除法.我理解基本的算法,例如这个 https://www.mathsisfun.com/long_division.html,但我想不出一种方法可以将其转换为代码
我将不胜感激
我正在尝试编写一个公式来计算球体的体积,用户输入半径.公式是V = (4/3)PI r*r*r.我无法弄清楚为什么我的代码只是说无论输入是什么,音量都是1.这是我正在使用的:
#include <stdio.h>
int main(void) {
float pi, r, v;
pi = 3.1416;
printf("What is the radius?\n");
scanf("%d", &r);
v = ((4/3) * pi) * r * r * r;
printf("Volume of the sphere is: %d\n", v);
if (v>10)
printf("Volume is greater than 10");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有以下 C 代码:
int count = 0; // relative time
int T1 = 20; // period 1 in ms
int T2 = 50; // period 2 in ms
int T3 = 80; // period 3 in ms
while (1) {
if (count%T1 == 0) function1();
if (count%T2 == 0) function2();
if (count%T3 == 0) function3();
count++;
if (count == T1*T2*T3) count = 0;
delay(1); // wait for 1 ms
}
Run Code Online (Sandbox Code Playgroud)
我想知道有整数除法 count%T1==0 而不是 count==T1 的原因。也许是考虑到周期 T1 可能不是整数?
先感谢您。
假设我有21875as totalQuantityNumber。
我需要totalQuantityNumber在 for 循环 ex 中拆分多天。第一天 20000 和第二天 1875。在最后的 for 循环中,我计算拆分数量的总和,以便稍后验证拆分是否正确,例如。totalSplittedQuantity += splittedQuantity.
在拆分结束时,我会验证拆分产品数量的总和是否与初始计划的总产品数量相同。totalQuantityNumber== totalSplittedQuantity它应该是21875 == 21875,但当数字是奇数时,我总是偏离一个数字。21875 == 21874. 我试图使除法小数点并在最后四舍五入,但问题仍然存在,有时结果也以一结束。21875 == 21876.
这是我在循环内的划分:
splittedQuantity = splittedDiffDuration * totalQuantityNumber/ totalDuration;
totalDuration并且splittedDiffDuration以分钟为单位。totalDuration = 120; splittedDiffDuration = 60;
基本上我从日期时间间隔(startDate,endDate)ex 循环遍历每一天。周一至周二 - 在计划的持续时间内拆分每天的数量。假设周一计划 60 分钟生产 X 数量,周二同样,60 分钟生产剩余数量。
我是编程新手,数学不太好。我的部门做错了什么?
我知道在组装时必须非常小心,即这样做:
mov ah, 10h
mov al, 00h ; dividend = 1000h
mov bl, 10h ; divisor = 10h
div bl ; Integer overflow exception, /result 100h cannot fit into al
Run Code Online (Sandbox Code Playgroud)
我已经编写了一些可能不可靠的逻辑来为除法创建一个更友好的环境:
mov ah, 10h
mov al, 00h
mov bl, 10h
TryDivide:
cmp bl,ah
jna CatchClause
div bl
clc
jmp TryEnd
CatchClause:
stc
TryEnd:
Run Code Online (Sandbox Code Playgroud)
有没有人知道类似这样的事情没有实现的技术原因,我们有例外而不是标志设置/寄存器被截断?