[英]Python interpreting equal values as non-equal
For this part of the code, which is inside a for loop 对于这部分代码,它位于for循环内
s = 'cl_num = %d, prev_cl_num = %d\n' % (cl_num, prev_cl_num);
fd.write( s );
if cl_num != prev_cl_num:
bb.instructions[i].is_cache_miss = 1;
s = 'instruction %x is cache miss, cl_num = %d, prev_cl_num = %d, base_cache_line = %d\n' % (bb.instructions[i].address, cl_num, prev_cl_num, base_cache_line);
fd.write( s );
bb.instructions[i].cache_line = cl_num - base_cache_line;
prev_cl_num = cl_num;
I get an output, as this in the fd file, 我得到一个输出,就像在fd文件中一样,
cl_num = 65557, prev_cl_num = 65557
instruction 400558 is cache miss, cl_num = 65557, prev_cl_num = 65557, base_cache_line = 65557
cl_num = 65557, prev_cl_num = 65557
instruction 400560 is cache miss, cl_num = 65557, prev_cl_num = 65557, base_cache_line = 65557
cl_num = 65557, prev_cl_num = 65557
instruction 400568 is cache miss, cl_num = 65557, prev_cl_num = 65557, base_cache_line = 65557
cl_num = 65557, prev_cl_num = 65557
instruction 400570 is cache miss, cl_num = 65557, prev_cl_num = 65557, base_cache_line = 65557
You see, the condition cl_num != prev_cl_num
gets evaluated to true
even when cl_num
is equal to prev_cl_num
. 您会看到,即使
cl_num
等于prev_cl_num
,条件cl_num != prev_cl_num
被评估为true
。 Why so? 为什么这样?
You have floating point values, but only write the integer portion to your file: 您具有浮点值,但仅将整数部分写入文件:
>>> '%d' % 3.3
'3'
Note how the .3
decimal portion has been ignored; 注意
.3
小数部分如何被忽略; %d
calls int()
on the interpolated value. %d
对插值调用int()
。
When writing debugging values, always use repr()
, or %r
in formatting: 编写调试值时,请始终在格式中使用
repr()
或%r
:
s = 'cl_num = %r, prev_cl_num = %r\n' % (cl_num, prev_cl_num);
repr()
on float values formats them as if using a %17g
formatter; 浮点值的
repr()
格式化它们就像使用%17g
格式化程序一样; 17 decimals are shown, scientific notation is used when the exponent is 17 or up. 显示的是17位小数,当指数为17或更高时,将使用科学计数法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.