HomeLinuxFloating Level Exception C++ Why and what's it?

Floating Level Exception C++ Why and what’s it?


Floating-point exception happens when a software program tries to carry out an improper operation on a numerical quantity. Such a exception happens when a consumer makes an attempt to execute invalid operations, comparable to division by zero, division of a unfavourable quantity by an excellent quantity, taking the sq. root of a unfavourable quantity, or overflow of a calculation that exceeds the restrict of the double knowledge kind.

In C++, the “SIGFPE” sign handler handles the floating level exceptions (FPEs). When a consumer makes an attempt to hold out the talked about duties, this sign handler is named. As soon as the sign handler has intervened, it prints an error message to straightforward output and halts this system.

Why Do Floating-Level Exceptions Happen?

Floating level exceptions can happen as a consequence of programming errors or when a program makes an attempt to course of a price that’s out of specification. For instance, if a program tries to divide an integer by zero, or if a program tries to take the sq. root of a unfavourable quantity, a floating level exception will happen. Moreover, some floating-point exceptions can happen as a consequence of processor misdetections.

Many elements, comparable to inappropriate operation, underflow, overflow, division by zero, and accuracy, would possibly lead to a floating-point exception. We’ll cowl these arguments one after the other on this part.

1: Unlawful Operation

When a consumer forgets to precise an operation or the operation has no mathematical worth, this system fails to execute as a consequence of an invalid operation. This contains calculations such because the sq. root and logarithm of unfavourable numbers, for example. Though it’s potential to take the sq. root of a unfavourable quantity when coping with complicated numbers, there is no such thing as a computer-based mechanism to precise this.

Moreover, an incorrect operation will end result if a software program executes a floating-point operation on an integer-only location. This is because of a mismatch between the operation you are trying to hold out on the info (floating-point operation) and the saved knowledge (integer).

2: Zero division

A floating-point exception is thrown for those who try and divide an integer by zero. The identical factor happens if you try and divide by NaN or infinity. Listed here are some examples: 1/0, log(0).

3: Overflow

When an operation returns a price that’s outdoors of its anticipated vary, an overflow exception occurs. The worth is both extra or decrease than the smallest representable worth, in accordance with this assertion.

4: Underflow

Underflow occurs when a calculation yields a end result that’s lower than what an information kind can maintain.

5: Inexact

When the result of an operation differs from what was anticipated, this is called an inexact exception. When the operation is carried out with unbound precision and an exponent vary, this happens.

In some circumstances, such conditions could be dealt with gracefully. For instance, when a program makes an attempt to divide a quantity by zero, it’s typically preferable to return an error message and gracefully terminate this system as a substitute of permitting this system to crash.

#embrace <iostream>

#embrace <stdexcept>

utilizing namespace std;

float Div(float num, float den)

{

    if (den == 0) {

       throw runtime_error(“Math error: Tried to divide by 0n);

    }

    return (num / den);

}

int primary()

{

    float num, denom, end result;

    num = 10;

    denom = 0;

    strive {

        end result = Div(num, denom);

            cout << “The quotient is “ << end result << endl;

    }

    catch (runtime_error& e) {

      cout << “Exception occurred” << endl << e.what();

    }

}

On this code, the Div perform is named by the strive block inside primary. If the denom is just not equal to zero, the Div perform returns the quotient; whether it is, a runtime error exception is thrown. Earlier than calling the what perform with the runtime error object e, the catch block intercepts this exception and prints the textual content “Error occurred”. It’s used to establish the exception. The category Customary exception, which is described within the stdexcept header file, has a digital perform referred to as what(). The message “Math error: Tried to divide by 0” is printed in consequence.

Output

To forestall floating level exceptions in C++, it’s important to examine all of the parameters handed to features, to make use of acceptable codecs and to explicitly take a look at divisors for zero values. As well as, when utilizing double knowledge sorts, you will need to enlarge the vary of the info kind if this system requires bigger arithmetic outcomes.

Conclusion

Floating level exceptions in C++ are brought on by invalid operations on numerical values and may have an effect on this system’s skill to execute appropriately. To keep away from such errors, you will need to examine all of the parameters handed to features and to make use of the suitable knowledge sorts. Moreover, it’s helpful to catch floating level exceptions.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments