|
Forum Index : Microcontroller and PC projects : Converting a single precision float to double in Basic
| Author | Message | ||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1769 |
As an alternative to Peter's code, we can also use the following: 'x$="bb10d5a6" 'x$="4640e400" x$="c640e733" Print x$ Print CDBL2(x$) End Function CDBL2(x$) Local i%,z$ For i% = 7 To 1 Step -2 z$=z$+Chr$(Val("&h"+Mid$(x$,i%,2))) Next CDBL2=Str2bin(SINGLE, z$) End Function Only tested with the three values! Regards Michael edit: But Johns function is 20% faster! I think this can be done with d$= Bin2str$(single, a!, BIG) .Just for me, this is the thread that triggered the problem. Edited 2024-06-23 00:23 by twofingers ‚My name is Ozymandias, king of kings Look on my works, ye Mighty, and despair!‘ Nothing beside remains. Round the decay Of that colossal wreck, boundless and bare The lone and ... |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4378 |
It's not my function, really - it's Peter's. All I did was tweak his code. John |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1769 |
@John All good! It's Peter's code and you wrapped it in a function. I don't think Peter is jealous. Kind regards (and it's always a pleasure to see/read something from you) Michael ‚My name is Ozymandias, king of kings Look on my works, ye Mighty, and despair!‘ Nothing beside remains. Round the decay Of that colossal wreck, boundless and bare The lone and ... |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3376 |
And if you need to convert that to Hex:- out$="":for n=1 to 4:out$=out$+hex$(STR2BIN(uint8,mid$(d$,n,1)),2):next:?out$ or out$="" a! = -0.002210000064 for n=1 to 4 out$=out$+hex$(STR2BIN(uint8,mid$(Bin2str$(single, a!, BIG),n,1)),2) next Print out$ > RUN BB10D5A6 > Or as a Function:- Function CSGL$(a!) 'you pass in the 8 byte double-precision float number Local out$="", sgl$="" sgl$= Bin2str$(single, a!, BIG) For n=1 To 4 out$=out$+Hex$(Str2bin(uint8,Mid$(sgl$,n,1)),2) Next CSGL$ = out$ End Function > ? csgl$(cdbl("abcd012")) 0ABCD012 > > ? cdbl(csgl$(-1.463246299e-12)) -1.463246299e-12 > Edited 2024-06-23 17:01 by phil99 |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
Excellent work. I put those two functions on an MMX (double precision) > ?pi 3.141592654 > ? CSNG$(pi) 40490FDB > ? CDBL("40490FDB") 3.141592741 <--- clearly some "rounding error" due to loss of granularity but likely close enough > ? pi - 3.141592741 -8.741020707e-08 > scribbled following to POKE "40490FDB" into a float on a MM Mk2 (single precision IEEE754 - same as that ACS7262) > list Dim a$="40490FDB" Dim q As float For n=0 To 3 Poke VAR q, 3-n, Val("&h"+Mid$(a$,(2*n)+1,2)) Next Print q > RUN 3.14159 > Well done all. h Edited 2024-06-24 05:46 by CaptainBoing |
||||
| zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 701 |
Looks good - especially as I get the same values [ f77 automatically converts double precision to single when required ] Running PROGRAM singval DOUBLE b REAL c b = 3.141592654 c = b FORMAT (A,F15.9,/) WRITE (6,0) "b = ", b WRITE (6,0) "c = ", c END \ b = 3.141592654 c = 3.141592741 OK Edited 2024-06-24 18:32 by zeitfest |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |