Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:37 25 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Converting a single precision float to double in Basic

     Page 1 of 2    
Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9110
Posted: 11:35am 20 Jun 2024
Copy link to clipboard 
Print this post

In another thread a reading from a sensor was returning a single precision floating point number.
This post shows how to convert this to a standard MMBasic double with a bit of of simple bit-twiddling.

Option explicit
Option default none
Dim w%,s$,c%,d%,e%,o%,a!,i!
Input "Enter a single precision FP number as hex and the expected answer";s$,i!
If Left$(s$,2)<>"&H" Then s$="&H"+s$
Poke word Peek(varaddr w%),Val(s$) 'store the single precision floating point
c%=(w% And &H80000000)<<32 'get the sign bit
d%=(w% And &h7FFFFF)<<29 'get the mantissa
e%=(((w% And &H7f800000)>>23)-127+1023)<<52 'get the exponent and convert
o%=(c% Or d% Or e%)
Poke integer Peek(varaddr a!),o% 'put the answer into a double
Print "Double precision", a!



 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2075
Posted: 11:42am 20 Jun 2024
Copy link to clipboard 
Print this post

hero! is it any wonder we love him so!  

seriously, great work!

Fill yer boots Stan!

h
Edited 2024-06-20 21:44 by CaptainBoing
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9110
Posted: 06:00pm 20 Jun 2024
Copy link to clipboard 
Print this post

In case it isn't clear, the algorithm is as follows:

Move the sign bit (bit31) from the float to the sign bit (bit63) in the double
Move the mantissa (bits0-22) from the float to bits29-51 in the double

The only remotely complex piece is the handling of the exponent
Bits23-30 are the exponent in the float (coded 0-255) and we have to subtract 127 to get the actual exponent -127 to 128
Bits52-62 are the exponent in the double (coded 0-2047) so we have to add 1023 to the actual float exponent to get the coded double exponent

hence given a integer variable in memory w% holding the 32-bit float:
c%=(w% And &H80000000)<<32 'get the sign bit
d%=(w% And &h7FFFFF)<<29 'get the mantissa
e%=(((w% And &H7f800000)>>23)-127+1023)<<52 'get the exponent and convert
o%=(c% Or d% Or e%)

will create an integer o% in memory holding the 64-bit double
Then this can be copied into a floating point variable a! using
Poke integer Peek(varaddr a!),o% 'put the answer into a double

Edited 2024-06-21 04:04 by matherp
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2125
Posted: 08:32pm 20 Jun 2024
Copy link to clipboard 
Print this post

er.. still don't understand, like logs when I was 15 or tables of pi, trig was hard to.
nice of @matherp to post the conversion code and explain the idea.
what's with using peek and poke? it's like picaxe but seems to have become normal in mmb but where's it say why? noticed mmb is getting more complicated? hate to be a new user.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6783
Posted: 09:17pm 20 Jun 2024
Copy link to clipboard 
Print this post

PEEK and POKE are powerful and very useful in MMBasic. Peter is using them to copy just part of one number into a new one. It's no use simply copying the number as he is converting the original format into a different one.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3800
Posted: 09:28pm 20 Jun 2024
Copy link to clipboard 
Print this post

Stan,

The internal encodings of single & double (IEEE) floating point numbers are fairly similar but need some adjusting to convert from single to double.  Peter's code does the magic needed.  It's a bit ugly looking but hey MMBasic was never designed to do it - and you can just grab the code :)

John
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 482
Posted: 12:05am 21 Jun 2024
Copy link to clipboard 
Print this post

There is a function CDBL in standard Basic for it,

see CDBL in this ref
Edited 2024-06-21 10:06 by zeitfest
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4223
Posted: 05:11am 21 Jun 2024
Copy link to clipboard 
Print this post

Peter,

Thanks for the explanation. I was mistaken about the exponent.

Volhout
PicomiteVGA PETSCII ROBOTS
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3800
Posted: 07:57am 21 Jun 2024
Copy link to clipboard 
Print this post

  zeitfest said  There is a function CDBL in standard Basic for it,

see CDBL in this ref

So something like (untested!)

Option explicit
Option default none

function CDBL(s$)
local w%,c%,d%,e%,o%,a!  
If Left$(s$,2)<>"&H" Then s$="&H"+s$
Poke word Peek(varaddr w%),Val(s$) 'store the single precision floating point
c%=(w% And &H80000000)<<32 'get the sign bit
d%=(w% And &h7FFFFF)<<29 'get the mantissa
e%=(((w% And &H7f800000)>>23)-127+1023)<<52 'get the exponent and convert
o%=(c% Or d% Or e%)
Poke integer Peek(varaddr a!),o% 'put the answer into a double
CDBL=a!
end function

but you pass in the 4 hex bytes of the single-precision number as a string

John
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6783
Posted: 08:05am 21 Jun 2024
Copy link to clipboard 
Print this post

  zeitfest said  There is a function CDBL in standard Basic for it,

see CDBL in this ref


"Standard BASIC"? What a strange concept. :) There ain't never been no such thing. The original BASIC (Dartmouth) had a single floating point precision for everything:
  Quote  All arithmetic was done in floating point. In the GE-225 and GE-235, this meant a precision of about 30 bits (roughly ten digits) with an base 2 exponent range of -256 to +255.

Since then all the BASICs have been non-standard. :)

Dartmouth BASIC commands
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2075
Posted: 08:26am 21 Jun 2024
Copy link to clipboard 
Print this post

  JohnS said  
So something like (untested!)

function CDBL(s$)
...
end function

but you pass in the 4 hex bytes of the single-precision number as a string


A candidate for inclusion on FotS

h
Edited 2024-06-21 18:39 by CaptainBoing
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 08:39am 21 Jun 2024
Copy link to clipboard 
Print this post

function CDBL(s$) tested and gives same results as Peter's original post.
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2125
Posted: 05:33pm 21 Jun 2024
Copy link to clipboard 
Print this post

  Mixtel90 said  
  zeitfest said  There is a function CDBL in standard Basic for it,

see CDBL in this ref


"Standard BASIC"? What a strange concept. :) There ain't never been no such thing. The original BASIC (Dartmouth) had a single floating point precision for everything:
  Quote  All arithmetic was done in floating point. In the GE-225 and GE-235, this meant a precision of about 30 bits (roughly ten digits) with an base 2 exponent range of -256 to +255.

Since then all the BASICs have been non-standard. :)

Dartmouth BASIC commands

basic in the 80's, many manufacturers basic, for pic picaxe,gcbasic,mmbasic ,
converting code is difficult imho.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6783
Posted: 05:53pm 21 Jun 2024
Copy link to clipboard 
Print this post

Converting between BASICs becomes more difficult the further away you move from Dartmouth BASIC. That defined the fundamental commands that are common to just about all of them (if you exclude the Tiny BASICs, which are specialised to run on low memory systems).
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3800
Posted: 05:59pm 21 Jun 2024
Copy link to clipboard 
Print this post

  phil99 said  function CDBL(s$) tested and gives same results as Peter's original post.

Thanks!

John
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2075
Posted: 07:55pm 21 Jun 2024
Copy link to clipboard 
Print this post

I never had a problem converting between dialects or even languages (within reason)... machine level stuff like PEEKs and POKEs, embedded machine code and calls into the firmware are really the only thing that gets in the way. If the "donor" code sticks to the basics (ha ha) then I consider it to be an enjoyable canter.

When I was a much younger programmer in simpler times (early-middle 80s), one of my favourite things was to hand-compile. Get a program working right in Basic, then convert it line by line to assembler optimizing along the way. The other day, I was looking at some met radar processing stuff I did 38 years ago and the tell-tale was jump destinations like L270, L390 etc. Floating point stuff and everything. You ended up with the same prog but very much faster plus things you couldn't do easily in the supplied Basic

h
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 857
Posted: 08:22pm 21 Jun 2024
Copy link to clipboard 
Print this post

  CaptainBoing said  
When I was a much younger programmer in simpler times (early-middle 80s), one of my favourite things was to hand-compile. Get a program working right in Basic, then convert it line by line to assembler optimizing along the way. The other day, I was looking at some met radar processing stuff I did 38 years ago and the tell-tale was jump destinations like L270, L390 etc. Floating point stuff and everything. You ended up with the same prog but very much faster plus things you couldn't do easily in the supplied Basic
h


This is the kinda stuff I like to read about  
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2125
Posted: 08:53pm 21 Jun 2024
Copy link to clipboard 
Print this post

siclair basic and bbc basic, very different yet one was used in schools and the other home,cost.
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 896
Posted: 12:20am 22 Jun 2024
Copy link to clipboard 
Print this post

  Quote  A candidate for inclusion on FotS

I have already added to page for it. Mostly just points to this thread at the moment. I also have a need to go the other way, sending the value in a double precision float to a CAN bus controlled power supply  which expects 4 bytes coded as a single precision float.
Gerry
FotS Page
P.S.
Any potential FotS authors just send me a PM and I will create an account. Include your email if its not already in your TBS member details as FotS  will automatically send the password.
Edited 2024-06-22 10:46 by disco4now
Latest F4 Latest H7
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2075
Posted: 06:04am 22 Jun 2024
Copy link to clipboard 
Print this post



It would be a shame to "lose" info like this - good work
 
     Page 1 of 2    
Print this page
© JAQ Software 2024