Newbie: strange int() behaviour

Joerg H. Baach usenet at baach.de
Tue Dec 3 08:25:32 EST 2002


Hi,

being new to python, and the group, I encountered a strange Problem - and
probably use the first opportunity to make a fool out of myself ;-) *

My very first python script calculates a value for a given number, prints
out a number of line conisting of stars (half the number) and the value 
itself.

Out of some strange reason it looks like 100 / 2 is in one case 49. Why?

The main loop:
for i in range (80,100,1):
    val = fuz.process("temp",i,"speed")
    half = val /2
    length = int(half)
    stars= length * "*"
    print "%s %s (%f) stars: %s" % (i,stars,val,length)
print "fertig"


The output:
...
85 ************************************************** (100.000000) stars: 50
86 ************************************************** (100.000000) stars: 50
87 ************************************************* (100.000000) stars: 49
88 ************************************************** (100.000000) stars: 50
...

87 is the only case where I encounter the problem. Why?

Thanks,

  Joerg


The script itself:
======================

class fuzzy:
    def __init__(self):
        self.data={}
        self.data["rules"]={}

    def addaxis (self, key, item):
        dic = {}
        for (name,a,b,c) in item:
            dic[name] = patch(a,b,c,name)
        self.data[key]=dic

    def addrule (self,key,val):
        self.data["rules"][key]=val


    def __getitem__ (self, key):
        return self.data[key]

    def output (self,startaxis,value,endaxis):
        key=(startaxis,endaxis)
        ruleset= self.data["rules"][key]
        out=[]
        for rule in ruleset:
            startpatch=self.data[startaxis][rule[0]]
            endpatch=self.data[endaxis][rule[1]]
            intens = startpatch.inpatch(value)
            #print "%s fires %s (%s)with %s" % (rule[0],rule[1],endpatch.top,intens)
            out.append((endpatch,intens))
        return out

    def defuzzy (self, patchlist):
        sum = div = 0
        
        for (patch,intens) in patchlist:
            sum += intens * patch.top
            div += intens
        if div==0:
            return 0
        else:
            return (sum / div)

    def process (self,startaxis,value,endaxis):
        new=self.defuzzy(self.output(startaxis,value,endaxis))
        #print "Wert auf Achse %s: %s" % (endaxis,new)
        return new


class patch:
    def __init__ (self,start,top,end,name):
        self.start=start
        self.top=top
        self.end=end
        self.name=name
        
    def inpatch (self,value):
        """Checks how much a value is in this patch"""
        
        if value <= self.top:
            min = self.start
            max = self.top
            dir = 1.0
            b = 0
        else:
            min = self.top
            max = self.end
            dir = -1.0 
            b = 1

        len = max - min
        if len == 0: y = b

        else:
            y = (dir / len) * (value - min) + b
        if y < 0: y =0

        return y
            

fuz = fuzzy();
fuz.addaxis(    "speed",
                [("stop",0,0,3),
                ("slow",10,30,50),
                ("medium",40,50,60),
                ("fast",50,70,90),
                ("blast",70,100,100)]
            )

fuz.addaxis(    "temp",
                [("cold",0,0,50),
                 ("cool",45,55,65),
                 ("right",55,65,70),
                 ("warm",65,75,85),
                 ("hot",80,90,90),
                 ("broken",71,81,83)]
            )
fuz.addrule(   ("temp","speed"),
               [("cold","stop"),
                ("cool","slow"),
                ("right","medium"),
                ("warm","fast"),
                ("hot","blast"),
                ("broken","stop")
                ]

            )

for i in range (80,100,1):
    val = fuz.process("temp",i,"speed")
    half = val /2
    length = int(half)
    stars= length * "*"
    print "%s %s (%f) stars: %s" % (i,stars,val,length)
print "fertig"



More information about the Python-list mailing list