Download presentation
Presentation is loading. Please wait.
Published byCassandra Horton Modified over 8 years ago
1
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Numbers
2
CIT 383: Administrative Scripting Types of Numbers Integers Numbers without decimal points. -3, 0, 2, 65536 Precise operations. 1 + 2 = 3 1 – 2 = -1 1 * 2 = 2 1 / 2 = 0 Floats Numbers with decimal points. -199.9482, 0.0, 3.14 Rounding errors. 1.0 + 2.0 = 3.0 1.0 – 2.0 = -1.0 1.0 * 2.0 = 2.0 1.0 / 2.0 = 0.5
3
CIT 383: Administrative Scripting Why Two Types of Numbers? Different uses –Money calculations should avoid rounding. –Measurements must often be floats. Performance –Floats take more space than integers. (usually) –CPU has separate integer and float units.
4
CIT 383: Administrative ScriptingSlide #4
5
CIT 383: Administrative Scripting Two Types of Integers Fixnum –32-bit machine integer –Fast (calculations in hardware) –Ranges from -2 31 to 2 31 – 1 –Ruby promotes to Bignum beyond range. Bignum –Arbitrary precision integer –Slow (calculations in software) –No limit to size.
6
CIT 383: Administrative Scripting Types of Numbers Numeric Fixnum IntegerFloat Bignum
7
CIT 383: Administrative Scripting Integer Literals Different bases Decimal: 255 Octal: 0377 Binary: 0b11111111 Hexadecimal: 0xFF Readability Insert _ as thousands separator. Can write 1000000000 as 1_000_000_000
8
CIT 383: Administrative Scripting Float Literals Always need a decimal point 1 is an integer, 1.0 is a float Scientific notation Avogadro’s number is 6.0221415e23 Readability 1_000_000_000.0
9
CIT 383: Administrative Scripting Arithmetic Operators Addition: + 7 + 3 == 10 Subtraction: - 7 – 3 == 4 Multiplication: * 7 * 3 == 21 Division: / 13 % 2 == 6 Remainder: % 13 % 2 == 1 Exponentiation: ** 2**8 == 256
10
CIT 383: Administrative Scripting Logical Operations Return a true or false value. Equality 1 == 1 Inequality 1 != 1 Less Than 1 < 2 Greater Than 1 > 2 Less Than or Equal To 1 >= 2 Greater Than or Equal To 1 <= 1
11
CIT 383: Administrative Scripting Floating Point Rounding Machine floats –Stored as binary fractions: ½, ¼, etc. –Decimal fractions: 0.1 cannot be exactly represented, as it’s repeating in binary like 1/3. Don’t use equality tests for floats 0.4 – 0.3 == 0.1 is false Check if difference is sufficiently small (0.4 – 0.3) – 0.1 < 1.0e-9 is true
12
CIT 383: Administrative Scripting Variables Variables allow us to name values x = 1.0 # Assigns the value 1.0 to x x # A variable reference, evals to 1.0 Variable naming –Valid characters: letters, numbers, _ –Name must start with letter or _ –Case sensitive: now, noW, nOw are different –If name starts with capital, it is a constant. –Examples: x, y2, new_val, _secret, PI
13
CIT 383: Administrative ScriptingSlide #13 References 1.Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. 2.David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. 3.Hal Fulton, The Ruby Way, 2 nd edition, Addison-Wesley, 2007. 4.Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2 nd edition, Pragmatic Programmers, 2005.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.