Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part...

6
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part III

Transcript of Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part...

Page 1: Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part III.

Introduction to Computer Organization & Systems

Introduction to Computer Organization & Systems

Topics:Types in C: floating point

COMP 21000

C Part III

Page 2: Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part III.

Types: floating point

• Family of float types. – float (often 16 bits)– double (often 32 bits)– long double (often 64 bits)

2-2

Page 3: Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part III.

Floating Point

2-3

1 #include <stdio.h> 2 3 /* print Fahrenheit-Celsius table 4 for f = 0, 20, ... , 300 */ 5 main() 6 { 7 int lower, upper, step; 8 float fahr, celsius; 9 10 lower = 0; /* lower limit of temperature table */ 11 upper = 300; /* upper limit */ 12 step = 20; /* step size */ 13 14 fahr = lower; 15 while (fahr <= upper) { 16 celsius = (5.0/9.0) * (fahr - 32.0); 17 printf("%4.0f %6.1f\n", fahr, celsius); 18 fahr = fahr + step; 19 } 20 }

Formatting instructionsFor floating point output

Page 4: Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part III.

2-4

Floating point input/output

Page 5: Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part III.

Floating point input/output

2-5

A conversion specification begins with a percent sign character, %, and has the following elements in order: 1. Zero or more flag characters: –, +, 0, #, or space, which modify the meaning of the conversion operation. 2. An optional minimum field width expressed as a decimal integer constant. 3. An optional precision specification expressed as a period optionally followed by a decimal integer.

This is the min number of digits to be printed in d, I, o, u, x and X conversions. Is the num of digits to right of decimal point in e, E, and f. Is the maximum number of characters in s conversion.

4. An optional size specification expressed as on of the letters ll, l, L, h, hh, j, z or t which tells whether conversion is to a long or a short when necessary. 5. The conversion operation, a single character from the set a, A, c, d, e, E, f, g, G, I, n, o, p, s, u, x, X, %

“%+5.2hf”

Page 6: Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP 21000 C Part III.

Conversion

2-6

Size specification changes output type