Overview
TheCCalcEngine class is the calculation engine that processes mathematical operations, maintains state, and performs infinite precision arithmetic using the RatPack library.
Constructor
Enable order of operations precedence (true for Scientific/Programmer, false for Standard)
Restrict to integer-only calculations (true for Programmer mode)
Provider for localized strings and resources
Optional display callback interface
Optional history display interface
Example
Initialization
InitialOneTimeOnlySetup()
Initializes shared global variables. Must be called once before creating any engine instances.Resource provider for loading engine strings
Example
Command Processing
ProcessCommand()
Processes a calculator command (digit, operator, function, etc.).The operation code to execute
Example
Display and Error Handling
DisplayError()
Displays an error message to the user.Error code (e.g., CALC_E_DOMAIN for invalid input)
FInErrorState()
Checks if the calculator is currently in an error state.True if in error state, false otherwise
FInRecordingState()
Checks if the engine is recording input (building an expression).True if recording, false if displaying a result
IsInputEmpty()
Checks if the current input is empty.True if input is empty or contains only “0”
Memory Operations
PersistedMemObject() - Get
Retrieves the persisted memory object.The current memory value as a Rational number
PersistedMemObject() - Set
Sets the persisted memory object.The memory value to persist
Example
Precision and Radix
ChangePrecision()
Changes the calculation precision.Number of digits of precision (16 for Standard, 32 for Scientific, 64 for Programmer)
GetCurrentRadix()
Gets the current number base.The current radix (2, 8, 10, or 16)
UpdateMaxIntDigits()
Updates the maximum integer digits based on current radix and word width.Display Formatting
GetCurrentResultForRadix()
Gets the current result formatted in a specific radix.The radix to format in (2, 8, 10, or 16)
Number of digits of precision
Whether to group digits with separators
Formatted string representation
GroupDigitsPerRadix()
Groups digits in a number string according to radix conventions.The number string to format
The radix (2, 8, 10, or 16)
Formatted string with digit grouping
- Decimal (10): Groups of 3 digits (e.g., “1,234,567”)
- Binary (2): Groups of 4 bits (e.g., “1010 1100”)
- Octal (8): Groups of 3 digits
- Hexadecimal (16): Groups of 4 digits
GetStringForDisplay()
Converts a Rational number to a display string.The rational number to convert
The radix to display in
String representation of the number
DecimalSeparator()
Gets the decimal separator character.The decimal separator character (e.g., ’.’ or ’,‘)
Trigonometry
IsCurrentTooBigForTrig()
Checks if the current value exceeds the maximum safe value for trigonometric functions.True if value is too large, false otherwise
History
GetHistoryCollectorCommandsSnapshot()
Gets a snapshot of expression commands for history display.Vector of expression commands
String Utilities
GetString()
Gets a localized string by ID.String resource ID
The localized string
GetString() - by name
String resource name
OpCodeToString()
Converts an operation code to its string representation.The operation code
String representation (e.g., ”+”, ”×”, “sin”)
OpCodeToUnaryString()
Converts a unary operation to its string representation.The operation code
Whether inverse function is active
Current angle type (Degrees, Radians, Gradians)
OpCodeToBinaryString()
Converts a binary operation to its string representation.The operation code
Whether in integer mode
Enumerations
NUM_WIDTH
Defines word size for programmer mode.NumberFormat
Defines number formatting styles.AngleType
Defines angle units for trigonometric functions.Internal State
The engine maintains:- Current value (
m_currentVal): Currently displayed number - Last value (
m_lastVal): Left operand of pending operation - Hold value (
m_holdVal): For repetitive calculations (pressing ”=” multiple times) - Parenthesis stack (
m_parenVals): Values saved when opening parentheses - Precedence stack (
m_precedenceVals): Values for order of operations - Operation state: Current and previous operations, flags for inverse, error, etc.
See Also
- CalculatorManager - High-level calculator interface
- RatPack - Infinite precision arithmetic library