How do you calculate floating point in a radius other than 10?

Given the Wikipedia article on Radix Point , how can one calculate the binary equivalent of 10.1 or the hexadecimal equivalent of 17.17? For the first, what is the binary equivalent of the tenth? For the latter, the hexadecimal representation is 17/100?

I am looking more for an algorithm than for solutions for only these two examples.

+3
source share
5 answers

To convert decimal 10.1 to binary, separate the integer and fractional parts and convert each separately.

, 2, :

10/2 = 5 0

5/2 = 2 1

2/2 = 1 0

1/2 = 0 1

: 1010

, 2, . :

0,1 * 2 = 0,2

0,2 * 2 = 0,4

0,4 ​​* 2 = 0,8

0,8 * 2 = 1,6

0,6 * 2 = 1,2

0,2 * 2 = 0,4

0,4 ​​* 2 = 0,8

... ( )

, 0,1 0,000110011001100...

( . dec2bin_i() dec2bin_f() http://www.exploringbinary.com/base-conversion-in-php-using-bcmath/.)

, , / 16 2. 9 : 10 A, 11 B,..., 15 F.

+6

(, ) n 1 b 1, b 2. , b 1 b 2.

0,1 10 , 0.17 10 . 0,1 3 3, 10, , 0. (3) 10 ( , 3 ). , 0,1 10 0,17 10 , 0.0 (0011) 2 0,2 (B851E) 16

- , , .

+3

, , , . - :

shift=0;

while v>=base,  v=v/base, shift=shift+1;  

Next digit: 
if v<1.0 && shift==0, output the decimal point
else 
   D=floor(v)
   output D
   v=v-D
v=v*base
shift = shift-1
if (v==0) exit;
goto Next Digit

, N .

+2

" " - , .. 1/10 ^ 1, 1/2 ^ 1.

. , 1 :

 8 4 2 1 . 1/2 1/4 1/8 1/16 

, 10.1 , , "8" "2", 10 . 1/2 (0,5) , 1/4 (0,25) , 1/8 (0,125) . 1/16 (0,0625), 0.0375. 1/32 - 0.03125, . :

 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32
 1 0 1 0    0   0   0   1     1

0,00625. 1/64 (0,015625) 1/128 (0,0078125) , 1/256 (0,00390625):

 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256
 1 0 1 0    0   0   0   1     1    0   0     1

0,00234375.

..1 ( , 1/3 ). , , , , .

+1

Before I start with this in the light of my GMP library, here, where I should try to make PHP Rick Regan code common for any base from 2 to 36.

Function dec2base_f(ByVal ddecimal As Double, ByVal nBase As Long, ByVal dscale As Long) As String
    Const BASES = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 'up to base 36
    Dim digitCount As Long
    Dim wholeNumber As Double
    Dim digit As String * 1
    digitCount = 0
    dscale = max(dscale, Len(CStr(ddecimal)) - Len("0."))
    Dim baseary_f As String
    baseary_f = "0."
    Do While ddecimal > 0 And digitCount < dscale
        ddecimal = ddecimal * nBase
        digit = Mid$(BASES, Fix(ddecimal) + 1)
        baseary_f = baseary_f & digit '"1"
        ddecimal = ddecimal - Fix(ddecimal)
        digitCount = digitCount + 1
    Loop
    dec2base_f = baseary_f
End Function

Function base2dec_f(ByVal baseary_f As String, nBase As Double) As Double
    Const BASES As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim decimal_f As Double
    Dim i As Long
    Dim c As Long
    For i = Len(baseary_f) To Len("0.") + 1 Step -1
        c = InStr(BASES, Mid$(baseary_f, i, 1)) - 1
        decimal_f = decimal_f + c
        decimal_f = decimal_f / nBase
    Next
    base2dec_f = decimal_f
End Function

Debug.Print base2dec_f(dec2base_f(0.09, 2, 200), 2) --> 0.09
Debug.Print base2dec_f(dec2base_f(0.09, 8, 200), 8) --> 0.09
Debug.Print base2dec_f(dec2base_f(0.09, 16, 200), 16) --> 0.09
0
source

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


All Articles