Code:
'========================================================================= ' Fixed point numbers demo ' Huge Integer Math and Encryption library dll ' ( H I M E . d l l ) ' (C) 2002-2007 DevOTechS ' e-mail: support at devotechs.com ' ' Demo program to use HIME.dll with fixed point numbers ' HIME.dll can be downloaded from: ' www.devotechs.com ' '========================================================================= #PBFORMS CREATED #COMPILE EXE #DIM ALL '-------------------------------------------------------------------------------- ' ** Includes ** '-------------------------------------------------------------------------------- #INCLUDE "HIME.inc" #PBFORMS BEGIN INCLUDES '#RESOURCE "HIME_Fixed_Point_Demo.pbr" #IF NOT %DEF(%WINAPI) #INCLUDE "WIN32API.INC" #ENDIF #INCLUDE "PBForms.INC" #PBFORMS END INCLUDES '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- ' ** Globals ** '-------------------------------------------------------------------------------- GLOBAL ghDlg AS DWORD GLOBAL HiFP_Num1 AS STRING GLOBAL HiFP_Num2 AS STRING GLOBAL HiFP_Accuracy AS LONG GLOBAL HiFP_Accuracy_Zeroes AS STRING '-------------------------------------------------------------------------------- ' ** Constants ** '-------------------------------------------------------------------------------- #PBFORMS BEGIN CONSTANTS %IDD_DIALOG1 = 101 %IDC_LABEL1 = 1001 %IDC_LABEL2 = 1003 %IDC_TEXT_NUM1 = 1002 %IDC_LABEL3 = 1005 %IDC_TEXT_NUM2 = 1004 %IDC_LABEL4 = 1006 %IDC_TEXT_RESULT = 1007 %IDC_BUTT_ADD = 1008 %IDC_BUTT_SUBTR = 1009 %IDC_BUTT_DIV = 1010 %IDC_BUTT_MUL = 1011 %IDC_LABEL5 = 1013 %IDC_TEXT_ACC = 1012 %IDC_LABEL6 = 1014 #PBFORMS END CONSTANTS '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- ' ** Declarations ** '-------------------------------------------------------------------------------- DECLARE SUB HiFP_Init DECLARE SUB HiFP_Division DECLARE SUB HiFP_Multiplication DECLARE SUB HiFP_Addition DECLARE SUB HiFP_Subtraction DECLARE SUB HiFP_SendNumbersToHIME DECLARE FUNCTION HiFP_GetNumberFromHIME(i AS LONG) AS STRING DECLARE FUNCTION HiFP_Integer2FixedPoint(Num AS STRING) AS STRING DECLARE CALLBACK FUNCTION ShowDIALOG1Proc() DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG #PBFORMS DECLARATIONS '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- FUNCTION PBMAIN() ShowDIALOG1 %HWND_DESKTOP END FUNCTION '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- ' ** CallBacks ** '-------------------------------------------------------------------------------- CALLBACK FUNCTION ShowDIALOG1Proc() SELECT CASE CBMSG CASE %WM_COMMAND SELECT CASE CBCTL CASE %IDC_LABEL1 CASE %IDC_TEXT_NUM1 CASE %IDC_LABEL2 CASE %IDC_TEXT_NUM2 CASE %IDC_LABEL3 CASE %IDC_LABEL4 CASE %IDC_TEXT_RESULT CASE %IDC_BUTT_ADD IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN HiFP_Addition END IF CASE %IDC_BUTT_SUBTR IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN HiFP_Subtraction END IF CASE %IDC_BUTT_DIV IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN HiFP_Division END IF CASE %IDC_BUTT_MUL IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN HiFP_Multiplication END IF CASE %IDC_TEXT_ACC CASE %IDC_LABEL5 CASE %IDC_LABEL6 END SELECT END SELECT END FUNCTION '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- ' ** Dialogs ** '-------------------------------------------------------------------------------- FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG LOCAL lRslt AS LONG #PBFORMS BEGIN DIALOG %IDD_DIALOG1->-> LOCAL hDlg AS DWORD LOCAL hFont1 AS DWORD LOCAL hFont2 AS DWORD DIALOG NEW hParent, "HIME Fixed Point Numbers Demo", 235, 254, 311, 194, _ %WS_POPUP OR %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR _ %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE _ OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, _ %WS_EX_WINDOWEDGE OR %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _ %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg CONTROL ADD LABEL, hDlg, %IDC_LABEL1, "Enter 2 decimal point numbers and " + _ "select an operation", 6, 4, 298, 16, %WS_CHILD OR %WS_VISIBLE OR _ %SS_CENTER OR %SS_SUNKEN, %WS_EX_LEFT OR %WS_EX_LTRREADING CONTROL ADD TEXTBOX, hDlg, %IDC_TEXT_NUM1, "1.123456", 4, 36, 170, 12 CONTROL ADD LABEL, hDlg, %IDC_LABEL2, "Number 1", 4, 26, 64, 8 CONTROL ADD TEXTBOX, hDlg, %IDC_TEXT_NUM2, "5.123456", 4, 62, 170, 12 CONTROL ADD LABEL, hDlg, %IDC_LABEL3, "Number 2", 4, 52, 64, 8 CONTROL ADD LABEL, hDlg, %IDC_LABEL4, "Result", 4, 80, 64, 8 CONTROL ADD TEXTBOX, hDlg, %IDC_TEXT_RESULT, "TextBox3", 4, 92, 300, 94, _ %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %WS_HSCROLL OR %WS_VSCROLL _ OR %ES_LEFT OR %ES_MULTILINE OR %ES_AUTOHSCROLL, %WS_EX_CLIENTEDGE OR _ %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR CONTROL ADD BUTTON, hDlg, %IDC_BUTT_ADD, "+", 180, 62, 20, 14, %WS_CHILD OR _ %WS_VISIBLE OR %WS_TABSTOP OR %BS_TEXT OR _ %BS_PUSHBUTTON OR %BS_CENTER OR %BS_VCENTER, %WS_EX_LEFT OR _ %WS_EX_LTRREADING CONTROL ADD BUTTON, hDlg, %IDC_BUTT_SUBTR, "-", 202, 62, 20, 14, %WS_CHILD _ OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_TEXT OR _ %BS_PUSHBUTTON OR %BS_CENTER OR %BS_BOTTOM, %WS_EX_LEFT OR _ %WS_EX_LTRREADING CONTROL ADD BUTTON, hDlg, %IDC_BUTT_DIV, "/", 246, 62, 20, 14 CONTROL ADD BUTTON, hDlg, %IDC_BUTT_MUL, "*", 224, 62, 20, 14, %WS_CHILD OR _ %WS_VISIBLE OR %WS_TABSTOP OR %BS_TEXT OR _ %BS_PUSHBUTTON OR %BS_CENTER OR %BS_TOP, %WS_EX_LEFT OR _ %WS_EX_LTRREADING CONTROL ADD TEXTBOX, hDlg, %IDC_TEXT_ACC, "30", 180, 36, 34, 12 CONTROL ADD LABEL, hDlg, %IDC_LABEL5, "Accuracy [digits]", 180, 26, 64, 8 CONTROL ADD LABEL, hDlg, %IDC_LABEL6, "Operation", 180, 52, 64, 8 hFont1 = PBFormsMakeFont("Courier New", 9, 400, %FALSE, %FALSE, %FALSE, %ANSI_CHARSET) hFont2 = PBFormsMakeFont("MS Sans Serif", 12, 700, %FALSE, %FALSE, %FALSE, %ANSI_CHARSET) CONTROL SEND hDlg, %IDC_TEXT_NUM1, %WM_SETFONT, hFont1, 0 CONTROL SEND hDlg, %IDC_TEXT_NUM2, %WM_SETFONT, hFont1, 0 CONTROL SEND hDlg, %IDC_TEXT_RESULT, %WM_SETFONT, hFont1, 0 CONTROL SEND hDlg, %IDC_BUTT_ADD, %WM_SETFONT, hFont2, 0 CONTROL SEND hDlg, %IDC_BUTT_SUBTR, %WM_SETFONT, hFont2, 0 CONTROL SEND hDlg, %IDC_BUTT_DIV, %WM_SETFONT, hFont2, 0 CONTROL SEND hDlg, %IDC_BUTT_MUL, %WM_SETFONT, hFont2, 0 CONTROL SEND hDlg, %IDC_TEXT_ACC, %WM_SETFONT, hFont1, 0 #PBFORMS END DIALOG ghDlg = hDlg 'Store dialog handle in global to preserve it HiFP_Init DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt DeleteObject hFont1 DeleteObject hFont2 FUNCTION = lRslt END FUNCTION '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- ' Start of Fixed Point Numbers routines '-------------------------------------------------------------------------------- 'Convert from Fixed point (decimal) format to integer 'This means that we reformat the decimal number so that the number of fractional 'digits equal that specified in 'HiFP_Accuracy'. 'Then we remove the decimal point. 'Example: Suppose that HiFP_Accuracy equals 4 ' Input Normalised Output ' 4 4.0000 40000 ' 4.12 4.1200 41200 ' 4.12345 4.1234 41234 FUNCTION HiFP_FixedPoint2Integer(Num AS STRING) AS STRING LOCAL Digits_Integer AS STRING LOCAL Digits_Fraction AS STRING IF INSTR(Num, ".") > 0 THEN '== Num contains a decimal point Digits_Integer = PARSE$(Num, ".", 1) 'Get digits left of the decimal point Digits_Fraction = PARSE$(Num, ".", 2) 'Get digits right of the decimal point SELECT CASE LEN(Digits_Fraction) CASE > HiFP_Accuracy 'More digits in fraction than current accuracy --> truncate fraction Digits_Fraction = LEFT$(Digits_Fraction, HiFP_Accuracy) CASE < HiFP_Accuracy 'Less digits in fraction than current accuracy --> add zeroes Digits_Fraction = LEFT$(Digits_Fraction & HiFP_Accuracy_Zeroes, HiFP_Accuracy) CASE = 0 'Do nothing END SELECT ELSE '== Num does not contain a decimal point Digits_Integer = Num 'Get digits left of the decimal point (the entire number) Digits_Fraction = HiFP_Accuracy_Zeroes 'Get digits right of the decimal point (all zeroes here) END IF 'Compose the normalised number FUNCTION = Digits_Integer & Digits_Fraction END FUNCTION '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- 'Convert from (ascii) integer to Fixed point (decimal) format 'The input number (in decimal ascii integer format) is simply 'HiFP_Accuracy' times too big. 'Conversion is simply to insert a decimal point at the right place so that 'the number contains 'HiFP_Accuracy' digits right of the point. 'Example: Suppose that HiFP_Accuracy equals 4 ' Input Output ' 40000 4.0000 ' 12 0.0012 ' 412345 41.2345 FUNCTION HiFP_Integer2FixedPoint(Num AS STRING) AS STRING LOCAL Digits_Integer AS STRING LOCAL Digits_Fraction AS STRING 'Make sure that Num contains enough digits. If not, add leading zeroes IF LEN(Num) < (HiFP_Accuracy + 1) THEN 'Add leading zeroes Num = STRING$(HiFP_Accuracy + 1 - LEN(Num), "0") + Num END IF 'Insert decimal point Digits_Integer = LEFT$(Num, -HiFP_Accuracy) Digits_Fraction = RIGHT$(Num, HiFP_Accuracy) FUNCTION = Digits_Integer + "." + Digits_Fraction END FUNCTION '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- 'Fetch all data from the input textboxes and store in variables SUB HiFP_GetDataFromTextboxes LOCAL Temp AS STRING 'Get first number from textbox CONTROL GET TEXT ghDlg, %IDC_TEXT_NUM1 TO HiFP_Num1 'Get first number from textbox CONTROL GET TEXT ghDlg, %IDC_TEXT_NUM2 TO HiFP_Num2 'Get accuracy from textbox CONTROL GET TEXT ghDlg, %IDC_TEXT_ACC TO Temp HiFP_Accuracy = VAL(Temp) HiFP_Accuracy_Zeroes = STRING$(HiFP_Accuracy, "0") Temp = HiFP_Num1 + $CRLF + HiFP_Num2 END SUB '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- 'Do Fixed Point division SUB HiFP_Division LOCAL Temp AS STRING LOCAL Quot AS STRING 'Fetch all data from the input textboxes and store in variables HiFP_GetDataFromTextboxes 'Normalise the 2 numbers HiFP_Num1 = HiFP_FixedPoint2Integer(HiFP_Num1) HiFP_Num2 = HiFP_FixedPoint2Integer(HiFP_Num2) 'To keep the accuracy for the result, we must increase the accuracy of the dividend HiFP_Num1 = HiFP_Num1 + HiFP_Accuracy_Zeroes '.. and show them in the result box Temp = "Num 1 integer format: " + $CRLF + HiFP_Num1 Temp = Temp + $CRLF + "Num 2 integer format: " + $CRLF + HiFP_Num2 'Store the decimal numbers in HIME HiFP_SendNumbersToHIME 'Do the division hi_Div 1, 2, 3, 4 'Num1 \ Num2 --> Reg3 = quotient, Reg4 = Remainder 'Get quotient from HIME Quot = HiFP_GetNumberFromHIME(3) 'Get quotient (still in integer form though) Temp = Temp + $CRLF + "Quotient in integer format: " + $CRLF + Quot 'Convert quotient back to fixed point format Quot = HiFP_Integer2FixedPoint(Quot) Temp = Temp + $CRLF + "Quotient in fixed point format: " + $CRLF + Quot 'Show results in text box CONTROL SET TEXT ghDlg, %IDC_TEXT_RESULT, Temp END SUB '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- 'Do Fixed Point Addition SUB HiFP_Addition LOCAL Temp AS STRING LOCAL Additn AS STRING 'Fetch all data from the input textboxes and store in variables HiFP_GetDataFromTextboxes 'Normalise the 2 numbers HiFP_Num1 = HiFP_FixedPoint2Integer(HiFP_Num1) HiFP_Num2 = HiFP_FixedPoint2Integer(HiFP_Num2) ' '.. and show them in the result box Temp = "Num 1 integer format: " + $CRLF + HiFP_Num1 Temp = Temp + $CRLF + "Num 2 integer format: " + $CRLF + HiFP_Num2 'Store the decimal numbers in HIME HiFP_SendNumbersToHIME 'Do the addition hi_Add 1, 2, 3 'Num1 + Num2 --> Reg3 'Get result from HIME Additn = HiFP_GetNumberFromHIME(3) 'Get result (still in integer form though) Temp = Temp + $CRLF + "Result in integer format: " + $CRLF + Additn 'Convert result back to fixed point format Additn = HiFP_Integer2FixedPoint(Additn) Temp = Temp + $CRLF + "Result in fixed point format: " + $CRLF + Additn 'Show results in text box CONTROL SET TEXT ghDlg, %IDC_TEXT_RESULT, Temp END SUB '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- 'Do Fixed Point Subtraction SUB HiFP_Subtraction LOCAL Temp AS STRING LOCAL Subtr AS STRING 'Fetch all data from the input textboxes and store in variables HiFP_GetDataFromTextboxes 'Normalise the 2 numbers HiFP_Num1 = HiFP_FixedPoint2Integer(HiFP_Num1) HiFP_Num2 = HiFP_FixedPoint2Integer(HiFP_Num2) ' '.. and show them in the result box Temp = "Num 1 integer format: " + $CRLF + HiFP_Num1 Temp = Temp + $CRLF + "Num 2 integer format: " + $CRLF + HiFP_Num2 'Store the decimal numbers in HIME HiFP_SendNumbersToHIME 'Do the addition hi_Sub 1, 2, 3 'Num1 - Num2 --> Reg3 'Get result from HIME Subtr = HiFP_GetNumberFromHIME(3) 'Get result (still in integer form though) Temp = Temp + $CRLF + "Result in integer format: " + $CRLF + Subtr 'Convert result back to fixed point format Subtr = HiFP_Integer2FixedPoint(Subtr) Temp = Temp + $CRLF + "Result in fixed point format: " + $CRLF + Subtr 'Show results in text box CONTROL SET TEXT ghDlg, %IDC_TEXT_RESULT, Temp END SUB '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- 'Do Fixed Point Multiplication SUB HiFP_Multiplication LOCAL Temp AS STRING LOCAL Multi AS STRING 'Fetch all data from the input textboxes and store in variables HiFP_GetDataFromTextboxes 'Normalise the 2 numbers HiFP_Num1 = HiFP_FixedPoint2Integer(HiFP_Num1) HiFP_Num2 = HiFP_FixedPoint2Integer(HiFP_Num2) ' '.. and show them in the result box Temp = "Num 1 integer format: " + $CRLF + HiFP_Num1 Temp = Temp + $CRLF + "Num 2 integer format: " + $CRLF + HiFP_Num2 'Store the decimal numbers in HIME HiFP_SendNumbersToHIME 'Do the multiplication hi_Mul 1, 2, 3 'Num1 * Num2 --> Reg3 'Get result from HIME Multi = HiFP_GetNumberFromHIME(3) 'Get result (still in integer form though) Temp = Temp + $CRLF + "Result in integer format: " + $CRLF + Multi 'Result has the double accuracy of the 2 arguments. 'First reduce accuracy to the specified accuracy Multi = LEFT$(Multi, -HiFP_Accuracy) 'Convert result back to fixed point format Multi = HiFP_Integer2FixedPoint(Multi) Temp = Temp + $CRLF + "Result in fixed point format: " + $CRLF + Multi 'Show results in text box CONTROL SET TEXT ghDlg, %IDC_TEXT_RESULT, Temp END SUB '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- SUB HiFP_SendNumbersToHIME 'Store the decimal numbers in HIME hi_PutReg HiFP_Num1, 1 hi_PutReg HiFP_Num2, 2 'Convert from decimal to huge integer format hi_Dec2Huge 1, 1 hi_Dec2Huge 2, 2 END SUB '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- 'Get the number in specified register from HIME. 'Attention ! This number is converted to decimal in the same register. 'So original register contents will be lost because of the format conversion FUNCTION HiFP_GetNumberFromHIME(i AS LONG) AS STRING 'Convert from huge integer format to decimal hi_Huge2Dec i, i 'Get the decimal numbers from HIME FUNCTION = hi_GetReg(i) END FUNCTION '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- SUB HiFP_Init hi_RegClear 1 hi_HIMEParams_LoadFromFile 1 'Load HIME parameters from file 'Register HIME hi_PutReg("xxxxx-xxxxxxx", 1) <-- Put your license key here hi_Register 1 END SUB '--------------------------------------------------------------------------------
Comment