Fixed some minor bugs and improved interoperability (IMHO)
Summary
I really needed to exploit BigDecimals for a project of mine and soon I realized deveel-math was the only viable option.
As soon as I adapted my code to deveel-math, I noted a few minor bugs & possible improvements.
I tried to fix the former ones and i'm now proposing the latter ones, hoping you will accept them.
Bug fixes & Improvements
- I fixed
BigDecimals>and<operators which whose implementation was inverted - I made the
==operator coherent with other operators by making it employCompareToinstead ofEquals - I made some casts operator explicit in order to adopt the very intuitive semantics of C-like languages casts, that is, loseless casts can be implicit while losy casts must be explicit. For instance:
-
Implicit casts for
BigDecimal- from
BigDecimalto floating point primitive types (float,double,decimal) and vice versa - from
BigIntegertoBigDecimal
- from
-
Explicit casts for
BigDecimal- from
BigDecimalto integral primitive types (short,int,long) - from
BigDecimaltoBigInteger - from integral primitive types (
short,int,long) toBigDecimal- this is actually an exception to the aforementioned general rule since this cast is loseless and should be implicit. My aim is to avoid the situation where an overloaded method, say
someOp(BigIntegerorsomeOp(BigDecimal), generates an error if applyed to an integral of floating argument, thus proving implicit conversion of integral number to BigIntegers and floating numbers to BigDecimals.
- this is actually an exception to the aforementioned general rule since this cast is loseless and should be implicit. My aim is to avoid the situation where an overloaded method, say
- from
-
Implicit casts for
BigInteger- from
BigIntegerto floating point types - from integral types to
BigInteger
- from
-
Explicit casts for
BigInteger- from
BigIntegerto integral types
- from
-
Implicit casts for
- I added some tests for the aforementioned casts, but more are needed
- I noted that, for instance,
BigDecimal.Parse("-16.00").Compare((BigDecimal)BigInteger.Create(-16))was!= 0. The same happened for other values from time to time. I never really undestood why this happened by I unsterstood it was related the theBigDecimal.AproxPrecision()returning some wrong value withing theCompareTomethod. So I replacedBigDecimal.AproxPrecision()byBigDecimal.Precisionand now the problem is not occurring anymore.- I added some tests (namely,
CompareToBigDecimal2, andCompareToBigDecimal3) which should fail in your old implementation and should succed in my proposal.
- I added some tests (namely,
- Finally, I introduced the
BigDecimal.CreateExact(float|double|decimal)static methods enabling the creation of exactBigDecimals starting fromfloat/double/decimalliterals, so for instance, whilenew BigDecimal(1.9)actually createsBigDecimal("1.899999999999999911182158029987476766109466552734375"),BigDecimal.CreateExact(1.9)createsBigDecimal("1.9")
Hi, sorry to dig up this old PR, is there any intention of the mantainer to merge this improvements and release a new version with them?
Hello guys. This library was left unmaintained for a long time, and I have just resumed its maintenance. It's utterly late and I apologize for such a disgraceful job of maintenance: I had in fact to mark it as obsolete. In another branch, I did several updates to port it to .NET 6-8, but before merging to master I will review the changes that you have done back then (7 years ago now!! Sorry!!).