Professional Documents
Culture Documents
Lect15 PDF
Lect15 PDF
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3
Note
Note
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5
Example
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6
In a C Program
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8
Bit Patterns
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
s exponent significand/mantissa
1−bit 8−bits 23−bits
Single Precession (32−bit)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
s exponent significand/mantissa
1−bit 11−bits 20−bits
significand (continued)
32−bits
Double Precession (64−bit)
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10
Bit Pattern
#include <stdio.h>
void printFloatBits(float);
int main() // floatBits.c
{
float x;
printf("Enter a floating-point numbers: ");
scanf("%f", &x);
printf("Bits of %f are:\n", x);
printFloatBits(x);
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11
putchar(’\n’);
return 0;
}
void printBits(unsigned int a){
static int flag = 0;
if(flag != 32) {
++flag;
printBits(a/2);
printf("%d ", a%2);
--flag;
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 12
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 13
−1.0 1 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.7 0 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0
2.0 × 10−38 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 0 1
2.0 × 10−39 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 1 0 0 0 0
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 14
Interpretation of Bits
Types of Data
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 16
NaN
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 17
NaN
#include <stdio.h>
#include <math.h>
int main() // nan.c
{
printf("0.0/0.0: %f\n", 0.0/0.0);
printf("inf/inf: %f\n", (1.0/0.0)/(1.0/0.0));
printf("0.0*inf: %f\n", 0.0*(1.0/0.0));
printf("-inf + inf: %f\n", (-1.0/0.0) + (1.0/0.0));
printf("sqrt(-1.0): %f\n", sqrt(-1.0));
printf("log(-1.0): %f\n", log(-1.0));
return 0;
}
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 18
NaN
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 20
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 21
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 22
Infinity: inf
0 11111111 00000000000000000000000
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 23
Zero: 0.000000e+00
0 00000000 00000000000000000000000
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 24
s e−127
(−1) × 1.m × 2
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 26
An Example
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 27
An Example
An Example
Range of Significand
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 30
Count of Numbers
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 31
Count of Numbers
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 32
s −126
(−1) × 0.m × 2
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 33
Note
Note
Note
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 36
Note
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 37
Note
Infinity:
∞: 1111 1111 000 0000 0000 0000 0000 0000
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 38
Note
± Zeros
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 40
#include <stdio.h>
int main() // twoZeros.c
{
double a = 0.0, b = -0.0 ;
$ cc -Wall twoZeros.c
$ a.out
a: 0.000000, b: -0.000000
Equal
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 42
Largest +1 = ∞
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 43
Largest +1 = ∞
#include <stdio.h>
int main() // infinity.c
{
float f = 1.0/0.0 ;
int *iP ;
return 0 ;
& %
}
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 44
Largest +1 = ∞
$ cc -Wall infinity.c
$ ./a.out
f: inf
f: 340282346638528859811704183484516925440.00
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 45
Note
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 46
Note
#include <stdio.h>
#include <math.h>
int main() // infinity1.c
{
float f ;
f = 1.0/0.0 ;
printf("atan(%f) = %f\n",f,atan(f));
printf("1.0/%f = %f\n", f, 1.0/f) ;
return 0;
}
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 47
$ cc -Wall infinity1.c
$ ./a.out
atan(inf) = 1.570796
1.0/inf = 0.000000
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 48
Note
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 49
A Few Programs
& %
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 50
int isInfinity(float)
& %
}
Lect 15 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 51
int isNaN(float)
& %
Lect 15 Goutam Biswas