summaryrefslogtreecommitdiff
path: root/src/library/scala/math/BigDecimal.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2013-05-27 12:02:45 -0700
committerPaul Phillips <paulp@improving.org>2013-05-27 12:54:58 -0700
commita2e8f9e001356a993da4e01235c12583d97163d3 (patch)
treec52920518cedd2ed8c38682e014dbcab683b6597 /src/library/scala/math/BigDecimal.scala
parente42991f40707023d563f43bf66f3d8b9a637aa1b (diff)
downloadscala-a2e8f9e001356a993da4e01235c12583d97163d3.tar.gz
scala-a2e8f9e001356a993da4e01235c12583d97163d3.tar.bz2
scala-a2e8f9e001356a993da4e01235c12583d97163d3.zip
Make all numeric coercions explicit.
Optimistically, this is preparation for a day when we don't let numeric types drift with the winds. Even without the optimism it's a good idea. It flushed out an undocumented change in the math package object relative to the methods being forwarded (a type is widened from what is returned in java) so I documented the intentionality of it. Managing type coercions manually is a bit tedious, no doubt, but it's not tedious enough to warrant abandoning type safety just because java did it.
Diffstat (limited to 'src/library/scala/math/BigDecimal.scala')
-rw-r--r--src/library/scala/math/BigDecimal.scala10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/library/scala/math/BigDecimal.scala b/src/library/scala/math/BigDecimal.scala
index f75cfad882..00a3686585 100644
--- a/src/library/scala/math/BigDecimal.scala
+++ b/src/library/scala/math/BigDecimal.scala
@@ -44,6 +44,7 @@ object BigDecimal {
*/
def valueOf(d: Double): BigDecimal = apply(BigDec valueOf d)
def valueOf(d: Double, mc: MathContext): BigDecimal = apply(BigDec valueOf d, mc)
+ def valueOf(x: Long): BigDecimal = apply(x.toDouble)
/** Constructs a `BigDecimal` whose value is equal to that of the
* specified `Integer` value.
@@ -56,10 +57,10 @@ object BigDecimal {
if (mc == defaultMathContext && minCached <= i && i <= maxCached) {
val offset = i - minCached
var n = cache(offset)
- if (n eq null) { n = new BigDecimal(BigDec.valueOf(i), mc); cache(offset) = n }
+ if (n eq null) { n = new BigDecimal(BigDec.valueOf(i.toLong), mc); cache(offset) = n }
n
}
- else new BigDecimal(BigDec.valueOf(i), mc)
+ else new BigDecimal(BigDec.valueOf(i.toLong), mc)
/** Constructs a `BigDecimal` whose value is equal to that of the
* specified long value.
@@ -99,6 +100,9 @@ object BigDecimal {
def apply(d: Double, mc: MathContext): BigDecimal =
new BigDecimal(new BigDec(jl.Double.toString(d), mc), mc)
+ def apply(x: Float): BigDecimal = apply(x.toDouble)
+ def apply(x: Float, mc: MathContext): BigDecimal = apply(x.toDouble, mc)
+
/** Translates a character array representation of a `BigDecimal`
* into a `BigDecimal`.
*/
@@ -193,7 +197,7 @@ extends ScalaNumber with ScalaNumericConversions with Serializable {
*/
def isValidFloat = {
val f = toFloat
- !f.isInfinity && bigDecimal.compareTo(new java.math.BigDecimal(f)) == 0
+ !f.isInfinity && bigDecimal.compareTo(new java.math.BigDecimal(f.toDouble)) == 0
}
/** Returns `true` iff this can be represented exactly by [[scala.Double]]; otherwise returns `false`.
*/