Page 1 of 1

BIN string for Single FP question ...

Posted: Tue Dec 03, 2024 12:44 pm
by marc_256
Hallo,


For a side project (one of my small robots)
I like to use a LookUp table for the Cos() and Sin() values.

But I need the BIN() of these values.
Example for Pi

Code: Select all

Global Value.f
Value = 3.1415926535
Debug Bin (Value)
This shows me only the 3 value in BIN string (11).

Is there a pb command to convert a Single FP value to a BIN() string ?

I use [PB5.73 x64] on WIN 10 pro

Marc

Re: BIN string for Single FP question ...

Posted: Tue Dec 03, 2024 1:10 pm
by jacdelad
Binary numbers with fractions aren't trivial (though they aren't magic too): https://www.electronics-tutorials.ws/bi ... tions.html

You could multiply them by 10^number of desired digits behind the comma.

Re: BIN string for Single FP question ...

Posted: Tue Dec 03, 2024 1:16 pm
by infratec
Tell us what you expect as bin value.

A float for examples are 4 bytes.

Code: Select all

Global Value.f
Value = 3.1415926535

For i = 0 To 3
  Debug "$" + RSet(Hex(PeekA(@Value + i)), 2, "0") + " %" + RSet(Bin(PeekA(@Value + i)), 8, "0")
Next i

ShowMemoryViewer(@Value, 4)

Re: BIN string for Single FP question ...

Posted: Tue Dec 03, 2024 2:55 pm
by STARGĂ…TE

Code: Select all

Global Value.f
Value = 3.1415926535

Debug "SEEEEEEEEMMMMMMMMMMMMMMMMMMMMMMM  (S)ign, (E)xponent, (M)antissa"
Debug RSet(Bin(PeekL(@Value)), 32, "0")

Re: BIN string for Single FP question ...

Posted: Tue Dec 03, 2024 4:19 pm
by NicTheQuick
I would like to understand why you need the binary representation of a float for a lookup table of sin and cos. What exactly do you wanna do with that?

Re: BIN string for Single FP question ...

Posted: Tue Dec 03, 2024 5:15 pm
by marc_256
Hello all,
thanks for the help,


Well, as I see in the robot club,
the younger people, who have all the luxury of they lives today,
they can order electronics part from anywhere in the world, for almost no money.

Today, the robot club haves only older guys (+55 years and older), who are building autonomous robots.
So, I want to proof that with very old hardware (68HC11 8 bits cpu) you can join the yearly robot contest.


But, this cpu runs on 8 MHz clock, and need 4 clocks per 8bit machine instruction.
So, I have a assembler FP library for the 68HC11 cpu, but this is very slow to calculate Cos(), Sin(), Tan(), Sqr(), ...

I want to use a lookup table for this, stored in an EPROM 27C40 or FLASH 29C40 = 4 Mbits = 256 KBytes
For my application I need 0.00 to 360.00 degrees in a resolution of 0.01 degree.
This gives me 36000 * 4 bytes/Single FP = 144000 bytes in memory.
In combination with Cos and Sin this is ok for the volume of the EPROM/FLASH.

To program this EPROM or FLASH, I use a (very old) PROM programmer,
So, to calculate this values I want to use PB to create this lookup table.
I want to use the IEEE 754 FP notations for this.

1 Sign bit, 8 exp. bits, 23 fraction mantissa bits = 32 bits FP data.





I did this more than ones in the years 1970+,
the manual conversion from DEC to Single FP 32bits.
But I had have to look on YT for doing the conversions (Old guy).
Here an example:

Sign = 1 bit
Exponent = 8 bits
Mantissa = 23 bits

Code: Select all

USER VALUE DECIMAL         : 263.30
---------------------------------------------------------
BINARY LEFT VALUE          : 100000111
BINARY LEFT VALUE LENGTH   : 9

                             000000000011111111112222
                             012345678901234567890123
BINARY RIGHT VALUE         : 0100110011001100110011001

---------------------------------------------------------
BINARY SIGN BIT            : [0] 0=[+] / 1=[-]
BINARY EXPONENT            : [10000111]
BINARY MANTISSA            : [00000111010011001100110]
                             S EXPONENT MANTISSA---------------
BINARY IEEE 754 NOTATION   : 0 10000111 00000111010011001100110
BYTE 0            [Binary] : 01000011
BYTE 1            [Binary] : 10000011
BYTE 2            [Binary] : 10100110
BYTE 3            [Binary] : 01100110

Thanks marc,

Re: BIN string for Single FP question ...

Posted: Tue Dec 03, 2024 5:35 pm
by NicTheQuick
You can decrease the memory consumption even more when you consider that sin and cos are basically just phase shifted and that only a quarter of the sine wave is enough to calculate the rest of it by just mirroring and changing the sign of the value.

And here's another idea using interpolation: https://namoseley.wordpress.com/2015/07 ... rpolation/