Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!udel!haven!adm!news From: CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) Newsgroups: comp.lang.pascal Subject: Re: SET Message-ID: <25107@adm.brl.mil> Date: 26 Nov 90 19:46:12 GMT Sender: news@adm.brl.mil Lines: 119 In article <9011260748.aa11950@VIM.BRL.MIL> PRDESENV%ORION.CPQD.ANSP.BR@uga.cc.uga.edu ( Claudio ) writes: >Does anybody know how can I build a SET with Real (or double) >elements in Turbo Pascal? I Only need a SET with 15 real numbers. > >Once SET requires subranges with no more than 256 itens I can not >make a set of word (my real numbers have only one place after the >decimal point, so I could multiply by 10, solving the problem, but >the range from the first to the last overcome 256). [deleted to end] In article <9011260916.aa08727@VGR.BRL.MIL> ZCCBJSB%EB0UB011.BITNET@cunyvm.cuny.edu ( Josep Sau B. ) writes: >If those real values are variable, forget it, you can't use the set >structure. > >If those real values are constant, you may do thru with this trick: > >Declare a const array of real, and use the ordinal index type to >build up the set to work with. [deleted to end] I just wanted to point out that Josep Sau B.'s method may be more general than he admits: You should be able to declare a _variable_ array [1..max] of reals together with the value of the highest index in use. Then add reals to the array as required, starting with element 1. If you delete an element from the set, compact the array and Dec the value of the high index. Example: const maxIndex = 255; type RealSetArray = array[1..maxIndex]of real; var RealSet : RealSetArray; i,hiIndex : byte; procedure InitRealSetArray(var index : byte); begin index := 0; end; function RealInSet (r : real; var rsa : RealSetArray; var i : byte; hiIndex : byte) : Boolean; {Obviously for a large set you'd use a more sophisticated searching routine than you see here--but this is about sets, not about searching. } var ii : byte; begin for ii := 1 to hiIndex do if r = rsa[ii] then begin RealInSet := True; i := ii; Exit; end; RealInSet := False; end; function AddRealToSet (r : real; var rsa : RealSetArray; var index : Byte) : Boolean; var i : byte; begin AddRealToSet := True; if not RealInSet(r,rsa,i,index) then begin if index = maxIndex then AddRealToSet := False else begin Inc(index); rsa[index] := r; end; end; end; procedure RemoveRealFromSet (r : real; var rsa : RealSetArray; var index : Byte); var i : byte; begin if RealInSet(r,rsa,i,index) then begin rsa[i] := rsa[index]; Dec(index); end; end; begin InitRealSetArray(hiIndex); if not AddRealToSet(3.1415,RealSet,hiIndex) then Halt(1); if not AddRealToSet(2.7183,RealSet,hiIndex) then Halt(1); if not AddRealToSet(-1.5708,RealSet,hiIndex) then Halt(1); WriteLn('2.5 is in the set: ',RealInSet(2.5,RealSet,i,hiIndex)); WriteLn('Pi is in the set: ',RealInSet(3.1415,RealSet,i,hiIndex)); RemoveRealFromSet(3.1415,RealSet,hiIndex); if not AddRealToSet(2.5,RealSet,hiIndex) then Halt(1); WriteLn('Added 2.5, deleted 3.1415'); WriteLn('2.5 is in the set: ',RealInSet(2.5,RealSet,i,hiIndex)); WriteLn('Pi is in the set: ',RealInSet(3.1415,RealSet,i,hiIndex)); end. Caveat about RealInSet: Anything which relies on comparing "real" numbers for "exact equality" can prove flakey. You may want to Trunc or Round your reals in some manner which leads to reliable comparisons. +-------------------------------------------------------------------------+ | Karl Brendel Centers for Disease Control | | Internet: CDCKAB@EMUVM1.BITNET Epidemiology Program Office | | Bitnet: CDCKAB@EMUVM1 Atlanta, GA, USA | | ILink/RIME: KARL BRENDEL phone 404/639-2709 | | CIS : 73307,3101 fts 236-2709 | | GEnie: K.BRENDEL Home of Epi Info 5.0 | +-------------------------------------------------------------------------+