From: Horst Heinrich Dittgens on 14 Dec 2009 11:45 I am searching for a way to convert a double variable into a correspondent 8 byte string in a way that a greater double value will always be represented by a 'greater' string. I tried converting the double variable D into a string using CopyMemory VarPtr(ByteArray(0), VarPrt(D),8 S=String$(8,0):For I=1 to 8:Mid$(S,I)=chr$(ByteArray(I)):Next I This works fine but will not satisfy the condition that a resulting string S2 for a variable D2 > D1 will be 'greater' than S1 due to the order the bytes are stored in double precisions. Is there any function available which will return a 8 byte representation with the highest byte at the left and the lowest byte at the right? I need such a conversion function for a subroutine library which can do some nice things like sorting /searching a list of string keys (and corresponding data) and now should deal with doubles instead of strings in the key. Converting the doubles via str$(D) might not be a good idea because the data must be kept in memory and would probably take too much place if no rounding is allowed. Or in other words: How must I 'resort' the 8 bytes of a double precision to ensure that the highest bytes are leftmost and the lowest bytes are rightmost? Thank you.
From: Dee Earley on 14 Dec 2009 12:37 On 14/12/2009 16:45, Horst Heinrich Dittgens wrote: > I am searching for a way to convert a double variable into a > correspondent 8 byte string in a way that a greater double value will > always be represented by a 'greater' string. > > I tried converting the double variable D into a string using > > CopyMemory VarPtr(ByteArray(0), VarPrt(D),8 > S=String$(8,0):For I=1 to 8:Mid$(S,I)=chr$(ByteArray(I)):Next I > > This works fine but will not satisfy the condition that a resulting > string S2 for a variable D2 > D1 will be 'greater' than S1 due to the > order the bytes are stored in double precisions. > > Is there any function available which will return a 8 byte > representation with the highest byte at the left and the lowest byte at > the right? Very unlikely. The "parts" of a floating poi9nt number do not align to byte boundaries so you will need to work at the bit level. Further more, you can not safely store arbitrary binary data in a string. The only way to do this, is not to convert to a string, OR use a string representation (CStr) but a limit of 8 characters is very small, 8 BYTES even more so (4 characters only, VB uses unicode/utf-16). -- Dee Earley (dee.earley(a)icode.co.uk) i-Catcher Development Team iCode Systems
From: Nobody on 14 Dec 2009 12:53 "Horst Heinrich Dittgens" <hhd71(a)sofort-mail.de> wrote in message news:hg5q82$g74$00$1(a)news.t-online.com... >I am searching for a way to convert a double variable into a correspondent >8 byte string in a way that a greater double value will always be >represented by a 'greater' string. > > I tried converting the double variable D into a string using > > CopyMemory VarPtr(ByteArray(0), VarPrt(D),8 > S=String$(8,0):For I=1 to 8:Mid$(S,I)=chr$(ByteArray(I)):Next I > > This works fine but will not satisfy the condition that a resulting string > S2 for a variable D2 > D1 will be 'greater' than S1 due to the order the > bytes are stored in double precisions. > > Is there any function available which will return a 8 byte representation > with the highest byte at the left and the lowest byte at the right? > > I need such a conversion function for a subroutine library which can do > some nice things like sorting /searching a list of string keys (and > corresponding data) and now should deal with doubles instead of strings in > the key. > > Converting the doubles via str$(D) might not be a good idea because the > data must be kept in memory and would probably take too much place if no > rounding is allowed. > > Or in other words: How must I 'resort' the 8 bytes of a double precision > to ensure that the highest bytes are leftmost and the lowest bytes are > rightmost? It's not that simple. One way to compare doubles is to use a function like the following, which return the same result as StrComp(): Public Function DblComp(ByRef d1 As Double, ByRef d2 As Double) As Long Dim d As Double d = d1 - d2 If Abs(d) < 0.000000001 Then DblComp = 0 ' Equal ElseIf d > 0 Then DblComp = 1 ' d1 > d2 Else DblComp = -1 ' d1 < d2 End If End Function See also: http://en.wikipedia.org/wiki/Double_precision_floating-point_format
From: Rick Rothstein on 14 Dec 2009 13:43 > It's not that simple. One way to compare doubles is to use a function like > the following, which return the same result as StrComp(): > > Public Function DblComp(ByRef d1 As Double, ByRef d2 As Double) As Long > Dim d As Double > > d = d1 - d2 > > If Abs(d) < 0.000000001 Then > DblComp = 0 ' Equal > ElseIf d > 0 Then > DblComp = 1 ' d1 > d2 > Else > DblComp = -1 ' d1 < d2 > End If > End Function As a one-liner... Public Function DblComp(ByRef d1 As Double, ByRef d2 As Double) As Long DblComp = Sgn(d2 - d1) * (Abs(d2 - d1) >= 0.000000001) End Function -- Rick (MVP - Excel)
From: CY on 14 Dec 2009 14:58
On 14 Dec, 19:43, "Rick Rothstein" <rick.newsNO.S...(a)NO.SPAMverizon.net> wrote: > > It's not that simple. One way to compare doubles is to use a function like > > the following, which return the same result as StrComp(): > > > Public Function DblComp(ByRef d1 As Double, ByRef d2 As Double) As Long > > Dim d As Double > > > d = d1 - d2 > > > If Abs(d) < 0.000000001 Then > > DblComp = 0 ' Equal > > ElseIf d > 0 Then > > DblComp = 1 ' d1 > d2 > > Else > > DblComp = -1 ' d1 < d2 > > End If > > End Function > > As a one-liner... > > Public Function DblComp(ByRef d1 As Double, ByRef d2 As Double) As Long > DblComp = Sgn(d2 - d1) * (Abs(d2 - d1) >= 0.000000001) > End Function > > -- > Rick (MVP - Excel) Returning a boolean as a long? ;) //CY |