Odd error britlion Posting Freak Posts: 805 Threads: 135 Joined: Apr 2009 Reputation: 5 02-27-2012, 06:57 PM This might be another float bug. I wrote up the distance algorithm na_th_an showed us. It seems to work - but in this test program, it's the floating point values that go wrong... If you set your emulator to full speed, you see it looking for the biggest error it can find, showing larger errors as it finds them. Eventually it hits distance (250,56) which my function says is > 255 (so returns 255). The value calculated from square rooting in floats is...er 0. So that's an error of 255, apparently. Except the correct answer would be 256.1952 Is the int (256) returning 0 for some reason? On a float? When I'm happy with this funtion, it will go in the function library as a code snippet. Code:```FUNCTION fastcall distance (a as ubyte, b as ubyte) as uByte ' returns a fast approximation of SQRT (a^2 + b^2) - the distance formula, generated from taylor series expansion. asm POP DE ; return address ; First parameter in A POP BC ; second parameter -> B LD C,A ; Put second parameter in C for safekeeping. LD HL,0 ADD A,B LD L,A ; Put result in HL RL H   ; Pull in carry bit if necessary. LD A,B ; get original CP C   ; compare with C JP NC, distance_Cbigger LD C,A ; B was smaller, so we replace the value in C with the smaller value in A distance_Cbigger: ; C was smaller, so we leave it alone. SRL C ; C=C/2 XOR A LD B,A SBC HL,BC ; take half our smallest from HL SRL C AND A ; Clear carry flag. SBC HL,BC SRL C SRL C AND A ; Clear carry flag. ADD HL,BC LD A,H AND A JP NZ, distance_toobig LD A,L ; Get result. EX DE,HL ; return address is in DE. JP (HL) distance_toobig: ; If the answer turns out to be > 255, we'll return 255. LD A,255 EX DE,HL ; return address is in DE. JP (HL) END ASM END FUNCTION CLS DIM i,imax as uByte dim j,jmax as uByte dim answer,realanswer as uByte dim errorval,errorvalmax as float imax=0 jmax=0 errorvalmax=0 for j=1 to 250 for i=1 to 250 answer=distance(i,j) realanswer=SQR(CAST(float,i)*i+CAST(float,j)*j) errorval=ABS(CAST(float,realanswer)-answer) if errorval>errorvalmax then PRINT i;" ";j;" ";answer;" ";INT realanswer; "   ";int errorval imax=i jmax=j errorvalmax=errorval end if border j next i next j``` LCD Senior Member Posts: 614 Threads: 49 Joined: Feb 2009 Reputation: 2 02-28-2012, 07:09 PM If the Answer is defined as uByte, then 256 is 0... No Error of ZXBC I guess. ------------------------------------------------------------ http://lcd-one.da.ru redirector is dead Visit my http://members.inode.at/838331/index.html home page! britlion Posting Freak Posts: 805 Threads: 135 Joined: Apr 2009 Reputation: 5 02-29-2012, 01:55 PM Float, Jose LCD. Float: Britlion Wrote:The value calculated from square rooting in floats is...er 0. So that's an error of 255, apparently. Except the correct answer would be 256.1952 The ubyte bit is coming out nicely. I was baffled by the float version of the calculation, here. I changed how it all worked anyway, so it might not matter. I did wonder if it was pointing to an odd bug, though. boriel Administrator Posts: 1,690 Threads: 54 Joined: Aug 2019 Reputation: 10 02-29-2012, 02:13 PM britlion Wrote:Float, Jose. Float: Britlion Wrote:The value calculated from square rooting in floats is...er 0. So that's an error of 255, apparently. Except the correct answer would be 256.1952Uh:?: I haven't said a word... yet. :roll: britlion Posting Freak Posts: 805 Threads: 135 Joined: Apr 2009 Reputation: 5 02-29-2012, 02:17 PM Ah. Fundamental problem. I can't read. Apart from that, carry on as normal. britlion Posting Freak Posts: 805 Threads: 135 Joined: Apr 2009 Reputation: 5 04-06-2012, 02:00 PM No comment if this is a bug? Did it go away? « Next Oldest | Next Newest »