Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining floating point values by base and exponent explicitly

I stumbled upon http://sourceware.org/ml/glibc-cvs/2013-q1/msg00115.html, which includes the line

#define  TWO5      0x1.0p5      /* 2^5     */

Apparently, TWO5 is defined as a double with the explicit value 1<<5. However, this is the first time I see this notation. How has this format been in use, and what is the advantage over just writing 2.5?

like image 730
mafu Avatar asked Feb 06 '13 12:02

mafu


People also ask

How do you find the floating-point of an exponent?

To do so, floating-point uses a biased exponent, which is the original exponent plus a constant bias. 32-bit floating-point uses a bias of 127. For example, for the exponent 7, the biased exponent is 7 + 127 = 134 = 100001102. For the exponent −4, the biased exponent is: −4 + 127 = 123 = 011110112.

How are the floating-point values represented?

In computers, floating-point numbers are represented in scientific notation of fraction ( F ) and exponent ( E ) with a radix of 2, in the form of F×2^E . Both E and F can be positive as well as negative.

Why do floating-point numbers require an exponent bias?

The biased exponent is used for the representation of negative exponents. The biased exponent has advantages over other negative representations in performing bitwise comparing of two floating point numbers for equality. The range of exponent in single precision format is -128 to +127.

What are the 2 types of floating-point?

There are two floating point primitive types. Data type float is sometimes called "single-precision floating point". Data type double has twice as many bits and is sometimes called "double-precision floating point".


1 Answers

This notation was introduced in C99. The advantage is that the value is expressed in hexadecimal form, so it is not subject to rounding etc. that occurs when you convert a floating-point value between the underlying representation and a decimal form.

There are plenty of pages that describe this notation, for example:

http://www.exploringbinary.com/hexadecimal-floating-point-constants/

like image 52
Lindydancer Avatar answered Nov 15 '22 05:11

Lindydancer