Python Modulo复数

时间:2017-10-21 12:06:02

标签: python operators complex-numbers

 List=([1+3j,12j,-1j,12+3j,13,12j,12-3j,-45j,-45-5j,0,-323j,-2+3j])

这是我的功能,我称之为:

kernel void sum_float (global float* sum, global float* values)
{
  float sum_val = 0.0f;

  for (size_t i = 0u; i < get_num_groups(0); ++i)
  {
    size_t index = get_local_id(0) + i * get_enqueued_local_size(0);
    float value = work_group_reduce_add(values[index]);
    sum_val += work_group_broadcast(value, 0u);
  }

  sum[0] = sum_val;
}

该列表定义为:

kernel void sum_float2 (global float* sum, global float* values)
{
      float sum_partial = work_group_reduce_add(values[get_global_id(0)]);
      if(get_local_id(0)==0)
        sum[get_group_id(0)] = sum_partial; 
}

为什么它打印的所有数字不仅是模数<10

的数字

1 个答案:

答案 0 :(得分:0)

你可以这样做:

op = {'<': '__lt__', '>': '__gt__', '<=': '__le__', '>=': '__ge__'}

def print_numbers_modulo(List,Operator,ModuloNumber):
    CheckIfPrintedNumber=False
    for i in range(0,len(List)):
        if getattr(abs(List[i]), op[Operator])(ModuloNumber):
            print(List[i])
            CheckIfPrintedNumber = True
    if (CheckIfPrintedNumber == False):
        print("There are no numbers having modulo",Operator,ModuloNumber)
List=([1+3j,12j,-1j,12+3j,13,12j,12-3j,-45j,-45-5j,0,-323j,-2+3j])

print_numbers_modulo(List,'<',10) 

op是一个字典,它将您的运算符“转换”为float类的相应魔术方法,作为字符串。 然后,您可以使用getattrabs(List[i])上调用该方法,并将ModuloNumber作为参数:

getattr(abs(List[i]), op[Operator])(ModuloNumber)

例如,如果Operator = "<",则相当于:

abs(List[i]).__lt__(ModuloNumber)