为什么以下代码的输出等于0或serven?
cout << 7/9*9; //output 0 (zero) why?
float nine = 9;
float seven = 7;
float i = seven/nine*nine;
cout << i //output 7 Why?
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助.
如果我除以0,我会得到ZeroDivisionError,Infinity或NaN,具体取决于划分的内容.
ruby-1.9.2-p180 :018 > 0.0 / 0
=> NaN
ruby-1.9.2-p180 :020 > 3.0 / 0
=> Infinity
ruby-1.9.2-p180 :021 > 3 / 0
ZeroDivisionError: divided by 0
Run Code Online (Sandbox Code Playgroud)
我知道0.0/0不是无限(用数学术语),而3.0/0是为什么那么为什么不是3/0无穷大?为什么划分整数会引发异常,但划分浮点数不会?
如果我需要将例如7除以随机大小的随机数元素,我该怎么做?
所以有时我会[3,4],有时[2,3,1],有时[2,2,1,1,0,1]?
我想这很简单,但我似乎无法得到结果.这是我试图以代码方式做的事情(不起作用):
def split_big_num(num):
partition = randint(1,int(4))
piece = randint(1,int(num))
result = []
for i in range(partition):
element = num-piece
result.append(element)
piece = randint(0,element)
#What's next?
if num - piece == 0:
return result
return result
Run Code Online (Sandbox Code Playgroud)
编辑:每个结果数应小于初始数,零数应不小于分区数.
我有以下变量:
int first = 0;
int end = 0;
Run Code Online (Sandbox Code Playgroud)
在公共课上宣布.
在一个方法中:
double diff = end / first;
double finaldiff = 1 - diff;
Run Code Online (Sandbox Code Playgroud)
该end变量对System.out.printlnIS 527的first是480.
为什么差异的答案出现了1?它应该是1.097916667,我认为使用a double会使我计算成小数?
大约半个小时想"我做错了什么!?" 在5行代码..因为Python3以某种方式舍入大整数.任何人都知道为什么会出现这样的问题:
Python2:
int(6366805760909027985741435139224001 # This is 7**40.
/ 7) == 909543680129861140820205019889143 # 7**39
Run Code Online (Sandbox Code Playgroud)
Python3:
int(6366805760909027985741435139224001
/ 7) == 909543680129861204865300750663680 # I have no idea what this is.
Run Code Online (Sandbox Code Playgroud) 我的任务是编写一个显示程序PSP线性地址的程序.我写了以下内容:
ORG 256
mov dx,Msg
mov ah,09h ;DOS.WriteStringToStandardOutput
int 21h
mov ax,ds
mov dx,16
mul dx ; -> Linear address is now in DX:AX
???
mov ax,4C00h ;DOS.TerminateWithExitCode
int 21h
; ------------------------------
Msg: db 'PSP is at linear address $'
Run Code Online (Sandbox Code Playgroud)
我搜索了DOS api(使用Ralph Brown的中断列表)并没有找到输出数字的单个函数!我错过了吗,我该怎么办?
我想以DX:AX十进制显示数字.
我如何告诉MSVC编译器使用64位/ 32位除法运算来为x86-64目标计算以下函数的结果:
#include <stdint.h>
uint32_t ScaledDiv(uint32_t a, uint32_t b)
{
if (a > b)
return ((uint64_t)b<<32) / a; //Yes, this must be casted because the result of b<<32 is undefined
else
return uint32_t(-1);
}
Run Code Online (Sandbox Code Playgroud)
我希望代码在if语句为true时编译为使用64位/ 32位除法运算,例如:
; Assume arguments on entry are: Dividend in EDX, Divisor in ECX
mov edx, edx ;A dummy instruction to indicate that the dividend is already where it is supposed to be
xor eax,eax
div ecx ; EAX = EDX:EAX / ECX
Run Code Online (Sandbox Code Playgroud)
...但是x64 …
我在我应该修复的程序中发现了一个函数,该mod函数定义了一个函数:
int mod(int a, int b)
{
int i = a%b;
if(i<0) i+=b;
return i;
}
Run Code Online (Sandbox Code Playgroud)
有人告诉我,a并b会一直在路上...阳性
嗯?if(i<0)?
论点是,那
模运算的结果是一个等价类,可以选择该类的任何成员作为代表
并且只是作为事后的想法
...; 然而,通常的代表是最小的正余数,属于该类的最小非负整数,即欧几里德除法的余数。然而,其他约定也是可能的。
这意味着6 % 7可以返回6(到目前为止很好),但也可以返回-1. 嗯……真的吗?(让我们忽略所呈现的实现无法处理所有情况的事实。)
我知道模运算就是这样在数学上是正确的。但是后来有人告诉我,C%实际上“没有实现模运算符,而是实现了余数”。
那么,C 是如何定义%运算符的呢?
在 C-Draft 我只找到
/ 运算符的结果是第一个操作数除以第二个操作数的商;% 运算符的结果是余数。在这两个操作中,如果第二个操作数的值为零,则行为未定义。
这是否意味着,6 % 7总是这样6?或者也可以-1?
unsigned int a=200;
//mov dword ptr [a],0C8h
a >>= 1;
//mov eax,dword ptr [a]
//shr eax,1
//mov dword ptr [a],eax
a /= 2;
//mov eax,dword ptr [a]
//shr eax,1
//mov dword ptr [a],eax
int b = -200;
//mov dword ptr [b],0FFFFFF38h
b /= 2;
//mov eax,dword ptr [b]
//cdq
//sub eax,edx
//sar eax,1
//mov dword ptr [b],eax
b >>= 1;
//mov eax,dword ptr [b]
//sar eax,1
//mov dword ptr [b],eax
Run Code Online (Sandbox Code Playgroud)
我正在使用 msvc,// 是该 C 语句的程序集。
为什么signed int/=2不同于>>=1 …
在这行代码中,如何解决“浮点上下文中的整数除法”警告:
int fps = 60;
double timePerTick = 1000000000 / fps;
Run Code Online (Sandbox Code Playgroud) java floating-point intellij-idea integer-division suppress-warnings
integer-division ×10
c ×3
assembly ×2
division ×2
java ×2
python ×2
c++ ×1
dos ×1
largenumber ×1
math ×1
modulo ×1
python-3.x ×1
random ×1
rounding ×1
ruby ×1
visual-c++ ×1
x86 ×1
x86-16 ×1
x86-64 ×1