Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:48 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 2 of 2    
Author Message
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1241
Posted: 12:51pm 22 Jun 2024
Copy link to clipboard 
Print this post

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!  

  disco4now said  ... 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

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
causality ≠ correlation ≠ coincidence
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3800
Posted: 03:02pm 22 Jun 2024
Copy link to clipboard 
Print this post

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: Germany
Posts: 1241
Posted: 03:14pm 22 Jun 2024
Copy link to clipboard 
Print this post

@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
causality ≠ correlation ≠ coincidence
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 05:28am 23 Jun 2024
Copy link to clipboard 
Print this post

  twofingers said  I think this can be done with
d$= Bin2str$(single, a!, BIG)

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 Kingdom
Posts: 2075
Posted: 10:00am 23 Jun 2024
Copy link to clipboard 
Print this post

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: Australia
Posts: 482
Posted: 08:30am 24 Jun 2024
Copy link to clipboard 
Print this post

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
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024