Академический Документы
Профессиональный Документы
Культура Документы
30 October 2012
Data types in C
The basic data types in C are
I
int, an integer
Susi Lehtola
Data types
2/19
Data types in C
The basic data types in C are
I
int, an integer
... but what are these, really, and why do we need them?
Susi Lehtola
Data types
3/19
Data types in C
The basic data types in C are
I
int, an integer
... but what are these, really, and why do we need them?
Lets return to that in a bit.
Scientific Programming in C, fall 2012
Susi Lehtola
Data types
4/19
Constants
I
1u or 1U is an unsigned integer
1l or 1L is a long integer
I 0 x0
I
is a character constant
Susi Lehtola
Data types
5/19
Representation of integers
xxxxxxxx
00000000
to
1
99999999
Susi Lehtola
Data types
6/19
Binary system
Susi Lehtola
Data types
7/19
Susi Lehtola
Data types
8/19
5.2917721 1011 m
Susi Lehtola
Data types
9/19
Floating-point numbers
Applying the same principle to represent real numbers on the
computer, we end up with the floating-point system.
Numbers are represented in the format
x = mantissa baseexponent
Type
float
double
80-bit double
quad
Sign
1
1
1
1
Exp.
8
11
15
15
Mantissa
23
52
64
112
Total bits
32
64
80
128
Decimal digits
7.2
15.9
19.2
34.0
Source: http://en.wikipedia.org/wiki/Floating_point
Susi Lehtola
Data types
10/19
Roundoff errors
Because of the floating-point system, the accuracy is not universal
anymore. Whereas the result of, e.g., additions in the fixed-point
system is independent of the order of summation, in the
floating-point system the result may depend on the order the
elements are summed in.
The machine epsilon is inextricably linked to this. It is defined as
the smallest number for which
1 + > 1.
For example:
+1+ =
1
2
2
+ +1= 1+
2 2
For float is normally around 107 and for double around 1016 .
Scientific Programming in C, fall 2012
Susi Lehtola
Data types
11/19
Due to the finite precision and roundoff errors, its rather rare that
floating-point arithmetic gives you exactly the number you expect.
If you expect x to be equal to y , instead of using the comparison
x y
use
|x y | 10 max{x, y }
to find out if this is true.
Susi Lehtola
Data types
12/19
Susi Lehtola
Data types
13/19
Susi Lehtola
Data types
14/19
If statements
If you have if statements within if statements, with else
statements, you really should use braces to be clear.
i f ( i ==0)
i f ( j ==0)
k =0;
else
k =1;
Here the else statement refers to the last if statement (which is
made clear by the indentation). But compilers often warn about
these kinds of statements, since they might lead to unexpected
behavior.
i f ( i ==0) {
i f ( j ==0)
k =0;
else
k =1;
}
Scientific Programming in C, fall 2012
Susi Lehtola
Data types
15/19
Loop example
Connection between Fahrenheit and centigrade temperature scale
is F = 1.8 C + 32
#i n c l u d e < s t d i o . h>
float celcius to fahr ( float c) {
r e t u r n 1 . 8 f c +32.0 f ;
}
i n t main ( v o i d ) {
int i ;
f o r ( i =0; i <=100; i +=10)
p r i n t f ( %i %f \n , i , c e l c i u s t o f a h r ( c ) ) ;
return 0;
}
Scientific Programming in C, fall 2012
Susi Lehtola
Data types
16/19
Susi Lehtola
Data types
17/19
If example
The realization of the function
(
a, x a
y (x) =
x, x > a
is
double c u t o f f ( double x , double a ) {
i f ( x<=a )
return a ;
else
return x ;
}
Susi Lehtola
Data types
18/19
Operators
The operators that are available in C are
=
/
+
%
assignment
multiplication
division
addition
substraction
modulus division
equality
greater than
at least
smaller than
at most
Susi Lehtola
Data types
19/19