I may not have worded the title correctly. Is there any programming language(s) which deals with natural mathematical number types rather than the typical data types we see like Int32, Int64, Float, Double etc?

By natural number types I mean the likes of

natural (1, 2, 3 ...)
whole (0, 1, 2, 3 ...)
integer (-1, 0, 1, 2, 3 ...)
rational (-1, 0, 1, 1.25, 2, 3, 3.5 ...)
real (-1, 0, 1, 1.25, 2, 3, pi, 3.5 ...)
complex (-1, 0, 1, 1.25, 2, 3, pi, 3.5, i, 2i, 1 + 2i ...)

Or may be bit more practical and useful set of types, like

whole (0, 1, 2, 3 ...)
integer (-1, 0, 1, 2, 3 ...)
real (-1, 0, 1, 1.25, 2, 3, 3.5 ...)
complex (-1, 0, 1, 1.25, 2, 3, pi, 3.5, i, 2i, 1 + 2i ...)

so that if I write:

whole w = 1 // compiles
integer i = 1.2 // doesnt compile
real r = 1.2 //compiles
complex c = 1 + i //compiles

w = i //doesnt compile
i = w //compiles
r = i //compiles
c = r //compiles

I know I can achieve this in most programming languages using uint32, int32, decimal etc types but I'm looking forward to a language that doesn't appear that technical about numeric types.

Few points:

  1. Looking for programming languages which names the numeric types from a mathematical perspective and not computer.

  2. I know size always matter in programming, but let's assume size is not an issue for now. Or may be it is configurable and I could set the size to be as that ofuint64, int64 etc. This is more than ever needed.

  3. I am not after math libraries which has types defined like real, complex etc. I want those types to be treated as first class by compiler/language itself so that static error checking is done.

Asking this just out of curiosity when I was thinking of programming languages for educational purposes for non-programmer friend.

  • 1
    When a tag says "DO NOT USE THIS TAG", why do you decide to use it anyway? – durron597 Jul 11 '15 at 21:53
  • Which tag says that? Sorry, havn't noticed. I'm on mobile now. – nawfal Jul 11 '15 at 21:54
  • 1
    education – durron597 Jul 11 '15 at 21:55
  • 4
    You might be looking for "symbolic computation", which is natively supported in languages such as Mathematica. While it's impossible to make a programming language that genuinely supports every conceivable real number and operation on them, Mathematica gets a hell of a lot closer than C. – Ixrec Jul 11 '15 at 22:15

The real and complex numbers are uncountable. Regardless of what representation you choose, almost all of them cannot be stored on anything that could be considered a computing device. The closest you can get is the computable numbers, but testing two computable is undecidable. Even disregarding that, it's not a very practical representation. Some sort of multi-precision floating point numbers, or arbitrary precision rational number, is the most general practical option — despite only supporting (a subset of) the rational numbers. Naturals and integers are common though, arbitrary precision integer arithmetic is supported in many languages, quite a few of them use them as default type (or transparently upgrade to it when overflow would occur).

Now, as for static type checking: You rule out libraries, but "static error checking" is equally possible for library types. You just don't get convenient numeric literals. There are a few languages that allow user-defined literals.

There might be languages that subscribe to the particular set of numeric types and implicit conversions you propose, but it's a rather odd bunch. Are you dead set on these specifics? Many high-level languages such as Python, Ruby, and various Lisp dialects have arbitrarily large integers and floating point and complex numbers built in, and occasionally also rational numbers. They are not statically typed though. Likewise, most statically typed languages default to fixed-sized types. Haskell might be close, including overloading integer literals though I don't think complex numbers can have 4i syntax.

NB: What you call "whole numbers" are usually called the natural numbers. Some mathematicians exclude 0 from the natural numbers, but equally many (and all computer scientists) include it. The integers are the whole numbers.

| improve this answer | |
  • delnan, thanks for answering. I do realise numeric type has to have a size and operating on numbers depends on bit-ness of the number. For me size is not a requirement, let it have an upper cut off 2 ^ 32 or 2 ^ 64. So I wasn't exactly after arbitrary precision rational numbers or so. Just asking if there are computer languages which by default names its numeric types like in the mathematical world, and not int64, double, float etc which sound technical. – nawfal Jul 12 '15 at 5:24
  • What do you mean by "real and complex numbers are uncountable" ? – nawfal Jul 12 '15 at 5:24
  • 1
    @nawfal Just the names is a strange requirement. In particular for your favorite approximation of reals, names like "float" or "double" or "decimal" are good, I'd argue, even for beginners, because they make very clear that there are not the real numbers (and unlike integers for example, the differences are easy to hit even without overflow). As for uncountability: In short, there is no way to list all reals - any list, even infinite, must necessarily miss most of them. More technically, there is no one-to-one mapping from the natural numbers to the reals. See Cantor's diagonal argument. – user7043 Jul 12 '15 at 7:44
  • 1
    @nawful - real (and hence complex) numbers are "uncountable" in a mathematical sense. With natural numbers, we can pick a starting point, then count up, such as 0, 1, 2, 3... . You can't do the same with real numbers. If you have any two natural numbers, there is always another one that is between the two. So you could try starting with 0, 1..., but there's 1/2 between them, so it should be 0, 1/2, 1... . But then there's 1/4 and 3/4, so it's 0, 1/4, 1/2, 3/4, 1... . But then there's 1/8, 3/8 and so on. Ultimately, there is no such thing as the "second" real number. – Simon B Jul 13 '15 at 8:00
  • 2
    @SimonB You seem to confuse cardinality with well-ordering or something related to it. A countable set has a bijection with the naturals, but there is no requirement that this mapping preserves order (or that there even is such a thing as an "order"). The rationals are countable, for example, even though there is yet another rational between any two rationals. – user7043 Jul 13 '15 at 8:02

FORTRAN, is close. It has INTEGER, REAL, COMPLEX. REAL explicitly has 2 different precision lengths. COMPLEX cannot be negative.

Looking for programming languages which names the numeric types from a mathematical perspective and not computer.

Fortran is especially suited to numeric computation and scientific computing.

Some mathematicians exclude 0 from the natural numbers, but equally many (and all computer scientists) include it.

Zero is to computer programming as The Big Bang is to AstroPhysics. ... Just a thought.

| improve this answer | |
  • This answer doesn't deserve the downvote. In particular it addresses the OP's point "I want those types to be treated as first class by compiler/language itself so that static error checking is done". This is true of Fortran for COMPLEX. Still, C++ Standard Library also fits that requirement e.g std::complex – paisanco Jul 12 '15 at 1:12
  • 1
    Down votes deserve a comment as to why this is wrong. Down vote means "this answer is wrong." DV is not a means to say "I don't like it." or "other answers are better." It is not for giving some pseudo-ranking to answers. Anyone with edit privilege is allowed to improve an answer as well. – radarbob Jul 12 '15 at 1:52
  • I don't see anything wrong with the answer. Fortran is an old/niche language, that is true. But that doesn't make the answer incorrect. – paisanco Jul 12 '15 at 5:16
  • 1
    @paisanco the mouse over for a down vote reads "this answer is not useful." If you look at the first revision of the answer which likely got the down vote, the information is a bit sparse. Unfortunately, this type of question which asks for a listing of programming languages with mathematical types is hard to write a good answer for (a valid answer would be "Try perl's Math::Complex" which is equally right as this one and also not especially helpful). – user40980 Jul 12 '15 at 21:59
  • 2
    one problem here is that FORTRAN REAL is not a Real number but floating point, so there is very little FORTRAN does here than any other programming language – jk. Jul 13 '15 at 9:23

There are Computer algebra systems which usually support correct number representations necessary for any given computation they do.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.