docs.scala-lang icon indicating copy to clipboard operation
docs.scala-lang copied to clipboard

Scala 3 book - reason for using BigInt, BigDecimal unclear

Open Sciss opened this issue 4 years ago • 1 comments

They are introduced here: https://docs.scala-lang.org/scala3/book/taste-vars-data-types.html and here: https://docs.scala-lang.org/scala3/book/first-look-at-types.html

The description is vague and not helpful:

When you need really large numbers, use the BigInt and BigDecimal types:

var a = BigInt(1_234_567_890_987_654_321L)
var b = BigDecimal(123_456.789)

It is not apparent to reader why these are "really large numbers", and why they should use them instead of directly writing 1_234_567_890_987_654_321L and 123_456.789.

I suggest this be reworded to say technically correct something like

When you need to use numbers so large they exceed the range of Long, or that require a higher number of decimal digits than support by Double, use the BigInt and BigDecimal types

And instead of passing primitive values to the constructors, use the string constructors that demonstrate the "really large numbers", e.g.

BigInt("1234567890000000000000000")

Then, instead of just saying

A great thing about BigInt and BigDecimal is that they support all the operators you’re used to using with numeric types:

point out that for example BigInt does not produce numeric overflow:

1234567890 * 1234567890  // silently overflowing to become 304084036
BigInt(1234567890) * BigInt(1234567890) // correct: 1524157875019052100

Sciss avatar May 18 '21 21:05 Sciss

I can pick this up.

chaitanyawaikar avatar May 19 '21 09:05 chaitanyawaikar