Skip to content

condition (if a or b:) is always true #7

Description

@PSLLSP

I have found that condition

  if (x == 0x08) or (x == 0x04):  # BUG is HERE!!
      seed += 1

is always true

When I rewrite that condition to two simpler conditions, it works OK.

  if x == 0x08:
      seed += 1
  elif x == 0x04:
      seed += 1

A bug demo, "monkey piano" has two versions of simple pseudo-random generators rnd1() and rnd2(). These functions differ in condition, function rnd1() is buggy and always sets the lowest bit (seed +=1). Function rnd2() works, it generates sequence of 15 numbers.

#-------------Setup----------------

import Ed

Ed.EdisonVersion = Ed.V2

Ed.DistanceUnits = Ed.CM
Ed.Tempo = Ed.TEMPO_MEDIUM

#--------Your code below-----------

def rnd1():
  global seed
  if seed == 0:
    seed = 1
  x = seed & 0x0c
  seed += seed
  if (x == 0x08) or (x == 0x04):  # BUG is HERE, condition is always TRUE
      seed += 1
  seed &= 0x0f
  return seed

def rnd2():
  global seed
  if seed == 0:
    seed = 1
  x = seed & 0x0c
  seed += seed
  if x == 0x08:
      seed += 1
  elif x == 0x04:
      seed += 1
  seed &= 0x0f
  return seed

def num2tone(x):
  duration = Ed.NOTE_QUARTER
  if x >= 8:
     duration = Ed.NOTE_HALF
  x %= 8
  if x == 0:
      t = Ed.NOTE_C_7
  elif x == 1:
      t = Ed.NOTE_D_7
  elif x == 2:
      t = Ed.NOTE_E_7
  elif x == 3:
      t = Ed.NOTE_F_7
  elif x == 4:
      t = Ed.NOTE_G_7
  elif x == 5:
      t = Ed.NOTE_A_7
  elif x == 6:
      t = Ed.NOTE_B_7
  elif x == 7:
      t = Ed.NOTE_C_8
  Ed.PlayTone(t, duration)
  while Ed.ReadMusicEnd() == Ed.MUSIC_NOT_FINISHED:
      pass
  
## MAIN()

seed = 1

while True:
    r = rnd1()  # function with a bug   #  0x03 0x07 0x0f 0x0f 0x0f 0x0f
    #r = rnd2()  # this works ok        #  0x02 0x04 0x09 0x03 0x06 0x0d
    Ed.SendIRData(seed)  
    Ed.TimeWait(100, Ed.TIME_MILLISECONDS)
    num2tone(r)

Demo for python at PC, it works:

def rnd():
  global seed
  if seed == 0:
    seed = 1
  x = seed & 0x0c
  seed += seed
  if (x == 0x08) or (x == 0x04):
      seed += 1
  seed &= 0x0f
  return seed

### MAIN()
seed = 1
for i in range(20):
   print rnd()

Simpler bug demo for Edison robot, just play with LEDs:

#-------------Setup----------------

import Ed

Ed.EdisonVersion = Ed.V2

Ed.DistanceUnits = Ed.CM
Ed.Tempo = Ed.TEMPO_MEDIUM

#--------Your code below-----------

# a=0 b=0 => OK
# a=0 b=1 => BUG (Left LED is not activated)
# a=1 b=0 => BUG (Left LED is not activated)
# a=1 b=1 => BUG (Left LED is not activated)

a = 0
b = 1

if (a==1) or (b==1):     # BUG is here, condition is always FALSE
    Ed.LeftLed(Ed.ON)

if (a==1):
    Ed.RightLed(Ed.ON)
elif (b==1):
    Ed.RightLed(Ed.ON)

while True:
    pass

and reference version for PC:

a = 0
b = 1

if (a==1) or (b==1):
    print "LEFT"

if (a==1):
    print "RIGHT"
elif (b==1):
    print "RIGHT"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions