Hi !
This is maybe a silly question, but...
is there a "easy way" to make eval() convert all floating numbers to Decimal
objects and return a Decimal?
for example:
eval('1.0000000 1+0.1111111') --> convert each number to a Decimal object,
perform the sum and obtain a Decimal object as a result?
maybe a parser is needed, but eval() already do the job, but I need the
precision that Decimal offers for numerical applications.
Thanks in advance.
--
Julián 4 3886
[Julian Hernandez Gomez] This is maybe a silly question, but...
is there a "easy way" to make eval() convert all floating numbers to Decimal objects and return a Decimal?
from decimal import Decimal
import re
number = re.compile(r"(( \b|(?=\W))(\d+( \.\d*)?|\.\d+)([eE][+-]?\d{1,3})?)")
deciexpr = lambda s: number.sub(r"De cimal('\1')", s)
for s in ('1.00000001+0. 1111111',
'+21.3e-5*85-.1234/81.6',
'1.0/7'):
print '%s\n --> %r' % (s, eval(s))
s = deciexpr(s)
print '%s\n --> %r\n' % (s, eval(s))
"""
1.00000001+0.11 11111
--> 1.11111111
Decimal('1.0000 0001')+Decimal( '0.1111111')
--> Decimal("1.1111 1111")
+21.3e-5*85-.1234/81.6
--> 0.0165927450980 39215
+Decimal('21.3e-5')*Decimal('85 ')-Decimal('.1234' )/Decimal('81.6')
--> Decimal("0.0165 927450980392156 8627450980")
1.0/7
--> 0.1428571428571 4285
Decimal('1.0')/Decimal('7')
--> Decimal("0.1428 571428571428571 428571429")
"""
Raymond Hettinger
On Tuesday 29 March 2005 03:04, Raymond Hettinger wrote: from decimal import Decimal import re
number = re.compile(r"(( \b|(?=\W))(\d+( \.\d*)?|\.\d+)([eE][+-]?\d{1,3})?)") deciexpr = lambda s: number.sub(r"De cimal('\1')", s)
for s in ('1.00000001+0. 1111111', * *'+21.3e-5*85-.1234/81.6', * *'1.0/7'): * * print '%s\n *--> %r' % (s, eval(s)) * * s = deciexpr(s) * * print '%s\n *--> %r\n' % (s, eval(s))
Wow!
Thank you so much!!!
now I can do my simple math function evaluator much more reliable !
Thanks again!
--
Julián
"Raymond Hettinger" <vz******@veriz on.net> wrote in message
news:6_72e.5007 7$u76.2569@trnd ny08... [Julian Hernandez Gomez] This is maybe a silly question, but...
is there a "easy way" to make eval() convert all floating numbers to Decimal objects and return a Decimal?
from decimal import Decimal import re
number = re.compile(r"(( \b|(?=\W))(\d+( \.\d*)?|\.\d+)([eE][+-]?\d{1,3})?)") deciexpr = lambda s: number.sub(r"De cimal('\1')", s)
for s in ('1.00000001+0. 1111111', '+21.3e-5*85-.1234/81.6', '1.0/7'): print '%s\n --> %r' % (s, eval(s)) s = deciexpr(s) print '%s\n --> %r\n' % (s, eval(s)) """ 1.00000001+0.11 11111 --> 1.11111111 Decimal('1.0000 0001')+Decimal( '0.1111111') --> Decimal("1.1111 1111")
+21.3e-5*85-.1234/81.6 --> 0.0165927450980 39215 +Decimal('21.3e-5')*Decimal('85 ')-Decimal('.1234' )/Decimal('81.6') --> Decimal("0.0165 927450980392156 8627450980")
1.0/7 --> 0.1428571428571 4285 Decimal('1.0')/Decimal('7') --> Decimal("0.1428 571428571428571 428571429")
This is less obvious and more useful, to me, than some of the recipies in
the new Cookbook.
TJR
> > [Julian Hernandez Gomez] is there a "easy way" to make eval() convert all floating numbers to Decimal objects and return a Decimal?
[Raymond Hettinger] from decimal import Decimal import re
number = re.compile(r"(( \b|(?=\W))(\d+( \.\d*)?|\.\d+)([eE][+-]?\d{1,3})?)") deciexpr = lambda s: number.sub(r"De cimal('\1')", s)
[Terry Reedy] This is less obvious and more useful, to me, than some of the recipies in the new Cookbook.
Okay, we can fix that. I've cleaned it up a bit and posted it on ASPN with
references, docs, and a doctest: http://aspn.activestate.com/ASPN/Coo.../Recipe/393265
Raymond This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Arvid Andersson |
last post by:
Hello, I need to convert a string to a number, but the string can
contain +,-,* and / as well as parenthesis. For example, if I have the
string "30/(6+9)" I would like a function that returned the number 2.
I actually wrote a java function that did this a couple of years ago, in
school, as an excersise in "binary trees". I lost it, and most of my
programming knowledge, but I figured perhaps there is a way to do this
easily in python? It...
|
by: flyaflya |
last post by:
a = "(1,2,3)"
I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
'2', ',', '3', ')') not (1,2,3)
|
by: aevans1108 |
last post by:
expanding this message to microsoft.public.dotnet.xml
Greetings
Please direct me to the right group if this is an inappropriate place
to post this question. Thanks.
I want to format a numeric value according to an arbitrary regular
expression.
|
by: Learner |
last post by:
Hello,
Here is the code snippet I got strucked at.
I am unable to convert the below line of code to its equavalent vb.net
code. could some one please help me with this?
static public List<RoleData> GetRoles()
{
return GetRoles(null, false);
}
|
by: Kathi215 |
last post by:
hi,
i`m wowrking in project that accept user input from swtiches(on /off) which is binary and then i have to display that number in lcd via maicrocontroller.I'm using c interactive language.My question is do i have to convert that number the user inter to decimal number or the c automatically convert it.If not how i can convert it to decimal?
| |
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?
-----------------------------------------------------------------------
When formatting money for example, to format 6.57634 to
6.58, 6.5 to 6.50, and 6 to 6.00?
Rounding of x.xx5 is uncertain, as such numbers are not
represented exactly.
|
by: elliotng.ee |
last post by:
I have a text file that contains a header 32-bit binary. For example,
the text file could be:
%%This is the input text
%%test.txt
Date: Tue Dec 26 14:03:35 2006
00000000000000001111111111111111
11111111111111111111111111111111
00000000000000000000000000000000
11111111111111110000000000000000
|
by: mrajanikrishna |
last post by:
Hi Friends,
I am accepting a number from the user entered in a textbox.
I want to assign to a variable in my code and assignt this to that
variable.
double num1 = (double)txtNum1.text;
this produced an error
|
by: karthickbabu |
last post by:
Hi
In my application i want to convert integer to decimal. I get a input and using convert function to convert into decimal. But it shows as it self. My code like as below, Is any wrong in my code
dim input as long // I declare as long
dim decimaloutput as decimal
decimaloutput = Convert.ToDecimal(input)
label1.text = decimaloutput.ToString()
|
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...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |