# Real Numbers in the Wolfram Language

Epilog:

Much of the discussion in the answers below revolves around the distinction between Real as a data type and real numbers as a domain or class of numbers (irrespective of the form in which they might be embodied).

This distinction between the data type and class is perhaps even clearer for the case of fractions and rational numbers. Rational numbers are those numbers that can be expressed a ratio of integers, a, b, where b is not equal to zero; that is, rational numbers are those that can be formatted as fractions. Although all fractions represent rational numbers, not all rational numbers are (formatted as) fractions. For example, the (rational) number 3 is not a fraction, even though it might be expressed as 3/1, a fraction. By this reasoning, it might be better if the head, Rational, were replaced by Fraction. Rationals could then be used to represent the class of numbers.

This is more of a request for advice and input than a solution to a programming challenge, but I thought it might be useful to raise here nonetheless.

I am wondering if Mathematica and the Wolfram Language may have adopted a definition of real numbers that conflicts with the definition (or definitions) of real numbers in modern mathematics.

In mathematics, the domain of real numbers is loosely thought of as all the points on the real line (http://en.wikipedia.org/wiki/Real_number).

## What real numbers are, according to the symbol, Reals

In Mathematica, the term Reals is said to represent the domain of real numbers. [Update] And as Yves Klett and Szabolcs note, this is consistent with the mathematical definition of the domain.

The following shows that Mathematica recognizes the symbol for the golden ratio to represent a real number, in this sense.

GoldenRatio \[Element] Reals


True

## What real numbers are, according to the symbol,Real

The head, Real, however, is employed expressly to designate floating-point numbers.

The definition is: "Real is the head for real (floating point) numbers".

This definition appears to conflate the domain of real numbers with floating point numbers.

Integers and irrational numbers are not floating point numbers. So they would not, according to this view, be considered real numbers.

If the head, Real, indicates that a number is a floating point number, not a real number, why not create the head, Float, to serve that purpose?

The designers of the language are certainly aware that integers and irrational numbers are real numbers. And they have expressly shunned the inclusion of RealQ. After all, what would we expect RealQ[3] to return, given that real numbers have been defined (in the definition of the head, Real) to be floating point numbers, and also defined (in the definition of the domain, Reals) as those numbers considered by mathematicians to be real numbers?

It seems to me that it would be advisable to eliminate the head, Real, from the Wolfram language. The head, Float, would fit the bill much better. Then Reals coud be properly defined as including rational numbers (including Integers), and irrational numbers.

One might argue that we should simply treat the domain of real numbers in mathematics as having a different meaning from the domain of real numbers in the Wolfram language. However, the Wolfram Language has chosen to use the double-strike R to designate the domain. This is precisely the symbol used throughout mathematics for the set of real numbers.

I realize that it is rather late in the game to be rethinking fundamental objects of the language, such as real numbers, but now that regions have been formally introduced, in version 10, it seems that it will become increasingly important to be clear and consistent in the specification of number domains.

• Comments are not for extended discussion; this conversation has been moved to chat. – Mr.Wizard Dec 24 '14 at 3:52
• Mr.Wizard I often disregard chat links because the discussions often stray from the issue at hand. As I understand it, that's the very reason they were created: to move potentially distracting conversations to the sidelines. As for the question of answers in the form of comments, people sometimes leave code-less answers of substance among the comments in the belief that formal answers require at least some Mathematica code. – DavidC Jan 4 '15 at 15:12
• Noted, again. Ironically I thought I was helping to preserve the information as items will not be deleted from the Chat (barring exceptional action) whereas long comment threads are often pared down over time. – Mr.Wizard Jan 4 '15 at 15:16
• Related Q&A at math.se. – István Zachar Jan 4 '15 at 23:25
• @alancalvitti We liked the Freudian touch. – Daniel Lichtblau Jan 6 '15 at 22:52

In some settings the integers, fractions, rational numbers, reals, and complexes are five distinct systems. Further, for reals and complexes, there are the standard reals and complexes as well as nonstandard systems. There are mappings from some to others, so that a subset of the reals in an isomorphic image of the integers (as rings), and so on for ${\bf Z} \subset {\bf Q}$, ${\bf Q} \subset {\bf R}$, ${\bf R} \subset {\bf C}$, and so on, where $\subset$ denotes a canonical injection. The rationals are also a natural homomorphic image of the fractions under the standard equivalence relation. In a math class (as opposed to a mathematics class), these things are all conflated, from school through the university courses that scientists and engineers take, and they are all thought of as belonging to same big set of "numbers." In a computational setting, there is a different sort of division depending on data types that correspond to hardware/software abilities, in which some things are exact and some approximate; there is always some truncation of the mathematical set (with respect to which numbers are actually representable). Computationally in school, you even get $\pi = 3.14 = 22/7$ sometimes; in the Indiana legislature, it was once proposed that $\pi =3.2$ would be a more convenient value.

So, while I think it's a pretty cool question, I don't think I'm up to fleshing out all the subtle distinctions a truly complete answer would have to make.

But one thing is clear, the type Real does not satisfy the properties of the real numbers. The following are inconsistent:

$MachineEpsilon == 0. (* False *)$MachineEpsilon == 0.15
(*  True  *)

1. + $MachineEpsilon == 1. (* True *) 1. +$MachineEpsilon/2 - 1.
(*  0.  *)

Head /@ {0.15, 0., 1.}
(*  {Real, Real, Real}  *)


Whatever the computational reasons underlying them, the results, except for the first, do not represent what happens with (mathematical) real numbers.

As for the domain Reals, I'm not convinced it represents a complete implementation of the real numbers as a logical system. I think it is primarily a way to specify a domain for exact algebra (as well as approximate numerics). For instance, I tried to specify and verify the least upper bound property but got stuck. Resolve did not seem to know how to deal with Subset[S, Reals] and Element[x, S] in the same statement. I'm not convinced that Subset[S, Reals] is a proper way to use Subset.

In practical terms, Mathematica is put out for use by mathematicians but also for use by those in many other fields. I'm not bothered by the term Real being distinct from "real", and I find "real (floating-point)" sufficiently clear to me, who knows what floating-point means. The case for or against calling approximate reals by the name Real ought to be based on the usefulness to the users who do not know what floating-point numbers are. If some are mathematicians, and some probably do not know numerics, they might be confused. For others who do not know about the axiomatic number systems of mathematics and think of reals numbers in terms of pencil & paper or what they see on their calculators, it might be easier for them to think of the Real numbers as real numbers.

I suspect the reason Wolfram did not choose Float is that compared to something like C, when Mathematica first came out, arbitrary precision was only available in specialized libraries (AFAIK). Arbitrary here means a user-specified finite amount, but that was an advance over machine floats that have a fixed precision. I would argue that they are closer to modeling the reals than C floats. They come close to an implementation of the computable reals. (I hedge a little because there is much to think through and verify to make an absolute statement.) Sometimes I wish machine reals and arbitrary precision reals had different heads, but we have MachineNumberQ to distinguish them when necessary.

• Nice! I interpret your "distinct systems" to refer to disjoint classes. No number can be both an Integer and a Real data type. This flatly contradicts the mathematician's view of number classes. BTW, I was unfamiliar with $Machine epsilon. It seems to be the smallest difference between two numbers. Reminds me of the following question I have posed to middle school students to provoke a discussion of continuity: "What's the smallest number that's greater than zero?" Lovely disagreements ensue. Apparently, computers can answer that question but mathematicians cannot. – DavidC Jan 4 '15 at 21:30 • I would +1 for "Indiana Pi Bill" alone. lol Sadly I don't think as a society we have really moved past such foolishness. – Mr.Wizard Jan 6 '15 at 12:09 • @DavidCarraher Yes, distinct means disjoint here. Mathematicians actually have several different views of numbers, from the ordinary view of an integer as a kind of real number to an integer being a distinct thing from an integral real number. See for instance, this construction of the integers, of the reals, of the rationals as well as other ways. Correspondences between them let us say they "really" mean the same thing. – Michael E2 Jan 6 '15 at 13:04 • N.B. Internal$EqualTolerance is giving you trouble here. 1. + $MachineEpsilon - 1. ==$MachineEpsilon (True) and 1. + $MachineEpsilon/2 - 1. ==$MachineEpsilon/2 (False) are probably more instructive comparisons. I agree with all your other comments. – Oleksandr R. Jan 6 '15 at 13:06
• @OleksandrR. Yes, I hesitated, and am still uncertain, how many examples are needed to establish a prima facie case and how much to go into "the computational reasons underlying" the results. I chose the two examples to show that Equal and arithmetic do not behave like their mathematical correlatives. I probably should have noted that 1. + $MachineEpsilon is in fact a distinct number from 1. while 1. +$MachineEpsilon/2 is not. – Michael E2 Jan 6 '15 at 13:31

As noted in post, responses and comments, Real is a Mathematica head and, as such, is distinct from Integer and Rational and Complex. All of these are regarded as "atomic" (notwitstanding that Rational has two Integer "parts", and Complex is comprised of any mix of the other three types).

These atomic types are in a sense distinct from the domains one considers. For domain purposes there are Integers, Rationals, Reals, and Complexes. By no means do all functions work with a notion of domains but some, such as Reduce, do so. Also these are good for inclusion testing in the sense that they conform to mathematical view of belonging: 3/4 is in Reals, for example. Well, there are edge cases. (Is 1.0 an integer? Mathematica gives its best attempt at "no comment").

Element[Pi^2 - E, Reals]

(* Out[326]= True *)

Element[1.2, Integers]

(* Out[328]= False *)

Element[1.0, Integers]

(* Out[329]= 1. \[Element] Integers *)


It's probably safe to assume that there will also be edge cases when it is difficult to determine whether an imaginary part is or is not zero. These might or might not hinge on what PossibleZeroQ does, I'm not sure.

In light of all this, a plausible test for expr being real might be TrueQ[Element[expr,Reals]]. I do not know if this will play nice with Assuming or \$Assumptions and do not have time to experiment, so I'll just mention that it might even be useful to enclose the above in Refine[]. Also there may need to be special handling for cases where one encounters a 1/0 type of infinity, e.g. a limiting value that is everywhere real off the point in question (and whether this should be regarded as real or not is likely to be problem-specific).

• One of my working concepts of an approximate Real number x is that it represents a range x ± epsilon for an appropriate epsilon. In that conception, Element[1.0, Integers] might or might not be True. (I hope that's a reasonable notion.) – Michael E2 Jan 6 '15 at 16:22

Couple of additional or summary points. This is a great question for the 21st century.

• Since the question regards mathematical definitions, "domain" isn't defined (at least by itself, unlike say "integral domain"). Instead should refer to specific categories like Set. Be especially careful with fields, eg non-Archimedean ones.

• IEEE 754 floating point was established only in 1985. Regardless of whether the Head is called Real or Float, does MMA implement 754?

• Perhaps more than any individual, William Kahan at Berkeley has analyzed the pathologies of floating point - see eg "Miscalculating area and angles of a needle-like triangle" to get a sense of the gap b/w math and computer science.

• An alternative numeric system developed primarily Clenshaw and Olver Level-Index arithmetic, based on iterated exponentiation, improves on floating computation by (removes underflow and controls overflow).

• Interval arithmetic is another approach to numeric computation - as per Michael E2's comment above.

• If WRI changes the implementation while keeping Head Real`, does OP's question change significantly?

• Re 2nd point/question: "Machine-precision computations are typically performed using native floating-point unit" from ref/MachinePrecision, "Background" section. – Michael E2 Jan 13 '15 at 1:25
• The folks from Wolfram Research may be able to answer whether MMA is 754 compliant or not. As regards your final question, it seems that no machine implementation will be able to handle all real numbers, so the problem boils down to what name(s) will be used to designate some subset of the reals that might correspond to finite decimals. Perhaps there ought to simply be some clearer way of distinguishing the form of a number (the data type) from the number domain it is presumably a member of. In principle, I would think it reasonable to reserve Real and Rational for domains, not data types. . – DavidC Jan 13 '15 at 2:33
• No, Mathematica does not implement IEEE754 arithmetic. It is coded in C, and the C compiler and platform are of course IEEE754 compliant, but depending on context, Mathematica is either not strict about making sure that operations are done correctly per the standard (inside the VM), or rejects IEEE754 completely (top-level, arbitrary precision). IMO there is no one consistent numerical model in Mathematica but rather a somewhat arbitrary mixture of different ones that are convenient in particular applications. This makes answering the question very difficult. – Oleksandr R. Jan 13 '15 at 11:34