Numbers

advertisement
Numbers
Kinds of numbers

There are basically two kinds of numbers



Integers: …, -3, -2, -1, 0, 1, 2, 3, …
“Real” numbers: 2.5, 0.002, 3.14…, 1.6x1020
In mathematics, integers and real numbers (π, for
example) can have an infinite number of digits

Computers are finite, therefore we have to place limits on
the number of digits and size of exponents
2
Ints


By far the most commonly used integer type in Scala
is the Int
An Int can have any value between -2147483648
and 2147483647 (roughly, plus or minus two
thousand million)


In American (but not British) English, this is two “billion”
In Scala, these are Int.MinValue and Int.MaxValue
3
Other integer types

Scala has other, less frequently used, integer types

Long: -9223372036854775808L to 9223372036854775807L




Notice the L suffix
In Scala, these are Long.MinValue and Long.MaxValue
Short: -32768 to 32767
Char: '\u0000' to '\uFFFF'


Yes, characters may be used as integers!
scala> 'a' + 0
res0: Int = 97
scala> 'a'.toInt
res1: Int = 97


Byte: -127 to 128
Scala also has binary, octal, and hexadecimal numbers, and
arbitrarily large integers—but that’s for another day
4
Integer arithmetic

When you do arithmetic using integers, you get an integer result


So you need to understand integer division:
scala> val n = 20 / 7
n: Int = 2
In Scala, integer arithmetic is done using Int (or Long)

scala> val small: Short = 17
small: Short = 17
scala> val smaller = small - 1
smaller: Int = 16

Also watch out for integer overflow:

scala> val big = Int.MaxValue
big: Int = 2147483647
scala> val bigger = big + 1
bigger: Int = -2147483648
5
Floating point numbers


In mathematics, real numbers have an infinite number of digits
(possibly all zero, but still an infinite number)
In programming, we don’t even use the term “real numbers”—we
call them floating point numbers



This term describes how such numbers are represented on the computer
You will learn about the representation in CIT 593
The usual type for floating-point number is Double


scala> val huge = Double.MaxValue
huge: Double = 1.7976931348623157E308
scala> val tiny = Double.MinPositiveValue
tiny: Double = 4.9E-324
The E stands for “times ten to the”, so 4.9E-324 means 4.9x10-324
6
Floating point numbers are approximate!

Rule: Never compare floating-point numbers for exact equality
(==) or inequality(!=)

scala> val oneTenth = 1.0 / 10.0
oneTenth: Double = 0.1
scala> val threeTenths = 3.0 / 10.0
threeTenths: Double = 0.3
scala> 3.0 * oneTenth == threeTenths
res16: Boolean = false

Such comparisons will sometimes work and sometimes not work



This is worse than having them always not work!
But it’s okay to use <, <=, >, and >=
Rule: Never use floating-point numbers for serious financial
computations

But they are probably okay for doing your checkbook
7
Floats

A second type of floating-point number is the Float

scala> Double.MaxValue
res4: Double = 1.7976931348623157E308
scala> Float.MaxValue
res5: Float = 3.4028235E38


Floats are less accurate and have a smaller range of values than
Doubles
Floats take less memory than Doubles, and can be used on those
(extremely rare) occasions when you need to work with billions of
numbers

To write a literal Float value, suffix the number with an F

scala> 17.3F
res6: Float = 17.3
8
Mixed modes

If you do arithmetic with an integer and a floating-point number, the result is a
floating-point number


You can usually use an integer where a floating-point number is expected


scala> val x = 7 + 1.5
x: Double = 8.5
scala> val y = math.sqrt(4)
y: Double = 2.0
To prevent accidental loss of precision, Scala will not let you use a floatingpoint number where an integer is expected

scala> var count = 0
count: Int = 0
scala> count = count + 1.0
<console>:11: error: type mismatch;
found
: Int
required: ?{def +(x$1: ? >: Double(1.0)): ?}
9
The End
10
Download