Introduction
In Scala, all the numeric types are objects, including Byte
, Char
,
Double
, Float
, Int
,
Long
, and Short
. These seven numeric types extend the
AnyVal
trait, as do the Unit
and Boolean
classes, which are considered to be
“nonnumeric value types.”
As shown in Table 2-1, the seven built-in numeric types have the same data ranges as their Java primitive equivalents.
Table 2-1. Data ranges of Scala’s built-in numeric types
Data type | Range |
---|---|
| 16-bit unsigned Unicode character |
| 8-bit signed value |
| 16-bit signed value |
| 32-bit signed value |
| 64-bit signed value |
| 32-bit IEEE 754 single precision float |
| 64-bit IEEE 754 single precision float |
In addition to those types, Boolean
can have the values true
or false
.
If you ever need to know the exact values of the data ranges, you can find them in the Scala REPL:
scala>Short.MinValue
res0: Short = −32768 scala>Short.MaxValue
res1: Short = 32767 scala>Int.MinValue
res2: Int = −2147483648 scala>Float.MinValue
res3: Float = −3.4028235E38
In addition to these basic numeric types, it’s helpful to understand
the BigInt
and BigDecimal
classes, as well as the methods in
the scala.math
package. These are all
covered in this chapter.
Complex Numbers and Dates
If you need more powerful math classes than those that are
included with the standard Scala distribution, check out the Spire project, which includes
classes like Rational
, Complex
, Real
, and more; and ScalaLab, which offers
Matlab-like scientific computing in Scala.
For processing ...
Get Scala Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.