Symbian
Symbian OS Library

FAQ-0692 How can I handle problems occurring in math operations?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: Development
Created: 12/19/2000 Modified: 04/09/2002
Number: FAQ-0692
Platform: ER5, Symbian OS v6.0, Symbian OS v6.1

Question:
How can I handle problems like divide by zero and inappropriate arguments/operands occurring in math
functions/operations?


Answer:
As math functions and operations don't leave, they cannot be TRAPped. However most of the functions in the Math class have return values which can be checked, e.g

KErrArgument -6 An argument is out of range.
e.g. Log returns KErrArgument=-6 if called with a negative argument.
KErrOverflow -9 In the context of mathematical or time/date functions, indicates a calculation has produced arithmetic overflow over the bounds allowed by a representation.
KErrUnderflow -10 In the context of mathematical or time/date functions, indicates a calculation has produced a result smaller than the smallest magnitude of a finite number allowed by the representation.
KErrDivideByZero -41 A divide-by-zero operation was attempted.

You can't do anything about standard math operations like multiplication and division (other than checking the operands beforehand). However if you move over to high presision, TRealX provides methods replicating the functionality of all the standard math operations but returning integer error codes in the case of exceptions, that can be checked for and handled as you see fit. Documentation on TRealX can be found in the C++ SDK, but beware as the operator equivalent of these functions do not return an integer error code but will instead panic. Thus use:

TInt err = a.Div(c, b) rather than operator version c = a/b
TInt err = a.DivEq(b) rather than operator version a /= b