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 KingdomPosts: 9110 |
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 KingdomPosts: 2075 |
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 KingdomPosts: 9110 |
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 KingdomPosts: 2125 |
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 KingdomPosts: 6783 |
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 KingdomPosts: 3800 |
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: AustraliaPosts: 482 |
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: NetherlandsPosts: 4223 |
Peter, Thanks for the explanation. I was mistaken about the exponent. Volhout PicomiteVGA PETSCII ROBOTS |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3800 |
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 KingdomPosts: 6783 |
"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: 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 KingdomPosts: 2075 |
A candidate for inclusion on FotS h Edited 2024-06-21 18:39 by CaptainBoing |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2135 |
function CDBL(s$) tested and gives same results as Peter's original post. |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2125 |
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 KingdomPosts: 6783 |
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 KingdomPosts: 3800 |
Thanks! John |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
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 KingdomPosts: 857 |
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 KingdomPosts: 2125 |
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: AustraliaPosts: 896 |
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 KingdomPosts: 2075 |
It would be a shame to "lose" info like this - good work |
||||
Page 1 of 2 |
Print this page |