Uploaded by B V

04-datatypes-conversions

advertisement
C Data Types
and Conversions
CSC 230 – C and Software Tools
Fall 2020
Dr. Douglas Reeves
NC State Department of Computer Science
1
Specializations of Fundamental Types
Integers can be…
– signed or unsigned (signed by default)
– really short (char), short, regular (default), long,
really long (long long)
Floating point (always signed) can be…
– regular precision (float)
– double precision (double)
– extended precision (long double)
2
1
“Usual” Min and Max Integer Values
unsigned not in Java
Tutorialspoint.com/cprogramming/c_data_types.htm
3
On Your Machine: Check limits.h
Demo…
4
2
Floating Point (Real Numbers)
Before IEEE Standard, very platform dependent!
Terminology
sign of the
mantissa
+.793 * 2
magnitude of
the mantissa
-36
base of the
exponent
exponent
Size of the exponent (# bits) mainly determines
the range of numbers that can be represented
Size of the mantissa (# bits) mainly determines
the precision of numbers that can be
represented
5
IEEE Standard
Single precision:
– 1-bit sign
– 23-bit (+ 1 implied bit) mantissa
– 8-bit biased exponent (base 2)
Double precision:
– 1-bit sign
– 52+1 bit mantissa
– 11-bit biased exponent (base 2)
6
3
Min and Max
Min (normalized) positive values (approximate)
– single precision (float): 2-126 (≈10-38 )
– double precision (double): 2-1022 (≈10-308)
– Q: small enough to measure the diameter of an atom,
in meters?
Max (normalized) positive values (approximate)
– single precision (float): 2127 (≈1038)
– double precision (double): 21023 (≈10308)
– Q: big enough to count the number of atoms in the
universe? distance to the edge of the observable
universe, in units of atom diameters?
7
Min and Max
long double = 128 bits
– more bits precision than double, same range
8
4
Reminder: Arithmetic Problems
Types make a difference in computer arithmetic
– signed vs. unsigned max and min values (integer)
– overflow (integer and floating point)
– underflow and limited precision (floating point)
Bottom line:
1. In expressions, types of operands should match
2. C will not protect from or warn about overflow –
the program has to check for it
N common source of bugs N
overflow, limits
of precision
9
C Type Conversions
Data type conversions occur in two ways
– explicitly (e.g., programmer deliberately casts from
one type to another)
– or implicitly (e.g., variables of different types are
combined in a single expression, compiler casts from
one type to another)
unsigned char a;
int b;
float c;
double d;
…
c = (float) b;
d = a + (b * c);
10
5
Casting (Explicit Conversion)
Programmer knows what they’re doing and wishes
to force a specific type conversion
Syntax: (typename) expression
d = (double) c;
Being careful / deliberate, not careless
A special case: (void) expression;
– means value of an expression must not be used in
any way
– Q: how could that possibly be useful?
11
Converting signed to unsigned
This only makes sense if you are sure the value
stored in the signed operand is positive
If signed is the shorter operand, extend it
int a;
unsigned b;
a = -36;
b = (unsigned) a;
a = (int) b;
int a;
unsigned short b;
a = -36;
b = (unsigned short) a;
a = (int) b;
Result when output:
b = 4294967260
a = -36
Result when output:
b = 65500
a = 65500
What happened???
12
6
Converting unsigned to signed
If signed is large enough to store the correct
value, no problems
– otherwise, will definitely be an error (overflow)!
int a;
unsigned int b;
b = 3000000000;
a = (int) b;
Result when output:
b = 3000000000
a = -1294967296
13
Floating to Integer
Rounds towards zero (“truncate”) to get the
integer part, and discard the fractional part
– +3.999 ® 3
– -3.999 ® -3
– obviously some loss of precision can occur here
Overflow if the integer variable is too small
float f = 1.0e10;
int i;
i = f;
Result when output:
f = 10000000000.0
i = -2147483648
14
7
Converting to Floating
Integer ® Floating
– if value cannot be represented exactly in floating
point, converts to the closest value (either higher
or lower) that can be represented in floating point
Double precision ® Single precision
– if value cannot be represented exactly, converts to
closest value (either higher or lower)
– can overflow or underflow!
15
Implicit Conversions
For “mixed type” expressions, e.g.,
d = a + (b * c);
double
float
short int
unsigned char
The compiler does “the usual arithmetic
conversions” before evaluating the expression
char’s and short’s are always converted to
ints (or unsigned ints) before evaluating
expressions
16
8
The “Usual Conversions”
For Arithmetic Operations
In a nutshell: when combining values of two
numbers…
1. if either is floating point, convert the other to
floating point, and
2. convert less precise to more precise
Order is significant in the following table!
17
The “Usual…” (cont’d)
Rule
If either
operand is…
And other
operand is…
Then convert other
operand to…
#1
long double
Anything
long double
Else…
#2
double
Anything
double
Else…
#3
float
Anything
float
Else…
#4
unsigned long
int
Anything
unsigned long int Else…
#5
long int
unsigned
int
! unsigned long
int
#6
long int
Anything else long int
Else…
#7
unsigned int
Anything
Else…
#8
unsigned int
Else…
(both operands have
type int, no action
needed)
18
9
Example
double
float unsigned char
short int
d = a + (b * c);
before evaluating expression:
convert b to unsigned int
before multiplying:
convert c to unsigned int (rule #7)
before adding:
convert result of multiplying to float (rule #3)
when assigning:
convert result of addition to double (rule #2)
19
10
Download