02-datatypes

download 02-datatypes

of 19

Transcript of 02-datatypes

  • 7/21/2019 02-datatypes

    1/19

    Scientific Programming in C

    II. Data types

    Susi Lehtola

    30 October 2012

    http://find/http://goback/
  • 7/21/2019 02-datatypes

    2/19

    Data types in C

    The basic data types in C are

    char, a single byte, capable of holding a character in thecurrent character set

    int, an integer

    float, single precision floating point number

    double, double precision floating point number

    There are also modifiers

    shortand longfor integers

    signed(default) or unsignedfor characters and integers

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 2/19

  • 7/21/2019 02-datatypes

    3/19

    Data types in C

    The basic data types in C are

    char, a single byte, capable of holding a character in thecurrent character set

    int, an integer

    float, single precision floating point number

    double, double precision floating point number

    There are also modifiers

    shortand longfor integers

    signed(default) or unsignedfor characters and integers

    ... but what are these, really, and why do we need them?

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 3/19

  • 7/21/2019 02-datatypes

    4/19

    Data types in C

    The basic data types in C are

    char, a single byte, capable of holding a character in thecurrent character set

    int, an integer

    float, single precision floating point number

    double, double precision floating point number

    There are also modifiers

    shortand longfor integers

    signed(default) or unsignedfor characters and integers

    ... 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

  • 7/21/2019 02-datatypes

    5/19

    Constants

    1 is an integer type constant

    1uor1Uis an unsigned integer

    1lor1L is a long integer

    1ulor1UL is an unsigned long integer

    1.0is a double precision floating point constant

    1.0f is a single precision floating point constant

    1.0l is a long double

    x is a character constant

    xyz is a string constant (a constant character array)

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 5/19

  • 7/21/2019 02-datatypes

    6/19

    Representation of integers

    To represent integers we need to define the maximum size of thenumber we use. For example, with 8 digits

    1 xxxxxxxx

    we are able to represent numbers from1 00000000

    to

    1 99999999

    in the 10-base system.

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 6/19

  • 7/21/2019 02-datatypes

    7/19

    Binary system

    Computers use the binary system, in which

    000000002= 010

    and

    111111112= 25510

    If we use the first bit to define the sign, our range is from 128 to127.

    We can extend or reduce the range we span by adding or removingmore bits from the data type.

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 7/19

  • 7/21/2019 02-datatypes

    8/19

    Representation of real numbers

    Extending the logic of the integer system, we can construct realnumbers by fixing a decimal point somewhere. Naturally, as thereis an infinite number of real numbers in any given finite interval,we introduce rounding errors.

    For example, in the convention that the last 3 digits are decimals,the raw-data value 12957203 would correspond to the number12957.203.

    This is known as the fixed-point system. Its not available in any

    (standard) programming languages but it can be manuallyimplemented using integers if necessary.

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 8/19

  • 7/21/2019 02-datatypes

    9/19

    Representation of decimal numbers, contd

    As practical applications often use real numbers with a huge range,usage of the fixed-point system is not computationally feasible.Computing the value of simple functions such as y=x2 would bedifficult.

    In practice scientists dont write out all the zeros and the decimals,but use the scientific notation instead. For instance the Bohrradius is

    a0 0.0000000000052917721 m

    = 5.29177211011 m

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 9/19

  • 7/21/2019 02-datatypes

    10/19

    Floating-point numbers

    Applying the same principle to represent real numbers on thecomputer, we end up with the floating-point system.

    Numbers are represented in the format

    x= mantissa baseexponent

    Type Sign Exp. Mantissa Total bits Decimal digits

    float 1 8 23 32 7.2double 1 11 52 64 15.9

    80-bit double 1 15 64 80 19.2quad 1 15 112 128 34.0

    Source: http://en.wikipedia.org/wiki/Floating_point

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 10/19

    http://en.wikipedia.org/wiki/Floating_pointhttp://en.wikipedia.org/wiki/Floating_point
  • 7/21/2019 02-datatypes

    11/19

    Roundoff errors

    Because of the floating-point system, the accuracy is not universalanymore. Whereas the result of, e.g., additions in the fixed-point

    system is independent of the order of summation, in thefloating-point system the result may depend on the order theelements are summed in.

    The machine epsilon is inextricably linked to this. It is defined as

    the smallest number for which

    1 + >1.

    For example:

    2+ 1 +

    2= 1

    2+

    2+ 1 = 1 +

    For float is normally around 10

    7 and for doublearound 10

    16.Scientific Programming in C, fall 2012 Susi Lehtola Data types 11/19

  • 7/21/2019 02-datatypes

    12/19

    Comparison of floating-point numbers

    Due to the finite precision and roundoff errors, its rather rare thatfloating-point arithmetic gives you exactly the number you expect.If you expect xto be equal to y, instead of using the comparison

    xy

    use|xy| 10max{x, y}

    to find out if this is true.

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 12/19

  • 7/21/2019 02-datatypes

    13/19

    Loops and if statements

    One of the most common structures in programming are loops andif statements. The structure of these in C is the following

    i n t i ;i n t x=0;f o r( i =0; i

  • 7/21/2019 02-datatypes

    14/19

    Loops and if statements, contdThe scope of if and foris the following statement, so if you wantto run more statements, enclose them in braces.

    i n t i =0;double j =0.0 ;double k = 0 . 0 ;f o r( i =0; i

  • 7/21/2019 02-datatypes

    15/19

    If statementsIf you have if statements within if statements, with elsestatements, you really should use braces to be clear.

    i f( i ==0)i f( j ==0)k=0;

    e l s e

    k=1;

    Here the elsestatement refers to the last if statement (which ismade clear by the indentation). But compilers often warn aboutthese kinds of statements, since they might lead to unexpectedbehavior.

    i f( i ==0) {i f( j ==0)

    k=0;e l s e

    k=1;

    }Scientific Programming in C, fall 2012 Susi Lehtola Data types 15/19

  • 7/21/2019 02-datatypes

    16/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>

    f l o a t c e l c i u s t o f a h r (f l o a t c ) {

    r e t u r n 1 . 8 f c +32.0 f ;}

    i n t m a i n ( v o i d ) {i n t i ;

    f o r( i =0; i

  • 7/21/2019 02-datatypes

    17/19

    Loop example, contd

    $ ./ a . out0 3 2. 00 00 0010 5 0 .0 0 00 0 020 6 8 .0 0 00 0 030 8 6 .0 0 00 0 0

    40 1 0 4. 0 00 0 0050 1 2 2. 0 00 0 0060 1 4 0. 0 00 0 0070 1 5 8. 0 00 0 0080 1 7 6. 0 00 0 0090 1 9 4. 0 00 0 00100 2 1 2 . 0 00 0 0 0

    Scientific Programming in C, fall 2012 Susi Lehtola Data types 17/19

    f

  • 7/21/2019 02-datatypes

    18/19

    If example

    The realization of the function

    y(x) =

    a, xa

    x, x >a

    is

    double c u t o f f ( double x , double a ) {i f( x

  • 7/21/2019 02-datatypes

    19/19

    Operators

    The operators that are available in C are

    = assignment multiplication/ division+ addition- substraction% modulus division

    There are also the following comparison operators

    == equality

    > greater than>= at least< smaller than