5 ms·
Common Lisp: Numbers
- mmoez 7y agoIt is worth noting that most scripting languages today are still restricted by 64-bit integers and IEEE floating points limitations. Python and Ruby are probably the only popular ones that ship by default with unlimited precision integers. For arbitrary precision reals in Python, there is mpmath http://mpmath.org/ http://mpmath.org/ . There is also the standard decimal module which "provides support for fast correctly-rounded decimal floating point arithmetic" : https://docs.python.org/3/library/decimal.html https://docs.python.org/3/library/decimal.html
- m4r35n357 7y agoFor MP work in Python, gmpy2 - https://pypi.org/project/gmpy2/ https://pypi.org/project/gmpy2/ comes with a much better selection of MP functions, backed by MPFR, MPC. Also, the buit-in Decimal class is horribly slow by comparison to gmpy2.
- fdej 7y agoAlso (shameless self-promotion) python-flint! http://fredrikj.net/python-flint/ http://fredrikj.net/python-flint/
- baruchel 7y agoPurpose of the Decimal module is not really arbitrary precision computation but rather exact computation on decimal numbers wherever it matters (financial arithmetic, etc.). This module should never be used for scientific purposes.
- 292355744930110 7y agoIn Go "Numeric constants represent exact values of arbitrary precision" https://golang.org/ref/spec#Constants https://golang.org/ref/spec#Constants
- divs1210 7y agoI don't think Go qualifies as a 'scripting language', even though the term is very loosely defined.
- 292355744930110 7y agoI missed the part about scripting languages. Is LISP a scripting language?
- kazinator 7y agoYes. 1. Most Lisp dialects, including Scheme and ANSI Lisp, have interactive modes for evaluating expressions. 2. Most Lisp dialects have a mode of processing files in the same manner: reading individual forms and evaluating them immediately. I.e. it is not necessary to translate files into objects that have to be linked to produce an executable program.
- munificent 7y agoThis is only true for values calculated at compile time. Once the result is stored in a variable it is converted to a fixed-size representation. All runtime calculations and results are fixed size.
- threwawasy1228 7y agoI think Haskell allows for arbitrary-precision with the Integer type. It is limited only by the available memory in the machine.
- divs1210 7y agoEven though the term 'scripting language' is very loosely defined, I don't think Haskell qualifies as one.
- ky3 7y agoHaskell has shell hash-bang scripting capabilities just like Python and Ruby do. See https://stackoverflow.com/questions/8676331/how-to-run-a-haskell-file-in-interpreted-mode/8676377#8676377 https://stackoverflow.com/questions/8676331/how-to-run-a-has...
- kazinator 7y agoAlmost anything can be hash-bang, including C. https://rosettacode.org/wiki/Native_shebang https://rosettacode.org/wiki/Native_shebang https://rosettacode.org/wiki/Multiline_shebang https://rosettacode.org/wiki/Multiline_shebang
- ky3 7y agoTrue, but the difference between C and Haskell hash-bangs is that GHC Haskell provides an interpreter. GCC hash-bang is still an edit-compile-link-debug cycle. Just like with Python and Ruby, Haskell hash-bang accelerates the cycle by jettisoning the compile and linking steps.
- kazinator 7y agoThe Rosetta examples show a solution that only compiles the C file if it has not been previously compiled, or if it has a newer modification timestamp than the executable.
- mpolun 7y agoBigInts are coming to Javascript -- they're supported in most browsers and in node now, but they haven't officially been added to the standard.
- nikofeyn 7y ago> Python and Ruby are probably the only popular ones that ship by default with unlimited precision integers. racket has exact numbers of unlimited size. https://docs.racket-lang.org/reference/numbers.html https://docs.racket-lang.org/reference/numbers.html
- jxy 7y agoIn addition to Scheme and CL, Perl: https://perldoc.perl.org/bignum.html https://perldoc.perl.org/bignum.html Perl6: https://docs.perl6.org/language/math#Arithmetic https://docs.perl6.org/language/math#Arithmetic J: https://code.jsoftware.com/wiki/Vocabulary/xco https://code.jsoftware.com/wiki/Vocabulary/xco ocaml: https://opam.ocaml.org/packages/zarith https://opam.ocaml.org/packages/zarith
- ddragon 7y agoJulia (as a "scripting" language for scientific purposes) also supports them natively, but defaults to normal int/float for performance reasons, which might create some gotchas for people who use the language but are not programmers. https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Arbitrary-Precision-Arithmetic-1 https://docs.julialang.org/en/v1/manual/integers-and-floatin... If I remember correctly, Elixir uses arbitrary precision integers by default, though I'm not sure about float.
- hajile 7y agoBigInt works in Firefox, Chrome, Opera, and upcoming Edge at the moment. Safari preview also has at least partial support.
- reikonomusha 7y agoThe COMPUTABLE-REALS [0] package from the article is interesting because it lets you change the precision even after a computation is “performed”. It basically builds up lazy representations of computations that can (internally) recompute to any precision specified. You do all of your arithmetic as normal and only until you need to do something with an actual decimal representation do you “request” it. You grab the actual approximation as a decimal number with the APPROX-R function, which provides as many digits as you please. There is no need to re-run all of your code which produced the (computable) number in the first place to reapproximate at higher precision. This all directly falls out of a mathematical formalism called “computable real numbers” [1] which are the (countable!) set of real numbers for which an algorithm exists to compute the digits to any desired precision. (Usually binary digits are the standard way to write out the definition of a computable number.) [0] https://github.com/stylewarning/computable-reals https://github.com/stylewarning/computable-reals [1] https://en.m.wikipedia.org/wiki/Computable_number https://en.m.wikipedia.org/wiki/Computable_number
- chipguy 7y agoThat's interesting because that's how humans actually do algebra. Who cares about the decimal expansion of some irrational if in the end we divide it by itself for example.
- gjm11 7y agoUnfortunately, most infinite-precision real-number implementations (including the one here, I bet, but I haven't checked) will not recognize that you're dividing something by itself. So you'll get a super-inefficient implementation of the number 1 :-). [EDITED to add:] I checked; the implementation here indeed won't recognize that you're dividing something by itself. For the avoidance of doubt, nor do I think trying to recognize such things would be an improvement. (Also, if you ask for e.g. the digit immediately preceding the decimal point, you will wait for ever because no amount of precision will enable the implementation to tell whether your number is just less than 1 or 1 or just over. So don't ask it for that, ask it for a very close approximation and draw your own conclusions.)
- gibsonf1 7y agoI also highly recommend the rtg-math commonlisp library (ql:quickload :rtg-math) https://github.com/cbaggers/rtg-math https://github.com/cbaggers/rtg-math Incredibly fast with support for quaternions / spherical coordinate systems etc
- pfdietz 7y agoA nice thing about Common Lisp's unlimited precision integer is it made testing a Common Lisp compiler much easier. One can easily generate random integer-valued lisp forms without having to do much checking that integers are in range. In C or other languages with bounded integer size it's much tougher (see Regehr et al.'s Csmith). It's also tough to reduce a failing test case without introducing bad behavior in C; in Lisp it's straightfoward.