## Odd error

Posts: 708

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

### Odd error

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.asmPOP DE ; return address; First parameter in APOP BC ; second parameter -> BLD C,A ; Put second parameter in C for safekeeping.LD HL,0ADD A,BLD L,A ; Put result in HLRL H   ; Pull in carry bit if necessary.LD A,B ; get originalCP C   ; compare with CJP NC, distance_Cbigger LD C,A ; B was smaller, so we replace the value in C with the smaller value in Adistance_Cbigger:; C was smaller, so we leave it alone.SRL C ; C=C/2XOR ALD B,ASBC HL,BC ; take half our smallest from HLSRL CAND A ; Clear carry flag.SBC HL,BCSRL CSRL CAND A ; Clear carry flag.ADD HL,BCLD A,HAND AJP 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,255EX DE,HL ; return address is in DE.JP (HL)END ASMEND FUNCTIONCLSDIM i,imax as uBytedim j,jmax as uBytedim answer,realanswer as uBytedim errorval,errorvalmax as floatimax=0jmax=0errorvalmax=0for j=1 to 250for i=1 to 250answer=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=errorvalend ifborder j next inext j`

Posts: 543

Joined: Fri Feb 13, 2009 3:11 pm

Location: Vienna, Austria

### Re: Odd error

If the Answer is defined as uByte, then 256 is 0...
No Error of ZXBC I guess.
------------------------------------------------------------

Posts: 708

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

### Re: Odd error

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.
Last edited by britlion on Wed Feb 29, 2012 2:18 pm, edited 1 time in total.

Posts: 1195

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

### Re: Odd error

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.1952

Uh:?: I haven't said a word... yet.

Posts: 708

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

### Re: Odd error

Ah. Fundamental problem. I can't read. Apart from that, carry on as normal.

Posts: 708

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

### Re: Odd error

No comment if this is a bug? Did it go away?

### Who is online

Users browsing this forum: No registered users and 1 guest