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?

6
  • 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). Commented Aug 16, 2023 at 18:53
  • 1
    @Woodford Floating point and decimal fixed-point are types of arithmetic. Python implements the latter in its decimal module. Commented Aug 16, 2023 at 19:13
  • @barmar That's correct and also has nothing to do with my comment. Commented Aug 16, 2023 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). Commented Aug 16, 2023 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). Commented Aug 16, 2023 at 21:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.