4 ms·
Python's math operations are going to need a lot of C code because the numbers can be any size. It's part of what makes it so great for scientific computing (as
by Uptrenda 3y ago
Python's math operations are going to need a lot of C code because the numbers can be any size. It's part of what makes it so great for scientific computing (as you don't have to spend hours implementing arbitrary precision math - probably badly.) If that's too slow though there's always NumPy.
- KeplerBoy 3y agoThat's only true for integers. For scientific applications you'd typically want floating points and Python's floats are just regular ieee-754 doubles (or whatever "double" meant to the compiler used to compile that python interpreter).
- yeshman 3y agoPython also has a fixed point arithmetic in its std library: https://docs.python.org/3/library/decimal.html https://docs.python.org/3/library/decimal.html
- Uptrenda 3y agoThe disadvantage with floats is you can't do accurate computations due to having an inexact representation. Python has the same issues with floats. For example: I do a lot of work with financial software and also some basic applied cryptography. And the essential rule is to never ever use floats. Where 'decimals' are needed you want them to be simulated using integers. Python has a module called decimal which I think helps mitigate some of these issues. I've written some code to work with accurate, large precision numbers in Python and C before. Mostly the annoying part with this is having data types for the numbers that correspond well to database fields (like uint64) or are portable (in C its easiest if you can get a u128 but this type is very compiler-specific so some hacking may be needed.) It's fun to work on code like this but definitely needs to be precise and have good test coverage. Writing your own math libraries that are going to be used for such important operations is hair-raising stuff.
- cozzyd 3y agoIf only there were libraries for multiprecison arithmetic in other languages...