Undefined value
This article needs additional citations for verification. (March 2010) |
In computing (particularly, in
undef
operator[1] which can "assign" such value to a variable. In other type systems an undefined value can mean an unknown, unpredictable value, or merely a program failure on attempt of its evaluation. Nullable types offer an intermediate approach; see belowHandling
The value of a
Reserved value
In applications where undefined values must be handled gracefully, it is common to reserve a special null value which is distinguishable from normal values. This resolves the difficulty by creating a defined value to represent the formerly undefined case. There are many examples of this:
- The C standard I/O library reserves the special value
EOF
to indicate that no more input is available. Thegetchar()
function returns the next available input character, orEOF
if there is no more available. (The ASCII character code defines a null character for this purpose, but the standard I/O library wishes to be able to send and receive null characters, so it defines a separateEOF
value.) - The not a number" value which is returned when an arithmetic operation has no defined value. Examples are division by zero, or the square root or logarithm of a negative number.
- Structured Query Language has a specialvalue to indicate missing data.
NULL
- The Perl language lets the definedness of an expression be checked via the
defined()
predicate.[2] - Many programming languages support the concept of a null pointer distinct from any valid pointer, and often used as an error return.
- Some languages allow most types to be nullable, for example C#.[3]
- Most Unix system calls return the special value −1 to indicate failure.
While
Exception handling
Some programming languages have a concept of exception handling for dealing with failure to return a value. The function returns in a defined way, but it does not return a value, so there is no need to invent a special value to return.
A variation on this is
Non-returning functions
A function which never returns has an undefined value because the value can never be observed. Such functions are formally assigned the bottom type, which has no values. Examples fall into two categories:
- Functions which partial recursive function.)
- Functions which terminate the computation, such as the
exit
system call. From within the program, this is indistinguishable from the preceding case, but it makes a difference to the invoker of the program.
Undefined behaviour
All of the preceding methods of handling undefined values require that the undefinedness be detected. That is, the called function determines that it cannot return a normal result and takes some action to notify the caller. At the other end of the spectrum, undefined behaviour places the onus on the caller to avoid calling a function with arguments outside of its domain. There is no limit on what might happen. At best, an easily detectable crash; at worst, a subtle error in a seemingly unrelated computation.
(The formal definition of "undefined behaviour" includes even more extreme possibilities, including things like "halt and catch fire" and "make demons fly out of your nose".[4])
The classic example is a
Undefined value sensu stricto
The strict definition of an undefined value is a superficially valid (non-null) output which is meaningless but does not trigger undefined behaviour. For example, passing a negative number to the fast inverse square root function will produce a number. Not a very useful number, but the computation will complete and return something.
Undefined values occur particularly often in hardware. If a wire is not carrying useful information, it still exists and has some voltage level. The voltage should not be abnormal (e.g. not a damaging overvoltage), but the particular logic level is unimportant.
The same situation occurs in software when a
In the other direction, the open
system call in POSIX takes three arguments: a file name, some flags, and a file mode. The file mode is only used if the flags include O_CREAT
. It is common to use a two-argument form of open
, which provides an undefined value for the file mode, when O_CREAT
is omitted.
Sometimes it is useful to work with such undefined values in a limited way. The overall computation can still be well-defined if the undefined value is later ignored.
As an example of this, the C language permits converting a pointer to an integer, although the numerical value of that integer is undefined. It may still be useful for debugging, for comparing two pointers for equality, or for creating an XOR linked list.
Safely handling undefined values is important in
Other examples of undefined values being useful are
Notation
In computability theory, undefinedness of an expression is denoted as expr↑, and definedness as expr↓.
See also
- Defined and undefined(mathematics)
- Null (SQL)
References
- ^ "undef". Perl 5 documentation. 2009-09-25. Retrieved 2010-03-26.
- ^ "defined". Perl 5 documentation. 2009-09-25. Retrieved 2010-03-26.
- ^ a b Carr, Richard (2006-10-01). "C# Nullable Numeric Data Types". C# Fundamentals tutorial. Retrieved 2010-03-27.
- ^ "Nasal demons". Jargon File. Retrieved 2014-06-12.