SFTW120-LectureNotes-08

SFTW120-LectureNotes-08 - SFTW120 Programming Science...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 SFTW120 Programming Science Lecture Notes 8: C: Simple Data Types (Textbook Chapter 7) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 8 Simple data types s A simple data type can be used for variables that store only a single value s Examples of simple data types in C: b Numeric integer types: short, int, long b Numeric real number types: float, double b Character type: char s Numeric integer types can be either signed (default) or unsigned (by adding the keyword unsigned before the type) s Type double can be made longer and more precise (by adding the keyword long before the type)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 8 Comparison of numeric types Integer types ( short, int, long ) s Can only represent integer values (like 13, 14, …) s Always precise (no rounding errors) s Efficient storage: uses less storage space than real number types s Efficient operations: faster than operations on real number types Real number types ( float, double ) s Can represent integer values (like 13) and also values with fractions (like 13.2854) s A little imprecise (subject to rounding errors) s Uses more storage space than integer types s Operations on real number types are a little slower than operations on integer types SFTW120 - Programming Science 4 Lecture Notes 8 Types, storage space, and typical ranges On a PC running Linux with gcc compiler: ± 1.1897 × 10 4932 ± 3.3621 × 10 -4932 12 long double ± 1.7977 × 10 308 ± 2.2251 × 10 -308 8 double ± 3.4028 × 10 38 ± 1.1755 × 10 -38 4 float 4,294,967,295 0 4 unsigned long 2,147,483,647 -2,147,483,648 4 long 4,294,967,295 0 4 unsigned int 2,147,483,647 -2,147,483,648 4 int 65,535 0 2 unsigned short 32,767 -32,768 2 short Range to Range from Bytes Type
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 8 Floating point number format s Defined by IEEE 754 (IEEE Standard for Binary Floating-Point Arithmetic), most widely-used s (Simplified) number representation: b s : significand (with implied radix point at defined position) b b : base (2 or 10) b e : exponent s Example: b s = 123, b = 10, e = 2, radix point after first digit B number n = 1.23 × 10 2 = 123 e b s × SFTW120 - Programming Science 6 Lecture Notes 8 IEEE 754 double format s 64 bits: b Sign: 1 bit b Exponent ( e ): 11 bits b Significand, i.e. fraction ( s ): 52 bits (+1 implied digit) b Exponent bias: 1023 s Number representation: s Example: b Binary number 0 01111111111 0000000000000000000000000000000000000000000000000000 b sign = 0, e = 1023, s = 0 B number n = -1 0 × 2 1023-1023 × 1.0 = 1 × 1 x 1.0 = 1.0 s bias e sign . 1 2 ) 1 ( × ×
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

SFTW120-LectureNotes-08 - SFTW120 Programming Science...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online