New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BigDecimal Division needs MathContext #1812
Comments
Imported From: https://issues.scala-lang.org/browse/SI-1812?orig=1
|
@eengbrec said: |
@eengbrec said: Welcome to Scala version 2.8.0.r0-b20090511215951 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_07).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val a = Decimal64(1)
a: Decimal64.Decimal = 1
scala> val b = Decimal64(3)
b: Decimal64.Decimal = 3
scala> a / b
res0: Decimal64.Decimal = 0.3333333333333333
scala> val c = Decimal32(3)
c: Decimal32.Decimal = 3
scala> a / c
<console>:7: error: type mismatch;
found : Decimal32.Decimal
required: Decimal64.Decimal
a / c
^
scala> As you can see it is well-behaved and won't let you randomly mix Decimals from different DecimalModule. |
@odersky said: |
Frank Teubler (dft) said: I see that BigDecimals with different contexts An alternative suggestion: If there were a global (thread local?) MathContext variable, |
@eengbrec said: |
@eengbrec said: |
@paulp said: You can change contexts with an apply method on BigDecimal, so: scala> val x = BigDecimal(1.0)
x: BigDecimal = 1.0
scala> x / 3
java.lang.ArithmeticException: Non-terminating decimal expansion [...]
scala> x(DECIMAL32) / 3
res5: BigDecimal = 0.3333333
scala> x(DECIMAL64) / 3
res6: BigDecimal = 0.3333333333333333 Also I added some new methods, like: scala> BigDecimal(100) /% 17
res7: (BigDecimal, BigDecimal) = (5,15)
scala> BigDecimal(100) quot 17
res8: BigDecimal = 5
scala> BigDecimal(100.0001).byteValueExact
java.lang.ArithmeticException: Rounding necessary And etc. |
@eengbrec said: What if the MC were encoded as a covariant type parameter instead? Then if someone really didn't care, they could use a base type on the parameter that would allow it to be mixed freely. It would take a little thought to work out, but probably not too much. Let me know what you're thinking against encoding the MC in the type system is, and I'll see if I can come up with something. Also, if the MC is not part of the type, then I would suggest using a more friendly MC as the default. Crashing on non-terminating expansions is really not a friendly default behavior. Oh...and why are we stuck with Sun's awful name? Yeah, I know in the 2.7 series it was already called BigDemical, but really. There's no "SmallDecimal" or "RegularDecimal" in the library. Just BigDecimal. In the name of good names I suggest adding a deprecated type alias mapping BigDecimal to a more appropriately name Decimal. |
@paulp said: The simple way to improve this, which seems a good idea anyway, |
@SethTisue said: |
robinst said: |
@dchenbecker said: |
without MathContext there is a risk of ArithmeticException
java.math.BigDecimal has overloaded versions of divide operation
The text was updated successfully, but these errors were encountered: