Hexadecimal floating point

How can I represent a given floating point number in hexadecimal? For instance,

60123,124;
+3
source share
3 answers

<sign>0x1.<mantissa>pΒ±<exponent>

>>> (1.2).hex()
'0x1.3333333333333p+0'
>>> (1125.2).hex()
'0x1.194cccccccccdp+10'
>>> (7e85).hex()
'0x1.204362b6da56fp+285'
>>> (5e-3).hex()
'0x1.47ae147ae147bp-8'
>>> (-8.).hex()
'-0x1.0000000000000p+3'

>>> (60123.124).hex()
'0x1.d5b63f7ced917p+15'
+1
source

Here (AU) we use the decimal point:

60123.124

Which my calculator is converted to six like this:

0xEADB.1FBE76C8B43958106

The principle is the same: where in base 10 the first decimal place represents the 10th, in base 16 the first decimal number is 16th.

+1
source

See this related question.

The format specifier is %a printfdescribed here.

0
source

Source: https://habr.com/ru/post/1755339/


All Articles