08 Optional Floating point in C

background image

University of Washington

Section 2: Integer & Floating Point Numbers

Representation of integers: unsigned and signed

Unsigned and signed integers in C

Arithmetic and shifting

Sign extension

Background: fractional binary numbers

IEEE floating-point standard

Floating-point operations and rounding

Floating-point in C

Floating Point in C

background image

University of Washington

Floating Point in C

C offers two levels of precision

float

single precision (32-bit)

double

double precision (64-bit)

Default rounding mode is round-to-even

#include <math.h> to get INFINITY and NAN constants

Equality (==) comparisons between floating point numbers are
tricky, and often return unexpected results

Just avoid them!

Floating Point in C

background image

University of Washington

Floating Point in C

Conversions between data types:

Casting between int, float, and double changes the bit
representation!!

int → float

May be rounded; overflow not possible

int → double or float → double

Exact conversion, as long as int has ≤ 53-bit word size

double or float → int

Truncates fractional part (rounded toward zero)

Not defined when out of range or NaN: generally sets to Tmin

Floating Point in C

background image

University of Washington

Summary

Zero

Normalized values

Infinity

NaN

Denormalized values

Floating Point in C

0 00000000 00000000000000000000000

s 1 to 2

k

-2 significand = 1.M


s 11111111 00000000000000000000000

s 11111111 non-zero

s

exp

frac

s 00000000 significand = 0.M

background image

University of Washington

Summary (cont’d)

As with integers, floats suffer from the fixed number of bits
available to represent them

Can get overflow/underflow, just like ints

Some “simple fractions” have no exact representation (e.g., 0.2)

Can also lose precision, unlike ints

“Every operation gets a slightly wrong result”

Mathematically equivalent ways of writing an expression
may compute different results

Violates associativity/distributivity

Never test floating point values for equality!

Floating Point in C


Wyszukiwarka

Podobne podstrony:
08 Optional Floating point in C
07 Optional Floating point Operations
07 Optional Floating point Operations
06 Optional IEEE Floating point Standard
06 Optional IEEE Floating point Standard
08 Kwestia potencjalności C (zarodki in vitro)
08 Option Lock
Options in Fiber
08 in bhp frezarka do słupka okna pl
08-in. ppoż w lokalu gastronomicznym, Instrukcje BHP, XIX - P.POŻ
How Chi Is Used In Dim Mak Pressure Point?fence
Gifford, Lazette [Quest for the Dark Staff 08] Hope in Hell [rtf](1)
Asimov, Isaac Robot 08 Pebble In The Sky(1)
you cant do that in america tn 29 05 08
Why the US is losing in Afghanistan A H Cordesman 1 Oct 08 doc
Asimov, Isaac Robot 08 Pebble In The Sky
Rueda Electromagnetic Zero Point Field as Active Energy Source in the Intergalactic Medium (1999)
GARCH Options in Incomplete Markets
Bova, Ben Orion 08 Orion in the Dying Time

więcej podobnych podstron