HACKER Q&A
📣 igravious

Which programming language has the best `number' implementation?


Might sound like a vague question but I'm thinking here in terms of a complete type hierarchy that's faithful to mathematics, going from Natural numbers to Integers to Rationals to Reals to Complex numbers (and others?). That caters for native primitive machine number types be they floating or fixed point or whole numbers, signed or unsigned.

That knows that i is the square root of -1, that knows that √2 is irrational but π and e are transcendental. And so on. Also looking for a language that has a complete efficient implementation of surreal numbers (prompted by Conway's recent passing), or at least allows you to plug one in seamlessly/transparently if you so desired.


  👤 s1t5 Accepted Answer ✓
Julia has the type hierarchy that you're describing and it's very easily extendable.

👤 jolmg
Personally, I like Haskell's treatment of numbers. I wrote a comment about it here:

https://news.ycombinator.com/item?id=18565234

Don't really know much about surreal numbers, but I found the following library that defines Fractional and Num instances for it, so that should enable Haskell to work with surreal numbers as if they were native to the language:

https://github.com/Lacaranian/surreal

There's also this module on transcendental numbers:

https://hackage.haskell.org/package/numeric-prelude-0.4.2/do...

auxym might be right, though, that you might be better served with a language that is specialized more in mathematics, rather than a general purpose language.


👤 qppo
It sounds like you're asking for symbolic math utilities, which exist in a number of languages.

Like for example I've tried to play this game building a toy language from first principles but you run into problems rather quickly. A computer can't represent reals (or even all rationals you care about) with a finite number of bits. You can't guarantee that all integers are representable by all rationals and all rationals by reals and so on.

And if you want to throw out formalism and accept the consequences, every language has a way to do it to varying degrees of success. Matlab, Mathematica, Julia, Python, R, basically anything that might be used by a working mathematician probably has the facilities and libraries to help.


👤 bjourne
I think Factor has the best number implementation. It has builtin support for bignums and rationals. Eg:

    IN: scratchpad 2 63 ^ 1 + 2  63 ^ / 2 63 ^ * 2 63 ^ 1 + =
    t
The corresponding expression would incorrectly evaluate to false in most languages:

    >>> n = 63
    >>> (2**n + 1) / 2**n * 2**n == 2**n + 1
    False
Factor also has syntactic support for complex numbers but not for irrational ones.

👤 afiori
If you want something like surreal numbers (or anything that might use ordinal induction) you might be better off with a proof assistant.

Lean is being developed specifically for being applicable to math researcher.


👤 igravious
Missed all these great replies :( Took a while for them to filter through so I thought my question had sunk like a stone into the murky depths of HN. I'll reply individually now and hope people notice.

👤 RobLach
Julia in the long term

👤 auxym
Mathematica?

👤 rurban
Common Lisp, Julia, perl 6.

👤 sloaken
Oldie but a goodie: FORTRAN It has evolved a lot over the years.

👤 jjgreen
Have a look at Pari/GP