Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39481
| References | <kg5sog$lfb$1@dont-email.me> <mailman.2191.1361478337.2939.python-list@python.org> <kg67l7$bm8$1@dont-email.me> |
|---|---|
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | 2013-02-21 22:53 +0000 |
| Subject | Re: Confusing math problem |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2209.1361487258.2939.python-list@python.org> (permalink) |
On 21 February 2013 22:39, Schizoid Man <schiz_man@21stcentury.com> wrote: > "Dave Angel" <davea@davea.name> wrote in message >> >> On 02/21/2013 02:33 PM, Schizoid Man wrote: >> However, there is an important inaccuracy in math.pow, because it uses >> floats to do the work. If you have very large integers, that means some of >> them won't be correct. The following are some examples for 2.7.3 on Linux: >> >> a b math.pow(a,b) a**b >> 3 34 1.66771816997e+16 16677181699666569 >> 3 35 5.0031545099e+16 50031545098999707 >> ... >> 5 23 1.19209289551e+16 11920928955078125 >> >> The built-in pow, on the other hand, seems to get identical answers for >> all these cases. So use pow() instead of math.pow() > > I see. I thought using the ** was shorthand for math.pow() and didn't think > that one would be integer operations and the other floats. Then you want operator.pow: >>> import operator >>> operator.pow(3, 2) 9 math.pow is basically the (double)pow(double, double) function from the underlying C library. operator.pow(a, b) is precisely the same as a**b. > I'm performing > some large integer arithmetic operations. I would normally do this my > writing my own multiplication class and storing results as strings, but a > friend suggested that I look at Python. There's no need to use strings if you're working with integers in Python. The results with int (not float) will be exact and will not overflow since Python's ints have unlimited range (unless your machine runs out of memory but that only happens with *really* big integers). If you want to do computations with non-integers and high precision, take a look at the decimal and fractions modules. http://docs.python.org/2/library/decimal.html http://docs.python.org/2/library/fractions.html There is also a good third party library, sympy, for more complicated exact algebra: http://sympy.org/en/index.html Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 19:33 +0000
Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 15:25 -0500
Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 22:39 +0000
Re: Confusing math problem Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-21 22:53 +0000
Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 23:41 +0000
Re: Confusing math problem Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-22 00:04 +0000
Re: Confusing math problem Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-21 17:19 -0700
Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 23:39 +0000
Re: Confusing math problem Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-21 13:42 -0700
Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 07:46 +1100
Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 22:44 +0000
Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 11:29 +1100
Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 21:19 -0500
Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 15:49 -0500
Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 08:23 +1100
Re: Confusing math problem Peter Pearson <ppearson@nowhere.invalid> - 2013-02-21 21:59 +0000
Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 09:11 +1100
Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 17:33 -0500
Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 10:15 +1100
Re: Confusing math problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-22 09:16 +0000
Re: Confusing math problem Serhiy Storchaka <storchaka@gmail.com> - 2013-02-22 13:48 +0200
Re: Confusing math problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-22 09:27 +0000
csiph-web