473,771 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decimal and Exponentiation

Hi,

I am the in the need to do some numerical calculations that involve
real numbers that are larger than what the native float can handle.

I've tried to use Decimal, but I've found one main obstacle that I
don't know how to sort. I need to do exponentiation with real
exponents, but it seems that Decimal does not support non integer
exponents.

I would appreciate if anyone could recommend a solution for this
problem.

Thank you.

May 19 '06 #1
7 2354
[elventear]
I am the in the need to do some numerical calculations that involve
real numbers that are larger than what the native float can handle.

I've tried to use Decimal, but I've found one main obstacle that I
don't know how to sort. I need to do exponentiation with real
exponents, but it seems that Decimal does not support non integer
exponents.

I would appreciate if anyone could recommend a solution for this
problem.


Wait <0.3 wink>. Python's Decimal module intends to be a faithful
implementation of IBM's proposed standard for decimal arithmetic:

http://www2.hursley.ibm.com/decimal/

Last December, ln, log10, exp, and exponentiation to non-integral
powers were added to the proposed standard, but nobody yet has written
implementation code for Python's module. [Python-Dev: somebody
wants to volunteer for this :-)]

If you're not a numeric expert, I wouldn't recommend that you try this
yourself (in particular, trying to implement x**y as exp(ln(x)*y)
using the same precision is mathematically correct but is numerically
badly naive).

The GNU GMP library (for which Python bindings are available) also
supports "big floats", but their power operation is also restricted to
integer powers and/or exact roots. This can be painful even to try;
e.g.,
from gmpy import mpf
mpf("1e10000") ** mpf("3.01")


consumed well over a minute of CPU time (on a 3.4 GHz box) before dying with

ValueError: mpq.pow fractional exponent, inexact-root

If you're working with floats outside the range of IEEE double, you
_probably_ want to be working with logarithms instead anyway; but that
depends on you app, and I don't want to know about it ;-)
May 19 '06 #2
Tim Peters wrote:
....
Wait <0.3 wink>. Python's Decimal module intends to be a faithful
implementation of IBM's proposed standard for decimal arithmetic:

http://www2.hursley.ibm.com/decimal/

Last December, ln, log10, exp, and exponentiation to non-integral
powers were added to the proposed standard, but nobody yet has written
implementation code for Python's module. [Python-Dev: somebody
wants to volunteer for this :-)]


Here's a quick-and-dirty exp function:
def exp(x):
"""
Return e raised to the power of x.
"""
if x < 0:
return 1 / exp(-x)
partial_sum = term = 1
i = 1
while True:
term *= x / i
new_sum = partial_sum + term
if new_sum == partial_sum:
return new_sum
partial_sum = new_sum
i += 1

May 20 '06 #3
elventear wrote:
Hi,

I am the in the need to do some numerical calculations that involve
real numbers that are larger than what the native float can handle.

I've tried to use Decimal, but I've found one main obstacle that I
don't know how to sort. I need to do exponentiation with real
exponents, but it seems that Decimal does not support non integer
exponents.

I would appreciate if anyone could recommend a solution for this
problem.

Thank you.

The clnum module has arbitrary precision floating point and complex
numbers with all of the standard math functions. For example, the cube
root of 2 can be computed to 40 decimal places with the following.
from clnum import mpf,mpq
mpf(2,40)**mpq( 1,3)

mpf('1.25992104 989487316476721 060727822835057 0251464701',46)

For more information see

http://calcrpnpy.sourceforge.net/clnumManual.html

May 20 '06 #4
Tim Peters wrote:
<snip>
The GNU GMP library (for which Python bindings are available) also
supports "big floats", but their power operation is also restricted to
integer powers and/or exact roots. This can be painful even to try;
e.g.,
>>> from gmpy import mpf
>>> mpf("1e10000") ** mpf("3.01")
consumed well over a minute of CPU time (on a 3.4 GHz box) before dying
with

ValueError: mpq.pow fractional exponent, inexact-root

<snip>

The clnum module handles this calculation very quickly:
from clnum import mpf
mpf("1e10000") ** mpf("3.01") mpf('9.99999999 999999999999999 32861e30099',26 ) x=_
x ** (1/mpf("3.01"))

mpf('9.99999999 999999999999999 53924e9999',26)

See http://calcrpnpy.sourceforge.net/clnumManual.html
May 20 '06 #5
[Raymond L. Buvel, on
http://calcrpnpy.sourceforge.net/clnumManual.html
]
The clnum module handles this calculation very quickly:
from clnum import mpf
mpf("1e10000") ** mpf("3.01")

mpf('9.99999999 999999999999999 32861e30099',26 )


That's probably good enough for the OP's needs -- thanks!

OTOH, it's not good enough for the decimal module:

(10**10000)**3. 01 =
10**(10000*3.01 ) =
10**30100

exactly, and the proposed IBM standard for decimal arithmetic requires
error < 1 ULP (which implies that if the mathematical ("infinite
precision") result is exactly representable, then that's the result
you have to get). It would take some analysis to figure out how much
of clnum's error is due to using binary floats instead of decimal, and
how much due to its pow implementation.
May 20 '06 #6
Tim Peters wrote:
[Raymond L. Buvel, on
http://calcrpnpy.sourceforge.net/clnumManual.html
]
The clnum module handles this calculation very quickly:
>>> from clnum import mpf
>>> mpf("1e10000") ** mpf("3.01")

mpf('9.99999999 999999999999999 32861e30099',26 )

That's probably good enough for the OP's needs -- thanks!

OTOH, it's not good enough for the decimal module:

(10**10000)**3. 01 =
10**(10000*3.01 ) =
10**30100

exactly, and the proposed IBM standard for decimal arithmetic requires
error < 1 ULP (which implies that if the mathematical ("infinite
precision") result is exactly representable, then that's the result
you have to get). It would take some analysis to figure out how much
of clnum's error is due to using binary floats instead of decimal, and
how much due to its pow implementation.


Indeed, it is not clear where the error is comming from especially since
you can increase the precision of the intermediate calculation and get
the exact result.
mpf(mpf("1e1000 0",30) ** mpf("3.01",30), 20)

mpf('1.0e30100' ,26)

Is this the kind of thing you will need to do in the Decimal module to
meet the specification?
May 20 '06 #7
[Raymond L. Buvel, on
http://calcrpnpy.sourceforge.net/clnumManual.html
]
The clnum module handles this calculation very quickly:

>>> from clnum import mpf
>>> mpf("1e10000") ** mpf("3.01")
mpf('9.99999999 999999999999999 32861e30099',26 )

[Tim Peters]
That's probably good enough for the OP's needs -- thanks!

OTOH, it's not good enough for the decimal module:

(10**10000)**3. 01 =
10**(10000*3.01 ) =
10**30100

exactly, and the proposed IBM standard for decimal arithmetic requires
error < 1 ULP (which implies that if the mathematical ("infinite
precision") result is exactly representable, then that's the result
you have to get). It would take some analysis to figure out how much
of clnum's error is due to using binary floats instead of decimal, and
how much due to its pow implementation.

[Raymond] Indeed, it is not clear where the error is comming from especially since
you can increase the precision of the intermediate calculation and get
the exact result.
mpf(mpf("1e1000 0",30) ** mpf("3.01",30), 20)

mpf('1.0e30100' ,26)

Is this the kind of thing you will need to do in the Decimal module to
meet the specification?


It will vary by function and the amount of effort people are willing
to put into implementations . Temporarily (inside a function's
implementation) increasing working precision is probably the easiest
way to get results provably suffering less than 1 ULP error in the
destination precision. The "provably" part is the hardest part under
any approach ;-)
May 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1413
by: Jeff Davis | last post by:
I was doing some thinking about exponentiation algorithms along with a friend, and I happened upon some interesting results. Particularly, I was able to outperform the ** operator in at least one case, with a recursive algorithm. This leads me to believe that perhaps the ** operator should tune it's algorithm based on inputs or some such thing. Here is my data: >>> def h(b,e): .... if(e==0): return 1 .... if(e==1): return b
15
11657
by: Steven T. Hatton | last post by:
Did I mess something along the way, or is there no function in Standard C++ to raise an (unsigned) int to a power of (unsigned) int returning (unsigned) int? I never gave this a second thought until today. I tried to do it, and discovered <cmath> std::pow() only takes floating point types for the first argument. Sure I could write one. I could have written at least 3 fundamentally differnet versions in the time it took to discover there...
16
2719
by: Mars | last post by:
I'm writing a program for listing all binary numbers of the same length with the same number of 1s. e.g. 0011 0101 0110 1001 1010 1100
2
4586
by: David Laub | last post by:
I know there is no C# exponentiation operator. But since the double class is sealed, there seems no way to add the operator override without creating a new class which uses containment (of a double value) This seems a major pain, and would probably wind up being more syntactically messy than just calling Math.Pow(x,y) Surely greater minds than I have already wrestled with this problem...
3
7875
by: James McGivney | last post by:
What is happening here ? long longg = 5; longg = longg + (2 ^ 8); the answer should be 5 + 256 or 261 but at the end of the above operation C# returns longg = 5 + 10 or 15
2
1267
by: xfx | last post by:
Please consider this code: ---------- Dim i As Decimal Dim p As Decimal = 31D i = (2D ^ (p - 1D)) * (2D ^ p - 1D) ----------
67
8669
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
8
5673
by: grahamhow424 | last post by:
Hi I am trying to figure out how to duplicate a, financial, calculation that uses the caret, Exponentiation. Here's the formula... A = 0.0755 B = 34 C = 50000
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.