Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hexadecimal Floating-point literals [duplicate]

How does 0x12.2P2 equal 72.5?

I know that the value following the P, called the binary exponent, indicates the power-of-two by which the number is multiplied?

like image 208
Farhan Shirgill Ansari Avatar asked Sep 07 '14 16:09

Farhan Shirgill Ansari


People also ask

What is a floating-point literal?

Floating-point literals are numbers that have a decimal point or an exponential part. They can be represented as: Real literals. Binary floating-point literals.

Which of the following are valid floating-point literals?

2. Which of the following is a valid floating-point literal? Explanation: To make a floating point literal, we should attach a suffix of 'f' or 'F' and there should not be any blank space. 3.

What is the difference between a floating point number and a floating-point literal?

Integer literals represent fixed integer values like 900, 12, 400, -222 etc. (with in the integer range). Whereas, floating point literals represents fractional values i.e. numbers with decimal values like 25.53, 45.66, 58.66 etc. while writing these literals we should use the notation f or F as 25.53.

Why floating-point numbers Cannot be stored accurately?

Floating-point decimal values generally do not have an exact binary representation. This is a side effect of how the CPU represents floating point data. For this reason, you may experience some loss of precision, and some floating-point operations may produce unexpected results.


1 Answers

0x12.2P2 is 0x122 / 1610 * 22

  • 0x122 = 29010
  • divided by 1610 = 18.12510
  • multiplied by 22 leads to 72.510

More formally (?), for the part before the P, you can use this very classic conversion table:

     decimal point here  ▼
+-----+-----+-----+-----+-+-----+-----+-----+
| 16³ | 16² | 16¹ | 16⁰ | | 16⁻¹| 16⁻²| 16⁻³|
+-----------------------------------------+
|     |     |  1  |  2  |.|  2  |     |     |
+-----+-----+-----+-----+-+-----+-----+-----+

So 0x12.2 is 1⨯16¹+2x16⁰+2⨯16⁻¹ = 18.125

like image 79
Sylvain Leroux Avatar answered Sep 26 '22 17:09

Sylvain Leroux