scala / docs.scala-lang

The Scala Documentation website
http://docs.scala-lang.org
561 stars 1.03k forks source link

Scala 3 book - reason for using BigInt, BigDecimal unclear #2033

Open Sciss opened 3 years ago

Sciss commented 3 years ago

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
chaitanyawaikar commented 3 years ago

I can pick this up.