Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.6x10 20.

Similar presentations


Presentation on theme: "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.6x10 20."— Presentation transcript:

1 Numbers

2 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.6x10 20 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

3 Int s 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

4 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

5 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

6 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

7 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

8 Float s A second type of floating-point number is the Float scala> Double.MaxValue res4: Double = 1.7976931348623157E308 scala> Float.MaxValue res5: Float = 3.4028235E38 Float s are less accurate and have a smaller range of values than Double s Float s take less memory than Double s, 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

9 Mixed modes If you do arithmetic with an integer and a floating-point number, the result is a floating-point number scala> val x = 7 + 1.5 x: Double = 8.5 You can usually use an integer where a floating-point number is expected scala> val y = math.sqrt(4) y: Double = 2.0 To prevent accidental loss of precision, Scala will not let you use a floating- point number where an integer is expected scala> var count = 0 count: Int = 0 scala> count = count + 1.0 :11: error: type mismatch; found : Int required: ?{def +(x$1: ? >: Double(1.0)): ?} 9

10 10 The End


Download ppt "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.6x10 20."

Similar presentations


Ads by Google