Skip to content

Make BigDecimalFromDigits extend FromDigits.Floating #7420

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

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/src/scala/util/FromDigits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ object FromDigits {
def fromDigits(digits: String, radix: Int): BigInt = BigInt(digits, radix)
}

given BigDecimalFromDigits : FromDigits.Decimal[BigDecimal] {
given BigDecimalFromDigits : FromDigits.Floating[BigDecimal] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like the correct fix, but then in which case would you actually use FromDigits.Decimal ? It seems like it's not really useful on its own /cc @odersky

Copy link
Member Author

@bishabosha bishabosha Oct 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if there is no sparse representation for the implementation then it could make sense, but it's simple to convert from an exponent anyway.

def fromDigits(digits: String): BigDecimal = BigDecimal(digits)
}
}
2 changes: 2 additions & 0 deletions tests/run/genericNumLits.check
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
13232202002020202020202
-50390822187678765893036
132322020020.223
1.00000000000000001234E+239
1.00000000000000001234E-203
Even(1234)
malformed
18 changes: 17 additions & 1 deletion tests/run/genericNumLits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ object Test extends App {
val x: BigInt = 13232202002020202020202
val y: BigInt = -0xaabb12345ACF12345AC
val z: BigDecimal = 132322020020.223
val w: BigDecimal = 10000000000000000.1234e223
val q: BigDecimal = 100000000000000001234e-223

case class Even(n: Int)

Expand All @@ -20,6 +22,8 @@ object Test extends App {
println(x)
println(y)
println(z)
println(w)
println(q)
println(e)

try println(123: Even)
Expand All @@ -45,6 +49,18 @@ object Test extends App {
(z: Any) match {
case 132_322_020_020.223: BigDecimal => ()
}
w match {
case 10000000000000000.1234e223 => ()
}
(w: Any) match {
case 10_000_000_000_000_000.1234e223: BigDecimal => ()
}
q match {
case 100000000000000001234e-223 => ()
}
(q: Any) match {
case 100_000_000_000_000_001_234e-223: BigDecimal => ()
}

e match {
case 1234 =>
Expand All @@ -54,4 +70,4 @@ object Test extends App {
case 1234: Even =>
case _: Even =>
}
}
}