Creating A Number Class that ELIMINATES rounding errors and is __repr__ pretty printed

I guess I will sheepishly admit I’m a python noob.

I am trying to produce float numbers that are correctly rounded to the EIGTH decimal place for distances,
and the TWENTY-SIXTH Decimal place for degree anglular measurments, so I can load them into a DXF file, and display them on QCAD….

My first initial approaches after seeing Excel will only handle Double Floats (with the IEEE754 rounding errors) was to use python 3.X , with out-of-the-box MPMATH 1.3.0 , of which I still consider using, because of the pretty printing that the Decimal Class lacks…

My Big misgiving with out-of-the-box mpmath 1.3.0, even with dps upped to 100 significant digits was a subtraction test I usually use mpmath.fsub("867.5309", "867.5308")

Now you’d think, since I use mpmath with str versions of the numbers… mpmath would spit out the exact number 0.0001 right?

Nope…. it spits out 0.000099999999999999999999999999....72927" Same thing using mpmath.mpf(“867.5309”) – mpmath.mpf(“867.5308”)Using python floatsmpmath.fsub(867.5309, 867.5308)gets me:0.000099999999974897946231067180633544921875`

Now Decimal … it works flawless… when I put Decimal("867.5309") - Decimal("867.5308") it spits out
Decimal("0.0001") and then I would have to convert it to a str and then mpmathify the number so it’s pretty printed as a mpf

So what’s the problem, you ask? Why not just ditch mpmath altogether and stick with Decimal?

Well, the problem is…. I have so much code invested with ‘mpmath’ expressions, it would be way too much a P-I-T-A to recode, plus I have to collaborate with a team that’s already invested in mpmath 1.3.0

So…. I’ve been using a regex engine to detect .9999999999 or .????00000000000???? on all my expression results to get them correctly rounded to the correct number.

Like I said: I need it exact to 8 decimal places for X/Y Distances, and up to 26 Decimal places for Degree Angular because DXF Entity Formats use Angle Start/Stop in Decimal Degree Measurments for the Arc Entity.

Intermediate Measurments line sin/cos/tan(x) can be anywhere from 50-250 decimal places to the right, that’s no problem, and I can probably use Decimal for those expression results… just as long as I can get the desired precisions on the XY distances and the Degree Angular Measurments. PLUS: if it rounds to a ZERO or an int… hold it as an int… if it rounds to 8 or 26 decimal places hold it as a mpmath float.

So… What’s my question, then?

Ok, I’m trying to write a Number Class that handles mpmath ( or basically any mathematical) expression results, that may (or may not) end up with an ungodly long decimal stream that may or may not have IEEE754 float errors inside it…

and…what I’m looking at, or rather, what I want, is, being able to call a class that depending on the qty_decimal_plcs parameter in the Call, will create a subclass that will store 8 decimal length mpmath floats (or ints) or a sub class that will store a 26 decimal length mpmath float (or int)

Now, My first idea is to use an ABC or an Interface because I want to be able to call the Parent Class and have it auto-magically send it to the right class (again, depending on how many decimal places in the ‘qty_decimal_plcs’ parameter, and of course, static-type any variable, with the Parent Class/Protocol

Let me just show you what I want to achieve:

SomeVariable: MagicNumber = MagicNumber(<some math expr, rslt, or initVar>, qty_decimal_plcs = 8) 
  # creates a MagicNumber_Distance object and stores <expr, that may or may not have repeater number> 
  # as an int or a mpmath float up to 8 decimal places, if number can be evaluated to be an int,
  # or a float that has a repeater that can be rounded away, 
  # or just rounded to the correct precision.

SomeOtherVariable: MagicNumber = MagicNumber(<some math expr, result, or initVar>, qty_decimal_plcs = 26)
  # creates a MagicNumber_Angluar object and stores <expr/reslt> as an int or a mpmath float up to 26 
  # decimal places, again, if it can be evaluated to be an int, or a float with repeater that can be
  # rounded

YetSomeOtherVariable = MagicNumber(<some other math expr, or result>)
  # math expr or result may or may not have 0.(...)99999999.... or [0.]...000000000....
  # and MagicNumber creates, perhaps if not a '_Distance' or '_Angular' subcless, 
  # maybe, creates an '_General' object, and stores <expr> as an int, if it can be 
  # rounded to or evaluated to be an int coming in, or, an mpmath float up to anywhere from
  # 50 to 100 decimal places

Another feature this MagicNumber Class/Parent/Child class will have is a Tolerance Bubble,
a tuple of two mpmath floats that are +/- a given distance away from the given determined number,
so, if any calculation result done outside any MagicNumber scope gets compared, that other can be compared to the bubble, being inside the bubble, or outside, along with usage of ‘Decimal.compare()’ which gives a -1, 0, 1: for example, here’s how I wanted to structure my dunders of ==, !=, <, <=, >, >=

Class MagicNumber():
    
    ... # some mystery mind-melting code here

    def __eq__(self, other: AugieNumber | SomeOtherNumber) -> bool:
        """ this tests for '==' """
        return bool(
            (Decimal(str(self._num)).compare(Decimal(str(other)) == 0) or (
                min(self._tol) <= mpmath.mpf(str(other)) <= max(self._tol)
            )
        )

    def __ne__(self, other: AugieNumber | SomeOtherNumber) -> bool:
        """ this tests for '!=' """
        return bool(
            (Decimal(str(self._num)).compare(Decimal(str(other))) != 0) or (
                min(self._tol) > mpmath.mpf(str(other))
            ) or (
                mpmath.mpf(str(other)) > max(self._tol)
            )
        )

    def __gt__(self, other: AugieNumber | SomeOtherNumber) -> bool:
        """ this tests for '<' """
        return bool(
            (Decimal(str(self._num)).compare(Decimal(str(other)) == 1 or (
                mpmath(str(other)) > max(self._tol)
            )
        )

    ... # rest of the comparison dunders...

Also, what I want in this class, as you can probably see… Is do the dunder boilerplate code
for __add__, __iadd__, __radd__, etc… as a expression of two Decimal Objects under the hood,
and convert to an int or a mpmath number and store that result in the respective subclass,
be it MagicNumber_Distance or MagicNumber_Angular or MagicNumber_General

YES….I do have a specialized algorithm working to handle repeater 9999… or repeater 0000… decimal segments, of whatever type, when found…

Don’t cry: I convert the whole result to a str and use a re expression to detect repeaters, and when detected, subtract a home-brewed criteria result from the len of the repeating 99999...' or …00000….` to determine whether to round at where the repeater starts, or round at the qty of decimal places if its not long enough to meet given home-brewed criteria.

You’ll forgive me, as I’m getting bleary-eyed, and I need to get to bed. I have to be on a bus in about three hours, and it’s a two-mile walk from here to the bus stop.
I’ll cut it off here, and edit it later… I just want your first reactions or suggestions.

New contributor

Kapt Blasto 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