Python “decimal” module for numerical computation

🙂

so I’m trying to modify the precision used in my code. the only method (I know of) is using the “decimal” module. but I’m not really sure how to use it. I’m not entirely new to Python, but I’m still learning how to implement some of the fundamentals, so modules like this one are kind of new to me and I’d really appreciate your help 🙁

for examle, in my code, I have the following definition

def f(R):
z = R – ((c1 * (mHS ** 2) * (( ((R) / (mHS**2) )) ** nHS))) / (1 + c2 * (((R) / (mHS ** 2)) ** nHS))
return z

where all variables (except “R”) have been previously defined as constants. if I were to use the “decimal” module, aside from writing “from decimal import *” and then “getcontext().prec = 6” at the top of my code:

  1. where (in the function I defined) should I put the “Decimal()” class? should I apply it to every single variable and number? should I apply it only to the whole thing my function returns (i.e. “Decimal(z)”)? should I just apply it to the numbers (there I only have “1” and “2”)? should I apply it to the argument I define in my function? probably not the last one, but I hope you get a feeling for what I’m trying to say with these questions.

  2. the code I’m writing is a fourth order Runge-Kutta method. so I also use these functions (defined by me) to add and multiply other things. that is, I also operate with these functions and I need to maintain a set precision throughout the whole algorithm. what do I do then? I saw something about using “quantize()” but “Decimal()” already confuses me enough to not use “quantize()” yet.

  3. aside from the last point, I want to avoid the whole “0.1 + 0.2 = 0.30000000000000004” situation in python (which I kind of understand that “Decimal” takes care of that?). and I wouldn’t like to carry extra decimals after operations are computed (e.g. always get a number with 64 “decimals” and truncate everything else after operating it. maybe 64 is a lot, but imagine a precision higher than the default double precision in python with truncated decimal values -I’m aware that higher precision will slow down the time it takes to compile/run my code). and again, I kind of understand that “Decimal” takes care of that? I just don’t know how to use it correctly.

I know some of these (if not all) might sound like dumb questions, but I’d really appreciate your help.

I have checked the documentation; but I’m not really sure how to specifically apply it to my problem.

New contributor

Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật