Author Topic: Decimal / Binary converter  (Read 8538 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #15 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.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #16 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
...

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Decimal / Binary converter
« Reply #17 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.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #18 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 :-))
« Last Edit: July 11, 2021, 08:24:51 am by bplus »

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: Decimal / Binary converter
« Reply #19 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://www.electronics-tutorials.ws/binary/binary-fractions.html
https://isaaccomputerscience.org/concepts/data_numbases_fractional_numbers



____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Decimal / Binary converter
« Reply #20 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Decimal / Binary converter
« Reply #21 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.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #22 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://www.electronics-tutorials.ws/binary/binary-fractions.html
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
  [ You are not allowed to view this attachment ]  
« Last Edit: July 11, 2021, 07:04:39 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #23 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"

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #24 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!
« Last Edit: July 11, 2021, 07:02:21 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Decimal / Binary converter
« Reply #25 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.

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Decimal / Binary converter
« Reply #26 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.
« Last Edit: July 11, 2021, 08:49:08 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Decimal / Binary converter
« Reply #27 on: July 11, 2021, 10:18:57 pm »
b$ = "1001.1001"

FYI, that's not a _BYTE, INTEGER, LONG, SINGLE, DOUBLE or FLOAT.
Granted after becoming radioactive I only have a half-life!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Decimal / Binary converter
« Reply #28 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.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #29 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".