BIN string for Single FP question ...

Just starting out? Need help? Post your questions and find answers here.
marc_256
Addict
Addict
Posts: 857
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

BIN string for Single FP question ...

Post 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
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
jacdelad
Addict
Addict
Posts: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: BIN string for Single FP question ...

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: BIN string for Single FP question ...

Post 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)
User avatar
STARGÅTE
Addict
Addict
Posts: 2259
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: BIN string for Single FP question ...

Post 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")
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: BIN string for Single FP question ...

Post 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?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
marc_256
Addict
Addict
Posts: 857
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: BIN string for Single FP question ...

Post 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,
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: BIN string for Single FP question ...

Post 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/
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply