QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Jaze on July 08, 2021, 10:28:33 pm

Title: Decimal / Binary converter
Post by: Jaze on July 08, 2021, 10:28:33 pm
Code: QB64: [Select]
  1. CONST TRUE = 1
  2. CONST FALSE = 0
  3. DIM decimal AS LONG
  4. DIM binaryNum$
  5.  
  6. CALL Menu
  7.  
  8. SUB Menu
  9.     DO
  10.         DO
  11.             COLOR 10, 0
  12.             PRINT: PRINT: PRINT
  13.             PRINT "1 - Decimal to binary"
  14.  
  15.             PRINT "2 - Binary to decimal"
  16.             which$ = INPUT$(1)
  17.         LOOP UNTIL which$ = "1" OR which$ = "2"
  18.         IF which$ = "1" THEN
  19.             CALL DtoB
  20.         ELSE
  21.             CALL BtoD
  22.         END IF
  23.     LOOP
  24.  
  25. SUB BtoD
  26.     PRINT: PRINT: PRINT
  27.     INPUT "Type in the binary number: ", binaryNumber$
  28.     power = 0
  29.     FOR digit = LEN(binaryNumber$) TO 1 STEP -1
  30.         onOff = VAL(MID$(binaryNumber$, digit, 1))
  31.         decimal = decimal + (onOff * (2 ^ power))
  32.         power = power + 1
  33.     NEXT digit
  34.     PRINT
  35.     PRINT
  36.     PRINT "Decimal equivalent is " + S$(decimal)
  37.     PRINT
  38.     PRINT
  39.     PRINT
  40.     PRINT "Press any key for another conversion or [ESC] to end.": CALL P(TRUE)
  41.  
  42. SUB DtoB
  43.     PRINT: PRINT: PRINT
  44.     INPUT "Type in a decimal number: ", decimal
  45.     binaryString$ = ""
  46.     DO
  47.         q = INT(decimal / 2): r = decimal MOD 2
  48.         binaryString$ = S$(r) + binaryString$
  49.         decimal = q
  50.     LOOP UNTIL q = 0
  51.     PRINT binaryString$
  52.     PRINT
  53.     PRINT
  54.     PRINT
  55.     PRINT "Press any key for another conversion or [ESC] to end.": CALL P(TRUE)
  56.  
  57. SUB P (escOrNot)
  58.     pause$ = INPUT$(1)
  59.     IF pause$ = CHR$(27) AND escOrNot = TRUE THEN END
  60.  
  61. FUNCTION S$ (number)
  62.     rtn$ = ""
  63.     rtn$ = STR$(number)
  64.     rtn$ = LTRIM$(rtn$)
  65.     S$ = rtn$
  66.  
Title: Re: Decimal / Binary converter
Post by: TempodiBasic on July 09, 2021, 05:49:05 pm
Hi Jaze
fine tool!
What do you think about an Inform version?
Title: Re: Decimal / Binary converter
Post by: johnno56 on July 09, 2021, 06:50:39 pm
Cool converter. Nicely done!
Title: Re: Decimal / Binary converter
Post by: bplus on July 09, 2021, 06:55:33 pm
For tickles compare: https://www.qb64.org/forum/index.php?topic=1602.0
Title: Re: Decimal / Binary converter
Post by: johnno56 on July 09, 2021, 07:05:59 pm
bplus: Point well made... Counterpoint. Will BIN$() teach the user 'how' it is converted? Nothing wrong with using the command but knowing how it is done is beneficial if one is using a 'basic' that does not utilize the BIN$() function...
Title: Re: Decimal / Binary converter
Post by: bplus on July 09, 2021, 09:38:41 pm
bplus: Point well made... Counterpoint. Will BIN$() teach the user 'how' it is converted? Nothing wrong with using the command but knowing how it is done is beneficial if one is using a 'basic' that does not utilize the BIN$() function...

Code: QB64: [Select]
  1.     DIM temp~&&, charPos%, highPos%
  2.     '--- init ---
  3.     temp~&& = value&&
  4.     BIN$ = STRING$(64, "0"): charPos% = 64: highPos% = 64  
  5. 'BIN$ is 0's with 1's to be inserted by way of MID$
  6.   DO
  7.  ' starting left and working right insert 1's where AND 1 is True then move place variables down 1 to right.
  8.         IF (temp~&& AND 1) THEN MID$(BIN$, charPos%, 1) = "1": highPos% = charPos%
  9.         charPos% = charPos% - 1: temp~&& = temp~&& \ 2 ' << tracking positions
  10.     LOOP UNTIL temp~&& = 0
  11.  
It's all Basic even those scary looking suffix :)

The adjustment for negative numbers is honestly over my head :( but this allows negative number conversions :).
Code: QB64: [Select]
  1.     IF value&& < 0 THEN
  2.         IF -value&& < &H0080000000~&& THEN highPos% = 33
  3.         IF -value&& < &H0000008000~&& THEN highPos% = 49
  4.         IF -value&& < &H0000000080~&& THEN highPos% = 57
  5.     END IF
Do you have to know how a TV works before you use it?

The final line cuts BIN$ down to size before returning BIN$ by function name.
Code: QB64: [Select]
  1. BIN$ = MID$(BIN$, highPos%)

Title: Re: Decimal / Binary converter
Post by: johnno56 on July 09, 2021, 10:43:00 pm
CRT, Plasma, LCD or OLED? Nah, kidding! Just curious as to how some commands work... lol
Title: Re: Decimal / Binary converter
Post by: bplus on July 10, 2021, 08:20:25 am
You can make some cool patterns by converting fractions (same denominator) to binary and letting 0 make a white square and 1 make a black square ( or for Johnno56 a Blue square ). eg 1/15, 2/15, 3/15... 14/15 stacked down the page.
Title: Re: Decimal / Binary converter
Post by: TempodiBasic on July 10, 2021, 08:39:51 am
@bplus
please Screenshot!
Title: Re: Decimal / Binary converter
Post by: bplus on July 10, 2021, 03:53:47 pm
@bplus
please Screenshot!

  [ This attachment cannot be displayed inline in 'Print Page' view ]  

Also makes for cool optical illusion, looks like non parallel lines (specially last set) :)
Title: Re: Decimal / Binary converter
Post by: Cobalt on July 10, 2021, 04:31:39 pm
Something tells me this work would all be a lot easier if you guys used the BIT routines; _SETBIT and _READBIT.

Just saying.
Title: Re: Decimal / Binary converter
Post by: bplus on July 10, 2021, 04:42:26 pm
Something tells me this work would all be a lot easier if you guys used the BIT routines; _SETBIT and _READBIT.

Just saying.

Yeah? so show us 27/101 in Binary :)
Title: Re: Decimal / Binary converter
Post by: bplus on July 10, 2021, 06:41:06 pm
This is start of what I am getting:
.0100010001101111100001100101011000101101100111111011

What are you guys getting?
Title: Re: Decimal / Binary converter
Post by: Bert22306 on July 10, 2021, 10:10:25 pm
For 27/101, and if I set the value of the MSbit to 1, I get:

0010 0010 0011 0111 1100 0011 0010 1011 0001 0110 1100 1111 11011

which are then followed by binary zeroes. Which implies to me that I've reached the limit of the accuracy of _Float.

This is where I compute the binary numbers, bit by bit. Also, my super duper program also gives the hex equivalent.

hex 2237C32B16CFD800
Title: Re: Decimal / Binary converter
Post by: Cobalt on July 10, 2021, 10:44:39 pm
Yeah? so show us 27/101 in Binary :)

you do realize that binary only deals in 1 and 0 right. 27/101 is not a 1 or a 0
Title: Re: Decimal / Binary converter
Post by: bplus on July 11, 2021, 12:01:30 am
you do realize that binary only deals in 1 and 0 right. 27/101 is not a 1 or a 0

neither is 5.

You do realize that binary means base 2? Any number expressed in one base can be expressed in another by conversion.
Title: Re: Decimal / Binary converter
Post by: bplus on July 11, 2021, 12:10:11 am
For 27/101, and if I set the value of the MSbit to 1, I get:

0010 0010 0011 0111 1100 0011 0010 1011 0001 0110 1100 1111 11011

which are then followed by binary zeroes. Which implies to me that I've reached the limit of the accuracy of _Float.

This is where I compute the binary numbers, bit by bit. Also, my super duper program also gives the hex equivalent.

hex 2237C32B16CFD800

Thanks @Bert22306  we seem to have the same expression except you don't show a decimal. Since 27/101 is .2673 with bar over top to represent repeating 4 digits, I think .01... does it with decimal placement as:
.1 base 2 = 1/2
.01 base 2 = 1/4  or .25
.001 base 2 = 1/8 or .125
...
Title: Re: Decimal / Binary converter
Post by: Cobalt on July 11, 2021, 02:30:51 am
neither is 5.

You do realize that binary means base 2? Any number expressed in one base can be expressed in another by conversion.

what I am saying is Binary can only express whole numbers. there are no fractions or decimals in binary.
Title: Re: Decimal / Binary converter
Post by: bplus on July 11, 2021, 07:57:54 am
what I am saying is Binary can only express whole numbers. there are no fractions or decimals in binary.

Maybe in Cobalt Code World that's the case ;-))

In bplus Code World and maybe the Math World would agree, Real Numbers can be expressed in any base be it 2, 10 or Pi.

Jiminy Crickets did I just say Pi? LOL  Code Worlds might not be ready for that one yet :-))
Title: Re: Decimal / Binary converter
Post by: George McGinn on July 11, 2021, 11:54:54 am
You can represent any decimal fractional number in Binary, as any systems programmer will tell you.

It is just not that simple.

Rather than me botching up or giving a convoluted explanation, here are some excellent sources:

https://stackoverflow.com/questions/4987176/how-do-you-convert-a-fraction-to-binary (https://stackoverflow.com/questions/4987176/how-do-you-convert-a-fraction-to-binary)
https://www.electronics-tutorials.ws/binary/binary-fractions.html (https://www.electronics-tutorials.ws/binary/binary-fractions.html)
https://isaaccomputerscience.org/concepts/data_numbases_fractional_numbers (https://isaaccomputerscience.org/concepts/data_numbases_fractional_numbers)



Title: Re: Decimal / Binary converter
Post by: SMcNeill on July 11, 2021, 12:37:15 pm
what I am saying is Binary can only express whole numbers. there are no fractions or decimals in binary.

This isn’t exactly true.   Binary is just Base-2 math

Take a 4 bit number:  1001

The right most bit represents 2 ^ 0.
The bit left of it represent 2 ^ 1.
The bit left of that represents 2 ^ 2.
The left most bit represents 2 ^ 3.

1001 = 2 ^ 3 + 2 ^ 0 = 9



Now, if we add decimal places, what happens?

1001.1001

We continue the pattern.  To the right of the decimal place, we have 2 ^ -1, 2 ^ -2, 2 ^ -3, 2 ^ -4.

Which breaks (on the right side of the decimal point) down to:

1/2 + 0 + 0 + 1/16

When combined, we get 1001.1001 = 9 + 9/16…


Which is where floating point errors come from, as there’s no way to represent something as simple as 1/10 in binary math.  (Just as decimal math can’t ever truly represent 1/3.  It’s 0.333333333333333333333333333333333333…..)

Binary math includes decimals.  They’re just in bases of 2, halved/doubled at each step of the way.
Title: Re: Decimal / Binary converter
Post by: Cobalt on July 11, 2021, 01:20:07 pm
I think we are just on 2 different planes here,

show me how to store this

1001.1001

in a BYTE, INTEGER, LONG, SINGLE, DOUBLE, or FLOAT?
In the computer store that binary sequence. with out extra code, just simple  BINARYBLAH = 1001.1001

I'm not saying on paper or on the screen as text, but inside the memory. That's all.

Yeah you can express or represent a decimal or fraction with binary values, but not store it. that information is not stored, its decoded. I was talking how that is stored. I think that is where we diverged. A simple miscommunication? I was talking oranges while you were talking apples? Both fruit but not the same.

Imagine the precision if you used an INTEGER64 to hold the decimal side! or multiple INT64s!
1\18446744073709551616

But then again who would want to deal with it? Just show me one real-world example where you need that level of precision, not theoretical but real-world. Do I need to worry about the mass of quarks or boson particles in my day to day activities? Probably not.
Title: Re: Decimal / Binary converter
Post by: bplus on July 11, 2021, 06:39:29 pm
You can represent any decimal fractional number in Binary, as any systems programmer will tell you.

It is just not that simple.

Rather than me botching up or giving a convoluted explanation, here are some excellent sources:

https://stackoverflow.com/questions/4987176/how-do-you-convert-a-fraction-to-binary (https://stackoverflow.com/questions/4987176/how-do-you-convert-a-fraction-to-binary)
https://www.electronics-tutorials.ws/binary/binary-fractions.html (https://www.electronics-tutorials.ws/binary/binary-fractions.html)
https://isaaccomputerscience.org/concepts/data_numbases_fractional_numbers (https://isaaccomputerscience.org/concepts/data_numbases_fractional_numbers)

Really not hard at all, here is a QB64 source by b+
Code: QB64: [Select]
  1. _Title "binary expansion of real numbers within a certain range of 0    B+ 2018-05-02"
  2. 'QB64 version 2017 1106/82 (the day before they switched to version 1.2)
  3. Const xmax = 1200
  4. Const ymax = 200
  5.  
  6. Screen _NewImage(xmax, ymax, 32)
  7. _ScreenMove 100, 360
  8.  
  9.     Cls
  10.     Print "BINARY EXPANSION of Real Numbers within Limits:"
  11.     Print
  12.     Print "Top limit is < 2^33,"
  13.     Print "The smallest number might depend on significant digits of input number."
  14.     Print "Small number .00000000001 ( 10 zeros and 1 ) seems OK... more checking needed."
  15.     Print "This expands to 100 binary places past integer point but precision of system may run out before then."
  16.     Print
  17.     Input "(Nothing quits) Enter a real number to see it's binary expansion "; a
  18.     If a = 0 Then Print "Goodbye": End
  19.     Print LTrim$(Str$(a)) 'trim lead blank
  20.  
  21.     'show expansion
  22.     b$ = ""
  23.     For P = 32 To -100 Step -1
  24.         If a >= (2 ^ P) Then
  25.             b$ = b$ + "1": a = a - (2 ^ P)
  26.         Else
  27.             If Len(b$) Then b$ = b$ + "0"
  28.         End If
  29.         If P = 0 Then b$ = b$ + "."
  30.         If a = 0 And P < 0 Then Exit For
  31.     Next
  32.     Print b$; " base 2"
  33.     _Clipboard$ = b$
  34.     Print
  35.     Input "Press Enter to continue...."; waitt
  36.  

9/16 = .5625 will use this later
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Decimal / Binary converter
Post by: bplus on July 11, 2021, 06:50:03 pm
Quote
show me how to store this

Quote from: SMcNeill on Today at 12:37:15 PM
1001.1001

b$ = "1001.1001"
Title: Re: Decimal / Binary converter
Post by: bplus on July 11, 2021, 06:56:45 pm
Quote
But then again who would want to deal with it? Just show me one real-world example where you need that level of precision, not theoretical but real-world. Do I need to worry about the mass of quarks or boson particles in my day to day activities? Probably not.

Say you work in a machine shop and all the washers are sized 1/2 inch, 1/4 inch, 1/8 inch, 1/16 inch ... (widths or thickness not radius or hole size)
and you have a .1001 base 2 inch space to fill (9/16 inches)
easy that takes 1 - 1/2 inch washer + 1 - 1/16 inch washer

easy because it's base 2 math like the washer sizes!
Title: Re: Decimal / Binary converter
Post by: SMcNeill on July 11, 2021, 07:57:02 pm
I think we are just on 2 different planes here,

show me how to store this

in a BYTE, INTEGER, LONG, SINGLE, DOUBLE, or FLOAT?
In the computer store that binary sequence. with out extra code, just simple  BINARYBLAH = 1001.1001

I'm not saying on paper or on the screen as text, but inside the memory. That's all.

Yeah you can express or represent a decimal or fraction with binary values, but not store it. that information is not stored, its decoded. I was talking how that is stored. I think that is where we diverged. A simple miscommunication? I was talking oranges while you were talking apples? Both fruit but not the same.

Imagine the precision if you used an INTEGER64 to hold the decimal side! or multiple INT64s!
1\18446744073709551616

But then again who would want to deal with it? Just show me one real-world example where you need that level of precision, not theoretical but real-world. Do I need to worry about the mass of quarks or boson particles in my day to day activities? Probably not.

Floating point numbers of all sorts work like this.

http://cstl-csm.semo.edu/xzhang/Class%20Folder/CS280/Workbook_HTML/FLOATING_tut.htm

If you look at the examples above, it describes how to write +1101.101 as a floating point value.

First, you normalize it, just like you would in base-10.  (1,234.567 is written in scientific notation as 1.234567E4, for example.

This makes our base-2 value of 1,101.101 become 1.101101E3.

Now, to write it as a float, the first digit is the sign:  0 for positive.
The next 8 bits are the exponent plus 127.  100000010
The remaining bits are the standardized value, after the decimal: 1011010000000000000000000

So 0100000010101101000000000000000000000000 is a 32-bit SINGLE which holds the value for 1101.101 in binary — which is 13 + 5/8.

Your machine *is* storing base-2 fractional values every time you use any floating point variable.

Title: Re: Decimal / Binary converter
Post by: SMcNeill on July 11, 2021, 08:28:24 pm
And, to show that these values are true and represented in memory in this manner, we have the following little program:

Code: QB64: [Select]
  1. Dim m(1) As _MEM
  2. m(0) = _Mem(s): m(1) = _Mem(l) 'point a memblock at both our values
  3.  
  4. Input "Enter a value =>"; s
  5. _MemPut m(1), m(1).OFFSET, s 'just copy what s holds in memory over to l, so we can read it with _Readbit
  6.  
  7. b = _ReadBit(l, 31) 'first bit is the sign
  8. Print "Sign:"; b
  9.  
  10. 'next 8 bits are the exponent +127
  11. Print "Exponent:";
  12. b$ = "&B"
  13. For i = 1 To 8
  14.     b = _ReadBit(l, 31 - i)
  15.     b$ = b$ + _Trim$(Str$(b))
  16.     Print b;
  17.  
  18. 'next 15 bits are the binary representation of that value, past the decimal point.
  19. Print "Mantissa:";
  20. m$ = ""
  21. For i = 1 To 22
  22.     b = _ReadBit(l, 23 - i)
  23.     Print b;
  24.     m$ = m$ + _Trim$(Str$(b))
  25. Print "Now to calculate that back, the exponent represents:"; Val(b$) - 127
  26. Print "Which says our original value is going to be 1."; m$; "shifted the above number of digits to the left."
  27. Print "At which point, you can now use base-2 math to convert that value back to its   decimal format.  (Just don't forget the sign from the first bit above.)"
  28. Print "As you notice, *ALL* these values are in BINARY (Base-2) format, and they       include fractional values."
  29. Print "It's this base-2 math which makes it impossible to represent a value such as    1/10 perfectly in floating point calculations."
  30.  
  31.  

As I described above, this takes any decimal value and converts to over to the closest base-2 representation that we can have with it, and then stores it in memory for us. 

First you formalize the value to 1.xxxxxxxE...
Then you add 127 to the exponent (giving a range of -127 to 128 from the 8 bits we use to store the exponent).
And the rest of the bits are used to represent you value.   (In the case of the binary value 1001.1001, it'd be stored as 00110010000000000000000.  The first one is understood to exist in formalized version, so we store the rest and use to exponent to properly position the decimal place seperately.)

And that's how it's all done, under the hood.



For an easy example to illustrate the process, enter the value 1.75 in the number above. 

You'll see the exponent in this case is 0.  The mantissa is 1100000....   

Putting those together, we get the original binary value of 1.1100000.... x 2^0.

1 in the one's position.
1 in the 1/2's position.
1 in the 1/4's position.

1.11 base-2 = 1.75 base-10, and our computers store them in the base-2 format.
Title: Re: Decimal / Binary converter
Post by: Cobalt on July 11, 2021, 10:18:57 pm
b$ = "1001.1001"

FYI, that's not a _BYTE, INTEGER, LONG, SINGLE, DOUBLE or FLOAT.
Title: Re: Decimal / Binary converter
Post by: Cobalt on July 11, 2021, 10:20:40 pm
Say you work in a machine shop and all the washers are sized 1/2 inch, 1/4 inch, 1/8 inch, 1/16 inch ... (widths or thickness not radius or hole size)
and you have a .1001 base 2 inch space to fill (9/16 inches)
easy that takes 1 - 1/2 inch washer + 1 - 1/16 inch washer

easy because it's base 2 math like the washer sizes!

I meant to the precision of an Integer64. if it was used as the decimal precision.
Title: Re: Decimal / Binary converter
Post by: bplus on July 11, 2021, 10:23:06 pm
FYI, that's not a _BYTE, INTEGER, LONG, SINGLE, DOUBLE or FLOAT.

You asked, "show me how to store this".
Title: Re: Decimal / Binary converter
Post by: bplus on July 11, 2021, 10:25:19 pm
I meant to the precision of an Integer64. if it was used as the decimal precision.

What? Integer is integer and decimal precision means floats, go with String and have best of both worlds ;-))
Title: Re: Decimal / Binary converter
Post by: Cobalt on July 11, 2021, 10:40:26 pm
You asked, "show me how to store this".

here if I make the rest of that line big and bold can you read it then?

I think we are just on 2 different planes here,

show me how to store this

in a BYTE, INTEGER, LONG, SINGLE, DOUBLE, or FLOAT?
Title: Re: Decimal / Binary converter
Post by: Cobalt on July 11, 2021, 11:08:48 pm
And, to show that these values are true and represented in memory in this manner, we have the following little program:

going off the page you shared, run this and see if you get the same number back they had,

from the page;
Quote
IEEE Short Real: 32 bits    1 bit for the sign, 8 bits for the exponent, and 23 bits for the mantissa. Also called single precision.

+1101.101       0  10000010  10110100000000000000000
so using SINGLE PRECISION (!)
with the 32 BITs as follows 01000001010110100000000000000000
printing the value, by your words, should print +1101.101 if it is represented in memory as a decimal-ed binary value.

Code: QB64: [Select]
  1. bit$ = "01000001010110100000000000000000"
  2. FOR i%% = 31 TO 0 STEP -1
  3.  IF MID$(bit$, i%%, 1) = "1" THEN a! = _SETBIT(a!, i%%)
  4. NEXT i%%
  5.  

alas it prints 46340
there is no native decimal information stored in that binary value. Which is why I don't believe we are talking about the same thing.
Title: Re: Decimal / Binary converter
Post by: Bert22306 on July 11, 2021, 11:35:29 pm
Thanks @Bert22306  we seem to have the same expression except you don't show a decimal. Since 27/101 is .2673 with bar over top to represent repeating 4 digits, I think .01... does it with decimal placement as:
.1 base 2 = 1/2
.01 base 2 = 1/4  or .25
.001 base 2 = 1/8 or .125
...

Yes, we get the same answer, and no, decimal points don't belong in binary numbers? I stipulated that the (decimal) value of the Most Significant Bit, in my answer, was 1. Which means that the next bit to the right would represent a value of 0.5, in decimal, etc. Which explains why the upper two bits are 0.

In my experience, that's the normal way of using binary numbers. You always have to specify either the value of the MSbit, or the value of the LSbit, to be able to use that binary number.

And yes, of course, decimal fractions can be encoded as binary numbers (responding to someone else, not you, bplus). Else, just one example, you'd never be able to use pi, in your programs!!

My decimal/binary/hex conversion program asks for the number of bits to be used in the binary, and asks for the value of the MSbit.

I have a different tool which deals only in binary. You tell it the value of the MSbit or the LSbit, and the number of bits in the binary number, and it gives the value of each bit in the binary number. No problem at all, representing decimal fractions in a binary number.

Okay, speaking of pi, this is what my program shows:

Decimal value =  3.141592653589793        Decimal as positive number = 3.141592653589793
MSbit value = 3 of the 32 -bit binary number

Binary value check, built from individual bit values (MSbit left) =
 10000110000010101001000111000001

Actual decimal equivalent =  3.14159265300259           Positive Integer = 2248839617             Hex = 860A91C1

You can, of course, change the scaling used. First, I create the binary number, within the limits allowed by _Float. And then, I go backwards, and reconstruct the original decimal value. You can see the error, in this 32-bit example. I could have specified fewer or more bits be used, in the binary expression, and a different decimal value assigned to the MSbit, of course. And "Positive Integer" means, what that binary number would represent, if it were to represent a decimal positive integer, i.e., where the LSbit represents decimal 1.
Title: Re: Decimal / Binary converter
Post by: Bert22306 on July 12, 2021, 12:22:49 am
You can also use what's known as fixed point, to represent decimal fractions in a binary number. Floating point gives you the most resolution, in general, but it's not mandatory, right?

Take a 16-bit binary number, in which you stipulate the the MSbit value, in decimal, is 5.0. This is the decimal value of each bit of that number. (So, when calculating the decimal value of the 16-bit number, you simply add up the decimal equivalent of every binary bit set to 1.)

---------------------------
Program computes the value of the bits in a digital quantity, starting with the known value of the MSbit
or the known value of the LSbit.

Bit numbers are assigned using the Internet convention (MSbit 0) and the serial convention (LSbit 0).

Value of each bit
Bit 0 or bit 15 =  5
Bit 1 or bit 14 =  2.5
Bit 2 or bit 13 =  1.25
Bit 3 or bit 12 =  .625     or 1 / 1.6
Bit 4 or bit 11 =  .3125    or 1 / 3.2
Bit 5 or bit 10 =  .15625   or 1 / 6.4
Bit 6 or bit 9 =  .078125   or 1 / 12.8
Bit 7 or bit 8 =  .0390625  or 1 / 25.6
Bit 8 or bit 7 =  .01953125               or 1 / 51.2
Bit 9 or bit 6 =  .009765625              or 1 / 102.4
Bit 10 or bit 5 =  .0048828125            or 1 / 204.8
Bit 11 or bit 4 =  .00244140625           or 1 / 409.6
Bit 12 or bit 3 =  .001220703125          or 1 / 819.2
Bit 13 or bit 2 =  .0006103515625         or 1 / 1638.4
Bit 14 or bit 1 =  .00030517578125        or 1 / 3276.8
Bit 15 or bit 0 =  .000152587890625       or 1 / 6553.6
Title: Re: Decimal / Binary converter
Post by: George McGinn on July 12, 2021, 12:53:54 am
@Cobalt @bplus @SMcNeill

I think what you are looking for is how the CPU does this.

Here is what I didn't want to explain, as to understand what the computer does, you need to understand assembler.

You see (in case you don't already know this) everything in a computer is stored in 1's and 0's. So when you do math in, say QB64, the CPU does it all in BINARY. That means fractions as well.

So, here is a quick pseudo-assembler routine to explain it.

In non-geek words, you need to split the binary number into three parts: integer, position of decimal place, and the fractional (decimal part) of the number.

From there (and you can follow the assembler program) you can do any math needed using 1's and 0's. (As you can see, this is done in BASE 2)

Code: Text: [Select]
  1. ;PROCEDURE EXPLANATION      
  2. ;This procedure converts the 4 digits of decimal fraction
  3. ;in to correspondent binary notation.  
  4.  
  5.  
  6. ;EXAMPLE DATA
  7. int_example  dw 0
  8. frac_example dw 1234        ;num is: 0.6425
  9.  
  10. mov ax, int_example
  11. mov bx, frac_example
  12. call convert
  13. end
  14.  
  15.  
  16. convert proc
  17.     call int_b_conversion
  18.     call frac_b_conversion
  19.     ret
  20. convert endp
  21.  
  22. int_b_conversion proc
  23.     ;to do
  24.     mov int_out, ax
  25.     ret
  26. int_b_conversion endp
  27.  
  28. frac_b_conversion proc
  29.     mov ax, 5000d
  30.     xor dx, dx           ;REG used to store the binary fractional part
  31.     mov cx, 08d          ;almost 8 digits in an half REG
  32.     cyc_01:  
  33.         rol dx, 1
  34.         cmp bx, ax
  35.         jg greater_so_sub
  36.         je break
  37.         ;lesser so multiply
  38.         rol bx, 1        ;mul by 2
  39.             ;have 0
  40.         jmp dodge_01
  41.         greater_so_sub:
  42.             inc dx       ;have 1
  43.             sub bx, ax
  44.             rol bx, 1
  45.         dodge_01:
  46.     loop cyc_01
  47.  
  48.     jmp dodge_02
  49.  
  50.     break:
  51.         sub cx, 1
  52.         inc dx
  53.         cyc_02:    
  54.             rol dx, 1
  55.             loop cyc_02
  56.  
  57.     dodge_02:
  58.             mov frac_out, dx
  59.             ret
  60. frac_b_conversion endp
  61.  



here if I make the rest of that line big and bold can you read it then?