You are on page 1of 8

C++ Programming

Floating point types

Mostafa S. Ibrahim
Teaching, Training and Coaching since more than a decade!

Artificial Intelligence & Computer Vision Researcher


PhD from Simon Fraser University - Canada
Bachelor / Msc from Cairo University - Egypt
Ex-(Software Engineer / ICPC World Finalist)
3 Data types
Floating point range: IEEE 754 representation

Img Src
Code
Internal floating-number representation
● This might be a bit complicated now
● Why floating-number values has much bigger range than integer?
○ Different representation
○ For 32 bit e.g.: 24 bits (mantissa part), 7 bits (exponent part), 1 bit for sign
○ This allows a bigger range, BUT less accurate
● Double is (64 bits, 15 digits, double precision) vs Float (32 bits, digits, single
precision)
NaN and Infinity
Tips
● Always use double, not float/long double, unless a good reason
● Be careful in financial or currency applications
● Never compare floating points directly
● Future readings
○ Floating-point Representation
○ Representation of Integers and Reals: TopCoder - By Misof
○ What Every Computer Scientist Should Know About Floating-Point Arithmetic
○ Comparing Floating Point Numbers
“Acquire knowledge and impart it to the people.”

“Seek knowledge from the Cradle to the Grave.”

You might also like