0

I get very weird fractions in sympy. enter image description here

I have no clue why it is given me these unusual fractions

enter image description here

bananenheld
  • 143
  • 4

2 Answers2

2

To answer your question:

Rational("0.1")
# out: 1 / 10
Rational(0.1)
# out: 3602879701896397/36028797018963968

The second output happens because 0.1 is a number of type float, hence it is inherently inaccurate.

Davide_sd
  • 10,578
  • 3
  • 18
  • 30
  • 1
    What about `nsimplify(0.1)`? [docs](https://docs.sympy.org/latest/modules/simplify/simplify.html#nsimplify) e.g. `nsimplify(sin(pi*0.1))` would be`-1/4 + sqrt(5)/4` – JohanC Jul 03 '22 at 13:57
1

sympy manual: https://docs.sympy.org/latest/explanation/gotchas.html#python-numbers-vs-sympy-numbers

in short:

you type Rational(0.1), then sympy has to test a float.

but Rational("0.1") works. str keep the number.

if you don't like this.

First convert it to sympy's type Integer(1)/10. Also works.

AsukaMinato
  • 1,017
  • 12
  • 21