Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Exact Number calculations" (making python do math like a TI-89)

Tags:

python

math

This is my first post here, I only started playing with programming two months ago.

My TI-89, and also Microsoft Mathematics, can divide, multiply, add fractions, etc. all without ever resorting to floating point numbers. I thought that such a module for doing math as exactly as possible must exist in python, but I can't find such a thing anywhere.

So, I've begun writing my own program to do this. So far, all I have is code that can factor the top and bottom halves of a fraction, and then simplify and return the fraction. From there, I will be able to write code that will add, subtract, multiply, and divide fractions and leave the numbers intact as exact. After that, I can just write logic gates that will execute the math in accord with orders of operation. I should note that all of this work is done in strings which are converted to numbers when necessary and then back to strings. Eventually, I hope to be able to add in math variables like x and y, and solving for variables

I'm not really sure what my question is beyond if anyone knows of such a module that can already do all of this. Also, does this sound like a useful tool to anyone? Any suggestions?

like image 814
Matt F Avatar asked Sep 14 '25 11:09

Matt F


2 Answers

You should maybe look into the following modules:

  • Sympy (symbolic mathematics) --> http://www.sympy.org/zh/index.html
  • fractions --> https://docs.python.org/3.4/library/fractions.html
  • decimal --> https://docs.python.org/3.4/library/decimal.html

If you are interested and able, you can volunteer to help the sympy team :)

like image 179
Reblochon Masque Avatar answered Sep 16 '25 00:09

Reblochon Masque


I think you're looking for https://docs.python.org/3/library/decimal.html

The decimal module provides support for fast correctly-rounded decimal floating point arithmetic.

There are definitely other symbolic math libraries to do things like solve algebraic equations.

like image 44
raylu Avatar answered Sep 16 '25 00:09

raylu