0

I'm working on a numerical evaluation using the sympy evalf() package. Consider the mpmath library's introduction to precision and accuracy https://mpmath.org/doc/current/technical.html, where on https://docs.sympy.org/latest/modules/evalf.html mentioned that

Exact SymPy expressions can be converted to floating-point approximations (decimal numbers) using either the .evalf() method or the N() function. ...

where, it also mentioned that

By default, numerical evaluation is performed to an accuracy of 15 decimal digits.

By "floating-point approximation", it sounded like the sympy's internal arithmetic is binary. However, when comparing an input value such as value=1.2, I found that the Float(value) was needed to feed into a function f_sympy(Float(value)) match the results from mpmath f_mpmath(value) and f_mpmath(mp.mpf(value)), where f_sympy(value) and f_mpmath(mp.mpf(str(value))) provided somewhat distinct decimal values. (precision to 200 decimal, where the deviation showed up on the 20th decimal places.)

Is sympy evalf() using binary or decimal arithmetic? Why Float(value) was required for sympy and why mp.mpf(str(value)) also changed the evaluation in mpmath?

  • *binary* and *decimal* are representations of numbers, not types of arithmetic. Where you seemed to get hung up was on the "15 decimal digits" part, which is just informing you that some numbers can't be represented perfectly by the computer and are limited to 15 significant digits (e.g. no amount of memory in the world can exactly represent the exact value of pi). – Woodford Aug 16 '23 at 18:53
  • 1
    @Woodford Floating point and decimal fixed-point are types of arithmetic. Python implements the latter in its `decimal` module. – Barmar Aug 16 '23 at 19:13
  • @barmar That's correct and also has nothing to do with my comment. – Woodford Aug 16 '23 at 20:07
  • 1
    If it isn't using native python number, `sympy` is using `mpmath`, which describes itself (from your link) as ` arbitrary-precision binary floating-point arithmetic `. Your links delve into this a lot more than we can (or want to). – hpaulj Aug 16 '23 at 21:03
  • 1
    SymPy's `evalf` uses mpmath which uses binary floating point. The note in the SymPy docs about "decimal numbers" is intended as an informal explanation because binary floats are generally displayed with decimal digits after the decimal point (and most users do not care with the internal format is binary or decimal). – Oscar Benjamin Aug 16 '23 at 21:37
  • Thank you. Somehow I think `mp.mpf(str(value)) ` provided different result because mpmath extrapolate the decimal number `1.2` to a binary that's more precise than the float stored in python. But I don't quite get why `f_sympy(Float(value))` was required and that the results differ from `f_sympy(value)`, it sounds like a bug. – ShoutOutAndCalculate Aug 17 '23 at 13:49

0 Answers0