CLD-Lec9-Fixed-Floating-Point-Numbers-Extended
Lecture Overview
Topic: Fixed and Floating Point Numbers
Instructor: Dr. Ngoc-Son Pham
Course: COSE221 Digital Logic Design
Institution: Computer Science & Engineering, Korea University
C Code Examples
Example 1
#include <stdio.h>
int main() {
signed int sa = 7;
signed int sb = -7;
unsigned int ua = *((unsigned int *) &sa);
unsigned int ub = *((unsigned int *) &sb);
printf("sa = %d : ua = 0x%x\n", sa, ua);
printf("sb = %d : ub = 0x%x\n", sb, ub);
return 0;
}Output Interpretation:
Displaying signed and unsigned integer representations.
Example 2
#include <stdio.h>
int main() {
float f1 = -58.0;
unsigned int u1 = *((unsigned int *) &f1);
printf("f1 = %f\n", f1);
printf("f1 = %3.20f\n", f1);
printf("u1 = 0x%X\n", u1);
return 0;
}Understanding Floating-Point Representation:
Shows conversion from float to unsigned integer.
Example 3
#include <stdio.h>
int main() {
double d1 = -58.0;
unsigned long long u1 = *((unsigned long long *) &d1);
printf("d1 = %lf\n", d1);
printf("d1 = %3.20lf\n", d1);
printf("u1 = 0x%llX\n", u1);
return 0;
}Output Interpretation:
Display double value and its unsigned long long representation.
Fixed-Point and Floating-Point Representation
Fixed-Point Numbers
Definition: Implies a binary point fixed at a certain position.
Example:
Representation of 6.75 with 4 integer bits and 4 fraction bits:
0110.1100
Agreement Required: Fixed-point representation needs agreement on number of integer/fraction bits.
Signed Fixed-Point Numbers
Representation: Can be in sign/magnitude or two’s complement.
Example: -2.375:
Sign/magnitude:
1010 0110Two’s complement:
Flip bits:
1101 1001Add 1:
1101 1010
Example Calculation
Calculation of 0.75 + (-0.625):
0.75 = 0000 1100-0.625 = 1111 0110
Floating-Point Numbers
Purpose: Allow representation of very large/small numbers, binary point floats
Scientific Notation Format:
± M × B^EStructure for IEEE 754 32-bit representation:
1 sign bit, 8 exponent bits, 23 mantissa bits
Floating-Point Representation Steps
Convert decimal to binary.
Assign sign bit, exponent, mantissa.
Use biased exponent - IEEE 754 standard uses 127.
Special Cases in Floating-Point Numbers
IEEE 754 Standard: Special Cases
Zero, ∞, NaN (Not a Number)
Floating-Point Precision
Single Precision:
32-bit (1 sign, 8 exponent, 23 fraction)
Range: ±1.175494 x 10^−38 to ±3.402824 x 10^38
Double Precision:
64-bit (1 sign, 11 exponent, 52 fraction)
Range: ±2.22507385850720 x 10^−308 to ±1.79769313486232 x 10^308
Rounding in Floating-Point Operations
Rounding modes:
Round down, up, toward zero, round to nearest.
Floating-Point Addition Steps
Extract exponent/fraction bits.
Prepend leading 1, compare exponents.
Shift as needed, add mantissas.
Normalize and round the result.
Floating-Point Multiplication Process
Multiply Signs:
XOR to determine the sign.
Add Exponents:
Adjust according to bias.
Multiply Mantissas:
Normalize result following the multiplication.
Example Calculation: Multiply 3.0 x 2.5
Convert to IEEE 754 format and perform calculation.
Binary Coded Decimal (BCD)
Concept: Encoding decimal digits in binary.
Example BCD representation:
1.7 = 0001 . 0111