FORWARD INTEGER PROC FNIntegerGetMathFloatCompareI( STRING s1, STRING s2 ) FORWARD PROC Main() FORWARD STRING PROC FNStringGetMathFloatAbsoluteS( STRING s1 ) FORWARD STRING PROC FNStringGetMathFloatAddS( STRING s1, STRING s2 ) FORWARD STRING PROC FNStringGetMathFloatDivideS( STRING s1, STRING s2 ) FORWARD STRING PROC FNStringGetMathFloatMultiplyS( STRING s1, STRING s2 ) FORWARD STRING PROC FNStringGetMathFloatSquareRootS( STRING s1 ) FORWARD STRING PROC FNStringGetMathFloatSubtractS( STRING s1, STRING s2 ) // --- MAIN --- // PROC Main() // STRING s1[255] = GetHistoryStr( _EDIT_HISTORY_, 1 ) // change this STRING s1[255] = "2" // change this IF ( NOT ( Ask( " = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF Warn( FNStringGetMathFloatSquareRootS( s1 ) ) // gives e.g. "1.4142135" CopyToWinClip( FNStringGetMathFloatSquareRootS( s1 ) ) END Main() // --- LIBRARY --- // // library: string: get: math: float: square: root Function to compute the square root of a floating-point number using Newton's method 1.0.0.0.22 (filenamemacro=getstsrv.s) [] [] [kn, ri, mo, 20-01-2025 02:20:10] STRING PROC FNStringGetMathFloatSquareRootS( STRING numS ) // e.g. PROC Main() // e.g. // STRING s1[255] = GetHistoryStr( _EDIT_HISTORY_, 1 ) // change this // e.g. STRING s1[255] = "2" // change this // e.g. IF ( NOT ( Ask( " = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF // e.g. Warn( FNStringGetMathFloatSquareRootS( s1 ) ) // gives e.g. "1.4142135" // e.g. CopyToWinClip( FNStringGetMathFloatSquareRootS( s1 ) ) // e.g. END // e.g. // e.g. Main() // // === // // Use case = calculate the floating point square root for input values from 0 to maximum 214748 // // === // // === // // Method = // // === // // === // // Example: // // Input: // /* --- cut here: begin -------------------------------------------------- --- cut here: end ---------------------------------------------------- */ // // Output: // /* --- cut here: begin -------------------------------------------------- --- cut here: end ---------------------------------------------------- */ // // === // // e.g. // QuickHelp( HELPDEFFNStringGetMathFloatSquareRootS ) // e.g. HELPDEF HELPDEFFNStringGetMathFloatSquareRootS // e.g. title = "FNStringGetMathFloatSquareRootS( s1 ) help" // The help's caption // e.g. x = 100 // Location // e.g. y = 3 // Location // e.g. // // e.g. // The actual help text // e.g. // // e.g. "Usage:" // e.g. "//" // e.g. "1. Run this TSE macro" // e.g. "2. Then press to show this help." // e.g. "3. Press to quit." // e.g. "//" // e.g. "" // e.g. "Key: Definitions:" // e.g. "" // e.g. "<> = do something" // e.g. END // // Declare all variables at the beginning of the function // STRING guessS[255] = "1.0" STRING prevGuessS[255] = "" STRING toleranceS[255] = "0.0000000001" // Tolerance for convergence STRING diffS[255] = "0.0" INTEGER maxIterationsI = 100 // Maximum iterations for Newton's method INTEGER iI = 0 STRING s[255] = "" STRING s1[255] = "" STRING s2[255] = "" STRING s3[255] = "" STRING numTempS[255] = numS // Local copy to avoid modifying the parameter STRING intPartS[255] = "" STRING fracPartS[255] = "" INTEGER dotPosI = 0 INTEGER maxInputValueI = 2147 // Maximum input value to avoid overflow STRING scaleFactorS[255] = "100.0" // Scaling factor (must be a perfect square) STRING scaleResultS[255] = "10.0" // SquareRoot(scaleFactorS) = 10.0 INTEGER isScaledI = 0 // // Handle invalid input (SquareRoot(x) is undefined for x < 0) IF ( FNIntegerGetMathFloatCompareI( numTempS, "0" ) == -1 ) Warn("Error: Square root is undefined for negative numbers.") RETURN("0.0") ENDIF // // Handle edge case: SquareRoot(0) = 0 IF numTempS == "0" RETURN("0.0") ENDIF // // Split into integer and fractional parts dotPosI = Pos(".", numTempS) IF dotPosI > 0 intPartS = SubStr(numTempS, 1, dotPosI - 1) fracPartS = SubStr(numTempS, dotPosI + 1, Length(numTempS) - dotPosI) ELSE intPartS = numTempS fracPartS = "0" ENDIF // // Check if the integer part exceeds the maximum allowed value IF Val(intPartS) > maxInputValueI // Scale down the number to avoid overflow numTempS = FNStringGetMathFloatDivideS( numTempS, scaleFactorS ) isScaledI = 1 ENDIF // // Use Newton's method to compute the square root FOR iI = 1 TO maxIterationsI prevGuessS = guessS s1 = FNStringGetMathFloatDivideS( numTempS, guessS ) // numTempS / guessS s2 = FNStringGetMathFloatAddS( guessS, s1 ) // guessS + (numTempS / guessS) s3 = FNStringGetMathFloatDivideS( s2, "2.0" ) // (guessS + (numTempS / guessS)) / 2 guessS = s3 // // Check for convergence diffS = FNStringGetMathFloatSubtractS( guessS, prevGuessS ) s = FNStringGetMathFloatAbsoluteS( diffS ) IF ( FNIntegerGetMathFloatCompareI( s, toleranceS ) == -1 ) BREAK ENDIF ENDFOR // // Scale the result back up if the input was scaled down IF isScaledI guessS = FNStringGetMathFloatMultiplyS( guessS, scaleResultS ) ENDIF // // Remove trailing zeros after the decimal point dotPosI = Pos(".", guessS) IF dotPosI > 0 WHILE SubStr(guessS, Length(guessS), 1) == "0" guessS = SubStr(guessS, 1, Length(guessS) - 1) ENDWHILE IF SubStr(guessS, Length(guessS), 1) == "." guessS = SubStr(guessS, 1, Length(guessS) - 1) ENDIF ENDIF // RETURN( guessS ) END // library: integer: get: math: float: compare Function to compare two floating-point numbers 1.0.0.0.4 (filenamemacro=getstfcs.s) [] [] [kn, ri, mo, 20-01-2025 03:43:20] INTEGER PROC FNIntegerGetMathFloatCompareI( STRING num1S, STRING num2S ) // e.g. INTEGER bufferGI = 0 // global variable // e.g. PROC Main() // e.g. // Declare all variables immediately after the procedure header // e.g. STRING num1S[255] = "" // e.g. STRING num2S[255] = "" // e.g. INTEGER resultI = 0 // e.g. // // e.g. // Save the current position in the editor // e.g. PushPosition() // e.g. // // e.g. // Create a temporary buffer for debugging // e.g. bufferGI = CreateTempBuffer() // e.g. // // e.g. // Restore the original position // e.g. PopPosition() // e.g. // // e.g. // Ask the user for the first floating-point number // e.g. IF Ask("Enter the first floating-point number: ", num1S) // e.g. // Ask the user for the second floating-point number // e.g. IF Ask("Enter the second floating-point number: ", num2S) // e.g. // Compare the two numbers // e.g. resultI = FNIntegerGetMathFloatCompareI( num1S, num2S ) // e.g. // // e.g. // Display the result // e.g. IF resultI == -1 // e.g. Warn(num1S, " is smaller than ", num2S) // e.g. AddLine(Format(num1S, " is smaller than ", num2S), bufferGI) // e.g. ELSEIF resultI == 0 // e.g. Warn(num1S, " is equal to ", num2S) // e.g. AddLine(Format(num1S, " is equal to ", num2S), bufferGI) // e.g. ELSE // e.g. Warn(num1S, " is larger than ", num2S) // e.g. AddLine(Format(num1S, " is larger than ", num2S), bufferGI) // e.g. ENDIF // e.g. ENDIF // e.g. ENDIF // e.g. // // e.g. // Navigate to the debugging buffer at the end of the program // e.g. GotoBufferId( bufferGI ) // e.g. END // // Declare all variables immediately after the function header INTEGER dotPos1I = 0 INTEGER dotPos2I = 0 STRING intPart1S[255] = "" STRING intPart2S[255] = "" STRING fracPart1S[255] = "" STRING fracPart2S[255] = "" INTEGER resultI = 0 // // Find the position of the decimal point in each number dotPos1I = Pos(".", num1S) dotPos2I = Pos(".", num2S) // // Split the numbers into integer and fractional parts IF dotPos1I > 0 intPart1S = SubStr(num1S, 1, dotPos1I - 1) fracPart1S = SubStr(num1S, dotPos1I + 1, Length(num1S) - dotPos1I) ELSE intPart1S = num1S fracPart1S = "0" ENDIF // IF dotPos2I > 0 intPart2S = SubStr(num2S, 1, dotPos2I - 1) fracPart2S = SubStr(num2S, dotPos2I + 1, Length(num2S) - dotPos2I) ELSE intPart2S = num2S fracPart2S = "0" ENDIF // // Compare integer parts IF Val(intPart1S) < Val(intPart2S) resultI = -1 ELSEIF Val(intPart1S) > Val(intPart2S) resultI = 1 ELSE // Compare fractional parts if integer parts are equal IF Val(fracPart1S) < Val(fracPart2S) resultI = -1 ELSEIF Val(fracPart1S) > Val(fracPart2S) resultI = 1 ELSE resultI = 0 ENDIF ENDIF // RETURN(resultI) // END // library: string: get: math: float: divide Function to divide two floating-point numbers represented as strings 1.0.0.0.5 (filenamemacro=getstfdn.s) [] [] [kn, ri, mo, 20-01-2025 01:41:41] STRING PROC FNStringGetMathFloatDivideS( STRING num1S, STRING num2S ) // e.g. PROC Main() // e.g. STRING num1S[255] = "2.8333333333" // e.g. STRING num2S[255] = "2.0" // e.g. STRING resultS[255] = "" // e.g. // // e.g. // Ask the user for the first number // e.g. IF Ask("Enter the first number: ", num1S) // e.g. // Ask the user for the second number // e.g. IF Ask("Enter the second number: ", num2S) // e.g. // Perform the division and display the result // e.g. resultS = FNStringGetMathFloatDivideS( num1S, num2S ) // e.g. Warn("Result: ", resultS) // e.g. CopyToWinClip( resultS ) // e.g. ELSE // e.g. Warn("Second number input was canceled.") // e.g. ENDIF // e.g. ELSE // e.g. Warn("First number input was canceled.") // e.g. ENDIF // e.g. END // // Declare all variables at the beginning of the function // STRING intPart1S[255] = "" STRING fracPart1S[255] = "" STRING intPart2S[255] = "" STRING fracPart2S[255] = "" STRING resultS[255] = "" INTEGER dotPos1I = 0 INTEGER dotPos2I = 0 INTEGER decimalPlaces1I = 0 INTEGER decimalPlaces2I = 0 INTEGER isNegative1I = 0 INTEGER isNegative2I = 0 INTEGER isResultNegativeI = 0 STRING num1TempS[255] = num1S // Create a local copy of num1S STRING num2TempS[255] = num2S // Create a local copy of num2S STRING combined1S[255] = "" STRING combined2S[255] = "" INTEGER iI = 0 INTEGER remainderI = 0 INTEGER quotientI = 0 INTEGER precisionI = 10 // Number of decimal places in the result INTEGER dotPosI = 0 INTEGER divisorI = 0 INTEGER dividendI = 0 INTEGER maxDecimalsI = 6 // Maximum number of decimal places to avoid overflow // // Determine if the numbers are negative IF SubStr(num1TempS, 1, 1) == "-" isNegative1I = 1 num1TempS = SubStr(num1TempS, 2, Length(num1TempS) - 1) // Remove the negative sign ENDIF // IF SubStr(num2TempS, 1, 1) == "-" isNegative2I = 1 num2TempS = SubStr(num2TempS, 2, Length(num2TempS) - 1) // Remove the negative sign ENDIF // // Determine the sign of the result isResultNegativeI = (isNegative1I AND NOT isNegative2I) OR (NOT isNegative1I AND isNegative2I) // // Split the input numbers into integer and fractional parts dotPos1I = Pos(".", num1TempS) IF dotPos1I > 0 intPart1S = SubStr(num1TempS, 1, dotPos1I - 1) fracPart1S = SubStr(num1TempS, dotPos1I + 1, Length(num1TempS) - dotPos1I) ELSE intPart1S = num1TempS fracPart1S = "0" ENDIF // dotPos2I = Pos(".", num2TempS) IF dotPos2I > 0 intPart2S = SubStr(num2TempS, 1, dotPos2I - 1) fracPart2S = SubStr(num2TempS, dotPos2I + 1, Length(num2TempS) - dotPos2I) ELSE intPart2S = num2TempS fracPart2S = "0" ENDIF // // Truncate fractional parts to a maximum of `maxDecimalsI` decimal places IF Length(fracPart1S) > maxDecimalsI fracPart1S = SubStr(fracPart1S, 1, maxDecimalsI) ENDIF // IF Length(fracPart2S) > maxDecimalsI fracPart2S = SubStr(fracPart2S, 1, maxDecimalsI) ENDIF // // Remove leading zeros from integer parts WHILE SubStr(intPart1S, 1, 1) == "0" AND Length(intPart1S) > 1 intPart1S = SubStr(intPart1S, 2, Length(intPart1S) - 1) ENDWHILE // WHILE SubStr(intPart2S, 1, 1) == "0" AND Length(intPart2S) > 1 intPart2S = SubStr(intPart2S, 2, Length(intPart2S) - 1) ENDWHILE // // Remove trailing zeros from fractional parts WHILE SubStr(fracPart1S, Length(fracPart1S), 1) == "0" AND Length(fracPart1S) > 1 fracPart1S = SubStr(fracPart1S, 1, Length(fracPart1S) - 1) ENDWHILE // WHILE SubStr(fracPart2S, Length(fracPart2S), 1) == "0" AND Length(fracPart2S) > 1 fracPart2S = SubStr(fracPart2S, 1, Length(fracPart2S) - 1) ENDWHILE // // Combine integer and fractional parts into single strings combined1S = intPart1S + fracPart1S combined2S = intPart2S + fracPart2S // // Calculate the number of decimal places decimalPlaces1I = Length(fracPart1S) decimalPlaces2I = Length(fracPart2S) // // Pad the shorter number with zeros to make lengths equal IF decimalPlaces1I > decimalPlaces2I FOR iI = 1 TO (decimalPlaces1I - decimalPlaces2I) combined2S = combined2S + "0" ENDFOR ELSEIF decimalPlaces2I > decimalPlaces1I FOR iI = 1 TO (decimalPlaces2I - decimalPlaces1I) combined1S = combined1S + "0" ENDFOR ENDIF // // Convert combined strings to integers dividendI = Val(combined1S) divisorI = Val(combined2S) // // Handle division by zero IF divisorI == 0 Warn("Error: Division by zero.") RETURN("0") ENDIF // // Perform division quotientI = dividendI / divisorI remainderI = dividendI MOD divisorI // // Convert the quotient to a string resultS = Str(quotientI) // // Handle the fractional part IF remainderI > 0 resultS = resultS + "." FOR iI = 1 TO precisionI remainderI = remainderI * 10 quotientI = remainderI / divisorI resultS = resultS + Str(quotientI) remainderI = remainderI MOD divisorI ENDFOR ENDIF // // Remove trailing zeros after the decimal point dotPosI = Pos(".", resultS) IF dotPosI > 0 WHILE SubStr(resultS, Length(resultS), 1) == "0" resultS = SubStr(resultS, 1, Length(resultS) - 1) ENDWHILE IF SubStr(resultS, Length(resultS), 1) == "." resultS = SubStr(resultS, 1, Length(resultS) - 1) ENDIF ENDIF // // Add the negative sign if the result is negative IF isResultNegativeI resultS = "-" + resultS ENDIF // RETURN( resultS ) // END // library: string: get: math: float: add Function to add two floating-point numbers represented as strings 1.0.0.0.3 (filenamemacro=getstfae.s) [] [] [kn, ri, mo, 20-01-2025 01:22:38] STRING PROC FNStringGetMathFloatAddS( STRING num1S, STRING num2S ) // e.g. PROC Main() // e.g. // Declare and initialize all variables immediately after the function header // e.g. STRING num1S[255] = "" // e.g. STRING num2S[255] = "" // e.g. STRING resultS[255] = "" // e.g. // // e.g. // Ask the user for input // e.g. IF Ask("Enter the first floating-point number: ", num1S) AND Ask("Enter the second floating-point number: ", num2S) // e.g. // Call the floating-point addition function // e.g. resultS = FNStringGetMathFloatAddS( num1S, num2S ) // e.g. CopyToWinClip( resultS ) // e.g. // // e.g. // Display the result // e.g. Warn("The sum is: ", resultS) // e.g. ELSE // e.g. Warn("Invalid input.") // e.g. ENDIF // e.g. END // // Declare and initialize all variables immediately after the function header // STRING intPart1S[255] = "" STRING fracPart1S[255] = "" STRING intPart2S[255] = "" STRING fracPart2S[255] = "" STRING resultS[255] = "" STRING tempNum1S[255] = num1S // Create a modifiable copy of num1S STRING tempNum2S[255] = num2S // Create a modifiable copy of num2S INTEGER dotPos1I = 0 INTEGER dotPos2I = 0 INTEGER maxFracLenI = 0 INTEGER carryI = 0 INTEGER iI = 0 INTEGER digit1I = 0 INTEGER digit2I = 0 INTEGER sumI = 0 INTEGER isNegative1I = 0 INTEGER isNegative2I = 0 INTEGER isResultNegativeI = 0 INTEGER isSwapI = 0 STRING tempS[255] = "" // // Check if tempNum1S is negative IF SubStr(tempNum1S, 1, 1) == "-" isNegative1I = 1 tempNum1S = SubStr(tempNum1S, 2, Length(tempNum1S) - 1) // Remove the negative sign ENDIF // // Check if tempNum2S is negative IF SubStr(tempNum2S, 1, 1) == "-" isNegative2I = 1 tempNum2S = SubStr(tempNum2S, 2, Length(tempNum2S) - 1) // Remove the negative sign ENDIF // // Split tempNum1S into integer and fractional parts dotPos1I = Pos(".", tempNum1S) IF dotPos1I > 0 intPart1S = SubStr(tempNum1S, 1, dotPos1I - 1) fracPart1S = SubStr(tempNum1S, dotPos1I + 1, Length(tempNum1S) - dotPos1I) ELSE intPart1S = tempNum1S fracPart1S = "0" ENDIF // // Split tempNum2S into integer and fractional parts dotPos2I = Pos(".", tempNum2S) IF dotPos2I > 0 intPart2S = SubStr(tempNum2S, 1, dotPos2I - 1) fracPart2S = SubStr(tempNum2S, dotPos2I + 1, Length(tempNum2S) - dotPos2I) ELSE intPart2S = tempNum2S fracPart2S = "0" ENDIF // // Pad fractional parts to the same length WHILE Length(fracPart1S) < Length(fracPart2S) fracPart1S = fracPart1S + "0" ENDWHILE // WHILE Length(fracPart2S) < Length(fracPart1S) fracPart2S = fracPart2S + "0" ENDWHILE // // Pad integer parts to the same length WHILE Length(intPart1S) < Length(intPart2S) intPart1S = "0" + intPart1S ENDWHILE // WHILE Length(intPart2S) < Length(intPart1S) intPart2S = "0" + intPart2S ENDWHILE // // Determine if we need to swap the numbers for subtraction IF NOT( isNegative1I == isNegative2I ) // Compare the absolute values of the two numbers IF (intPart1S + fracPart1S) < (intPart2S + fracPart2S) // Swap the numbers tempS[255] = intPart1S intPart1S = intPart2S intPart2S = tempS tempS = fracPart1S fracPart1S = fracPart2S fracPart2S = tempS isSwapI = 1 ENDIF ENDIF // // Determine if the result should be negative IF isNegative1I == isNegative2I // Both numbers have the same sign; result will have the same sign isResultNegativeI = isNegative1I ELSE // Numbers have different signs; result will have the sign of the larger absolute value IF isSwapI == 1 isResultNegativeI = isNegative2I ELSE isResultNegativeI = isNegative1I ENDIF ENDIF // // Perform manual addition for fractional parts resultS = "" carryI = 0 maxFracLenI = Length(fracPart1S) // FOR iI = maxFracLenI DOWNTO 1 digit1I = Val(SubStr(fracPart1S, iI, 1)) digit2I = Val(SubStr(fracPart2S, iI, 1)) IF isNegative1I == isNegative2I sumI = digit1I + digit2I + carryI ELSE sumI = digit1I - digit2I + carryI ENDIF IF sumI < 0 sumI = sumI + 10 carryI = -1 ELSE carryI = sumI / 10 ENDIF resultS = Str(sumI mod 10) + resultS ENDFOR // // Add the decimal point resultS = "." + resultS // // Perform manual addition for integer parts FOR iI = Length(intPart1S) DOWNTO 1 digit1I = Val(SubStr(intPart1S, iI, 1)) digit2I = Val(SubStr(intPart2S, iI, 1)) IF isNegative1I == isNegative2I sumI = digit1I + digit2I + carryI ELSE sumI = digit1I - digit2I + carryI ENDIF IF sumI < 0 sumI = sumI + 10 carryI = -1 ELSE carryI = sumI / 10 ENDIF resultS = Str(sumI mod 10) + resultS ENDFOR // // Add any remaining carry IF carryI > 0 resultS = Str(carryI) + resultS ENDIF // // Remove leading zeros from the integer part WHILE SubStr(resultS, 1, 1) == "0" AND Length(resultS) > 1 AND NOT(SubStr(resultS, 2, 1) == ".") resultS = SubStr(resultS, 2, Length(resultS) - 1) ENDWHILE // // Add the negative sign if necessary IF isResultNegativeI == 1 resultS = "-" + resultS ENDIF // // Return the result RETURN( resultS ) // END // library: string: get: math: float: subtract 1.0.0.0.1 (filenamemacro=getstfsy.s) [] [] [kn, ri, mo, 20-01-2025 01:50:16] STRING PROC FNStringGetMathFloatSubtractS( STRING num1S, STRING num2S ) // e.g. PROC Main() // e.g. STRING s1[255] = "" // change this // e.g. STRING s2[255] = "" // change this // e.g. IF ( NOT ( Ask( " = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF // e.g. IF ( NOT ( Ask( " = ", s2, _EDIT_HISTORY_ ) ) AND ( Length( s2 ) > 0 ) ) RETURN() ENDIF // e.g. Warn( FNStringGetMathFloatSubtractS( s1, s2 ) ) // gives e.g. ..."" // e.g. END // e.g. // e.g. Main() // // === // // Use case = // // === // // === // // Method = // // === // // === // // Example: // // Input: // /* --- cut here: begin -------------------------------------------------- --- cut here: end ---------------------------------------------------- */ // // Output: // /* --- cut here: begin -------------------------------------------------- --- cut here: end ---------------------------------------------------- */ // // === // // e.g. // QuickHelp( HELPDEFFNStringGetMathFloatSubtractS ) // e.g. HELPDEF HELPDEFFNStringGetMathFloatSubtractS // e.g. title = "FNStringGetMathFloatSubtractS( s1, s2 ) help" // The help's caption // e.g. x = 100 // Location // e.g. y = 3 // Location // e.g. // // e.g. // The actual help text // e.g. // // e.g. "Usage:" // e.g. "//" // e.g. "1. Run this TSE macro" // e.g. "2. Then press to show this help." // e.g. "3. Press to quit." // e.g. "//" // e.g. "" // e.g. "Key: Definitions:" // e.g. "" // e.g. "<> = do something" // e.g. END // STRING resultS[255] = "" // resultS = FNStringGetMathFloatAddS( num1S, "-" + num2S ) // RETURN( resultS ) // END // library: string: get: math: float: absolute 1.0.0.0.2 (filenamemacro=getstfab.s) [] [] [kn, ri, mo, 20-01-2025 01:55:23] STRING PROC FNStringGetMathFloatAbsoluteS( STRING numS ) // e.g. PROC Main() // e.g. STRING s1[255] = "-1.0000" // change this // e.g. IF ( NOT ( Ask( " = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF // e.g. Warn( FNStringGetMathFloatAbsoluteS( s1 ) ) // gives e.g. "1.0000" // e.g. END // e.g. // e.g. Main() // // === // // Use case = // // === // // === // // Method = // // === // // === // // Example: // // Input: // /* --- cut here: begin -------------------------------------------------- --- cut here: end ---------------------------------------------------- */ // // Output: // /* --- cut here: begin -------------------------------------------------- --- cut here: end ---------------------------------------------------- */ // // === // // e.g. // QuickHelp( HELPDEFFNStringGetMathFloatAbsoluteS ) // e.g. HELPDEF HELPDEFFNStringGetMathFloatAbsoluteS // e.g. title = "FNStringGetMathFloatAbsoluteS( s1 ) help" // The help's caption // e.g. x = 100 // Location // e.g. y = 3 // Location // e.g. // // e.g. // The actual help text // e.g. // // e.g. "Usage:" // e.g. "//" // e.g. "1. Run this TSE macro" // e.g. "2. Then press to show this help." // e.g. "3. Press to quit." // e.g. "//" // e.g. "" // e.g. "Key: Definitions:" // e.g. "" // e.g. "<> = do something" // e.g. END // IF SubStr(numS, 1, 1) == "-" // RETURN( SubStr( numS, 2, Length( numS ) - 1 ) ) // ENDIF // RETURN( numS ) // END // library: string: get: math: float: multiply Function to multiply two floating-point numbers represented as strings 1.0.0.0.2 (filenamemacro=getstfmu.s) [] [] [kn, ri, mo, 20-01-2025 01:30:16] STRING PROC FNStringGetMathFloatMultiplyS( STRING num1S, STRING num2S ) // e.g. PROC Main() // e.g. STRING num1S[255] = "" // e.g. STRING num2S[255] = "" // e.g. STRING resultS[255] = "" // e.g. // e.g. // Ask the user for the first number // e.g. IF Ask( "Enter the first number: ", num1S ) // e.g. // Ask the user for the second number // e.g. IF Ask( "Enter the second number: ", num2S ) // e.g. // Perform the multiplication and display the result // e.g. resultS = FNStringGetMathFloatMultiplyS( num1S, num2S ) // e.g. Warn( "Result: ", resultS ) // e.g. ELSE // e.g. Warn( "Second number input was canceled." ) // e.g. ENDIF // e.g. ELSE // e.g. Warn( "First number input was canceled." ) // e.g. ENDIF // e.g. END // // Declare all variables at the beginning of the function // STRING intPart1S[255] = "" STRING fracPart1S[255] = "" STRING intPart2S[255] = "" STRING fracPart2S[255] = "" STRING resultS[255] = "" INTEGER dotPos1I = 0 INTEGER dotPos2I = 0 INTEGER decimalPlaces1I = 0 INTEGER decimalPlaces2I = 0 INTEGER totalDecimalPlacesI = 0 INTEGER isNegative1I = 0 INTEGER isNegative2I = 0 INTEGER isResultNegativeI = 0 STRING num1TempS[255] = num1S // Create a local copy of num1S STRING num2TempS[255] = num2S // Create a local copy of num2S STRING combined1S[255] = "" STRING combined2S[255] = "" STRING productS[255] = "0" INTEGER len1I = 0 INTEGER len2I = 0 INTEGER iI = 0 INTEGER jI = 0 INTEGER kI = 0 INTEGER lI = 0 INTEGER digit1I = 0 INTEGER digit2I = 0 INTEGER carryI = 0 INTEGER productI = 0 STRING tempProductS[255] = "" INTEGER carryAddI = 0 STRING sumS[255] = "" INTEGER lenTempI = 0 INTEGER lenProdI = 0 INTEGER digitTempI = 0 INTEGER digitProdI = 0 INTEGER sumI = 0 INTEGER productLenI = 0 STRING leadingZerosS[255] = "" INTEGER dotPosI = 0 // // Determine if the numbers are negative IF SubStr(num1TempS, 1, 1) == "-" isNegative1I = 1 num1TempS = SubStr(num1TempS, 2, Length(num1TempS) - 1) // Remove the negative sign ENDIF // IF SubStr(num2TempS, 1, 1) == "-" isNegative2I = 1 num2TempS = SubStr(num2TempS, 2, Length(num2TempS) - 1) // Remove the negative sign ENDIF // // Determine the sign of the result // XOR equivalent: (isNegative1I AND NOT isNegative2I) OR (NOT isNegative1I AND isNegative2I) isResultNegativeI = (isNegative1I AND NOT isNegative2I) OR (NOT isNegative1I AND isNegative2I) // // Split the input numbers into integer and fractional parts dotPos1I = Pos(".", num1TempS) dotPos2I = Pos(".", num2TempS) // IF dotPos1I > 0 intPart1S = SubStr(num1TempS, 1, dotPos1I - 1) fracPart1S = SubStr(num1TempS, dotPos1I + 1, Length(num1TempS) - dotPos1I) ELSE intPart1S = num1TempS fracPart1S = "0" ENDIF // IF dotPos2I > 0 intPart2S = SubStr(num2TempS, 1, dotPos2I - 1) fracPart2S = SubStr(num2TempS, dotPos2I + 1, Length(num2TempS) - dotPos2I) ELSE intPart2S = num2TempS fracPart2S = "0" ENDIF // // Remove leading zeros from integer parts WHILE SubStr(intPart1S, 1, 1) == "0" AND Length(intPart1S) > 1 intPart1S = SubStr(intPart1S, 2, Length(intPart1S) - 1) ENDWHILE // WHILE SubStr(intPart2S, 1, 1) == "0" AND Length(intPart2S) > 1 intPart2S = SubStr(intPart2S, 2, Length(intPart2S) - 1) ENDWHILE // // Remove trailing zeros from fractional parts WHILE SubStr(fracPart1S, Length(fracPart1S), 1) == "0" AND Length(fracPart1S) > 1 fracPart1S = SubStr(fracPart1S, 1, Length(fracPart1S) - 1) ENDWHILE // WHILE SubStr(fracPart2S, Length(fracPart2S), 1) == "0" AND Length(fracPart2S) > 1 fracPart2S = SubStr(fracPart2S, 1, Length(fracPart2S) - 1) ENDWHILE // // Calculate the number of decimal places decimalPlaces1I = Length(fracPart1S) decimalPlaces2I = Length(fracPart2S) totalDecimalPlacesI = decimalPlaces1I + decimalPlaces2I // // Combine integer and fractional parts into single strings combined1S = intPart1S + fracPart1S combined2S = intPart2S + fracPart2S // // Perform string-based multiplication len1I = Length(combined1S) len2I = Length(combined2S) // FOR iI = len1I DOWNTO 1 digit1I = Val(SubStr(combined1S, iI, 1)) carryI = 0 tempProductS = "" FOR jI = len2I DOWNTO 1 digit2I = Val(SubStr(combined2S, jI, 1)) productI = digit1I * digit2I + carryI carryI = productI / 10 tempProductS = Str(productI MOD 10) + tempProductS ENDFOR IF carryI > 0 tempProductS = Str(carryI) + tempProductS ENDIF // Add zeros to the end of tempProductS based on the position of iI FOR kI = 1 TO (len1I - iI) tempProductS = tempProductS + "0" ENDFOR // Add tempProductS to productS carryAddI = 0 sumS = "" lenTempI = Length(tempProductS) lenProdI = Length(productS) FOR lI = 1 TO Max(lenTempI, lenProdI) // Get digit from tempProductS IF lI <= lenTempI digitTempI = Val(SubStr(tempProductS, lenTempI - lI + 1, 1)) ELSE digitTempI = 0 ENDIF // // Get digit from productS IF lI <= lenProdI digitProdI = Val(SubStr(productS, lenProdI - lI + 1, 1)) ELSE digitProdI = 0 ENDIF // // Calculate sum and carry sumI = digitTempI + digitProdI + carryAddI carryAddI = sumI / 10 sumS = Str(sumI MOD 10) + sumS ENDFOR IF carryAddI > 0 sumS = Str(carryAddI) + sumS ENDIF productS = sumS ENDFOR // // Insert the decimal point at the correct position productLenI = Length(productS) IF totalDecimalPlacesI > 0 IF productLenI > totalDecimalPlacesI resultS = SubStr(productS, 1, productLenI - totalDecimalPlacesI) + "." + SubStr(productS, productLenI - totalDecimalPlacesI + 1, totalDecimalPlacesI) ELSE // Manually add leading zeros leadingZerosS = "" FOR iI = 1 TO (totalDecimalPlacesI - productLenI) leadingZerosS = leadingZerosS + "0" ENDFOR resultS = "0." + leadingZerosS + productS ENDIF ELSE resultS = productS ENDIF // // Remove trailing zeros after the decimal point dotPosI = Pos(".", resultS) IF dotPosI > 0 WHILE SubStr(resultS, Length(resultS), 1) == "0" resultS = SubStr(resultS, 1, Length(resultS) - 1) ENDWHILE IF SubStr(resultS, Length(resultS), 1) == "." resultS = SubStr(resultS, 1, Length(resultS) - 1) ENDIF ENDIF // // Add the negative sign if the result is negative IF isResultNegativeI resultS = "-" + resultS ENDIF // RETURN( resultS ) // END