We will learn how to deal with Floating-Point Arithmetic Failure.
IEEE 754 - Floating-Point Standard
https://www.slideshare.net/prochwani9...
If a real number is of the following format
1 1 1
{ 1.(0|1)--- +(0|1)--- + (0|1)--- + ... } x 2^n
2 4 8
then the real number can be ACCURATELY REPRESENTABLE in floating-point format.
1.5 = 1.(1/2) ... is accurately representable in floating-point format.
1.25 = 1.(1/4) ... is accurately representable in floating-point format.
1/3 = 0.33333... is not accurately representable in floating-point format.
Informally the real number: -infinity -- real number -- +infinity
Floating-point number: -INFINITY -- valid floating-point number -- +INFINITY
Determinate States: -INFINITY, valid floating-point numbers, +INFINITY
INFINITY + INFINITY = INFINITY (Determinate State)
valid floating-poing number
-------------------------- = +/ INFINITY (Determinate State)
0
5.0
----- = + INFINITY (Determinate State)
0
Indeterminate States:
1. NaN or Not a Number is Indeterminate State.
2. NaN operator operand = NaN (Indeterminate State)
operand operator NaN = NaN (Indeterminate State)
3. NaN != NaN ? Yes, NaN != NaN.
INFINITY
-------- = NaN, or Not a Number, (Interminate State)
INFINITY
INFINITY - INFINITY = NaN, Not a Number (Interminate State)
0 Lim 2*x
-- = NaN (Indeterminate State) ---------- = 2
0 x--0 x
In the floating-point arithmetic,
it does not matter whether it is Divided-by-Zero or not.
What really matters is if the result of the operation is
in the Determinate State or in the Indeterminate State.
Indeterminate State is INVALID floating-point arithmetic
Determinate State is VALID floating-point arithmetic.
Then how can we detect "Indeterminate State?"
double d1 = some value;
double d2 = some value;
double rlt = d1 (operator) d2; // operator can be +, -, *, /
if(rlt != rlt) // NaN != NaN
{
// Floating-Point Arithmetic Failed.
}
else
{
// operation succeeded
}
Domain Error:
For mathematical functions, there is valid domain range.
sqrt(x) or square root, the x should be 0 or greater.
if x is less then 0, the return value is in Indeterminate State.
That is,
double rlt = sqrt(-1.0);
the value of rlt = NaN.
Do not concern yourself about Domain Error.
Instead examine the return value from the math functions.
C++2a Standard: Overflow and Division By Zero in Integer Arithmetic (007)
• C++2a Standard: Overflow and Division...
Смотрите видео C++2a Standard: Error Handling in Floating-Point Arithmetic (008) онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь IQ95 The Homo Siliconiens 10 Май 2019, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 128 раз и оно понравилось 2 людям.