INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Usefull Functions & Procedures

64BIT FLOATING FUNCTIONS by 738262
Posted: 18 May 02

FUNCTION  Float64ToNum
LPARAMETERS Float64 AS STRING

*!*        S XXXXXXX XXXX MMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM
*!*        0 0000000 0000 0000 00000000 00000000 00000000 00000000 00000000 00000000
*!*        1 2345678 9012 3456 78901234
*!*            1         2         3
LOCAL SIGN AS INTEGER
LOCAL Bias AS INTEGER
LOCAL Exponent AS INTEGER
LOCAL I AS INTEGER
LOCAL x AS STRING
LOCAL Mantissa AS NUMBER
LOCAL OldDecimals AS INTEGER
LOCAL oldFixed AS STRING
IF LEN(Float64)<8
  RETURN 0
ENDIF
*OldDecimals=SET("Decimals")
*SET DECIMALS TO 20
oldFixed=SET("Fixed")
SET FIXED OFF
Mantissa=1
SIGN=0
Bias=1023
Exponent=0
Float64=LEFT(Float64,8)
SIGN=IIF(BITTEST(ASC(LEFT(Float64,1)),7),-1,1)
FOR I=12 TO 2 STEP -1
  Exponent=Exponent+IIF(BITTEST(ASC(SUBSTR(Float64,INT((I-1)/8)+1,1)),7-MOD((I-1),8)),1,0)*(2^(12-I))
NEXT
Exponent=Exponent-1023
FOR I=13 TO 64
  AA=INT((I-1)/8)+1
  BB=7-MOD((I-1),8)
  B=IIF(BITTEST(ASC(SUBSTR(Float64,INT((I-1)/8)+1,1)),7-MOD((I-1),8)),1,0)
  Mantissa=Mantissa+IIF(BITTEST(ASC(SUBSTR(Float64,INT((I-1)/8)+1,1)),7-MOD((I-1),8)),1,0)*(1/(2^(I-12)))
NEXT
SET FIXED &oldFixed
RETURN SIGN*(Mantissa)*(2^Exponent)
ENDFUNC

FUNCTION NumToFloat64
LPARAMETERS NUMB AS NUMBER
LOCAL Float64 AS STRING
LOCAL SIGN AS INTEGER
LOCAL Bias AS INTEGER
LOCAL Exponent AS INTEGER
LOCAL I AS INTEGER
LOCAL Y AS INTEGER
LOCAL x AS STRING
LOCAL Mantissa AS NUMBER
LOCAL oldFixed AS STRING
LOCAL m.byte,m.bit
LOCAL fraction AS NUMBER
oldFixed=SET("Fixed")
SET FIXED OFF

Mantissa=0
SIGN=0
Bias=1023
Exponent=0

Float64=REPLICATE(CHR(0),8)

*set sing
Float64=STUFF(Float64,1,1,IIF(NUMB<0,CHR(128),CHR(0)))
NUMB=ABS(NUMB)
I=-1023
DO WHILE I<=1023
  IF NUMB<2^I
    EXIT
  ENDIF
  I=I+1
ENDDO
Exponent=1023+I-1
FOR I=0 TO 11
  IF BITTEST(Exponent,I)
    m.byte=INT(((12-I)-1)/8)+1
    m.bit=7-MOD(((12-I)-1),8)
    Float64=STUFF(Float64,m.byte,1,CHR(BITSET(ASC(SUBSTR(Float64,m.byte,1)),m.bit)))
  ENDIF
NEXT
Mantissa=NUMB/(2^(Exponent-1023))-1
FOR I=1 TO 52
  IF Mantissa>=1/(2^I)
    m.byte=INT(((I+12)-1)/8)+1
    m.bit=7-MOD(((I+12)-1),8)
    Float64=STUFF(Float64,m.byte,1,CHR(BITSET(ASC(SUBSTR(Float64,m.byte,1)),m.bit)))
    Mantissa=Mantissa-1/(2^I)
  ENDIF
  IF Mantissa=0
    EXIT
  endif
NEXT

SET FIXED &oldFixed
RETURN Float64
ENDFUNC

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close