BBC BASIC for Windows
General >> Suggestion Box >> Add 4 byte floats
http://bb4w.conforums.com/index.cgi?board=suggestions&action=display&num=1381323874

Add 4 byte floats
Post by Michael Hutton on Nov 17th, 2010, 01:43am

Just a wish list suggestion. I know the arguments against.

Michael
Re: Add 4 byte floats
Post by JGHarston on Nov 25th, 2010, 9:56pm

on Nov 17th, 2010, 01:43am, Michael Hutton wrote:
Just a wish list suggestion. I know the arguments against.

Why?

Ie, what are the arguments for - how are you not able to achieve what you want with 5-byte floats? I would argue that 4-byte floats already exist, you just chose to ignore the fifth byte of a 5-byte float, in the same way that 16-bit integers already exist.

Re: Add 4 byte floats
Post by softweir on Nov 26th, 2010, 01:36am

Direct-X compatibility? It is possible that a Direct-X application might have a significant overhead inherent in converting 5-byte or 8-byte floats to 4-byte floats for passing to Direct-X.
Re: Add 4 byte floats
Post by admin on Nov 26th, 2010, 09:49am

on Nov 26th, 2010, 01:36am, softweir wrote:
Direct-X compatibility? It is possible that a Direct-X application might have a significant overhead inherent in converting 5-byte or 8-byte floats to 4-byte floats for passing to Direct-X.

That might be an argument for providing a built-in conversion function, but not for supporting 4-byte floats as a native data type.

I tried measuring the amount of time spent in calls to FN_f4 (the conversion function to 4-byte floats) in the 'tumbling teapot' Direct3D demonstration. It was so small the profiler didn't even register it! Even with the WAIT removed the conversion still took less than 10% of the total time.

Richard.

Re: Add 4 byte floats
Post by JGHarston on Nov 27th, 2010, 8:32pm

on Nov 26th, 2010, 09:49am, Richard Russell wrote:
That might be an argument for providing a built-in conversion function, but not for supporting 4-byte floats as a native data type.
Odd, when I wrote this 11 years ago (!) the documents I refered to stated the exponent was 8 bits, so using !(1+^real) would give you a 4-byte float, a construction I've occasionally used.

Experimentation and examining FN_f4() suggests

float4%=!(1+^float5):?(^float4%+3)=?(^float4%+3)-&40

works, but I've lost a bit of the exponent somewhere.

Re: Add 4 byte floats
Post by admin on Nov 27th, 2010, 10:29pm

on Nov 27th, 2010, 8:32pm, JGHarston wrote:
Experimentation and examining FN_f4() suggests

Today I added a section to the relevant Wiki article, listing some assembler code which can convert BBC BASIC (variant) numeric values into 4-byte floats very much faster that the standard FN_f4() routine - in fact more than ten times faster:

http://bb4w.wikispaces.com/Using+32-bit+floating+point+values

Richard.
Re: Add 4 byte floats
Post by admin on Nov 28th, 2010, 11:26am

on Nov 27th, 2010, 8:32pm, JGHarston wrote:
float4%=!(1+^float5):?(^float4%+3)=?(^float4%+3)-&40 works, but I've lost a bit of the exponent somewhere.

It isn't that simple, because the sign bit is in a completely different place. The simplest BASIC code I've managed which gives identical results to FN_f4() is as follows:

Code:
      DEF FN_f4b(A)
      LOCAL A%
      A *= 1.0
      A% = !^A AND &7FFFFFFF
      IF (A% AND &1FF)<>&80 A% += &80
      = SGN(A) AND &80000000 OR ?(^A+4) - 1 << 23 OR A% >>> 8 

Of course, this only works in *FLOAT40 mode. The A *= 1.0 can be dispensed with if you know A isn't an integer.

n.b. If the mantissa is >= &7FFFFF80 the code will fail with a 'Number too big' error when it attempts to do the rounding, so it's not actually usable as it stands.

Richard.