News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Binary fractions

Started by Mikl__, November 24, 2019, 05:16:17 PM

Previous topic - Next topic

Mikl__

Preamble
It needed to translate 1/6 to IEEE 754 single precision. Usually I force the compiler, but this time I had to translate manually using the Windows calculator from the standard set of programs
  • 1/6 = 0.1666666666666666666666666666666667
    0.1666666666666666666666666666666667> 0 therefore sign = 0
  • calculate the exponent:
    log2(0,1666666666666666666666666666666667) = -2.584962501
    round down -2.584962501 to -3
    exponent shifted by a bias 127-3 = 124 = 1111100
    customize for 8 bits 01111100
  • calculate the fraction:
    23 bits - (-3) (exponent) = 26
    (0.1666666666666666666666666666666667-2-3) × 226 = 0.04166666666666666666666666666667 × 67108864 = 2796202.6666666666666666666666669 ≈ 2796203 = 10101010101010101010101011
    customize for 23 bits 0101010101010101010101011
  • sign.exponent.fraction = 0.01111100.010101010101010101010101011 = 0011.1110.0010.1010.1010.1010.1010.1011 = 0x3E2AAAAB
Main part
I drew attention to the repeating chains 0 and 1. The binary representation of fractions of the form M/(2N-1) without calculating on a calculator, maybe someone will come in handy
1/3 = 0,3333333333333333333333333333333333 = 0,01.01.01.01.01.01.01.01.01.01.01.01
2/3 = 0,6666666666666666666666666666666667 = 0,10.10.10.10.10.10.10.10.10.10.10.10
1/6 = 0,1666666666666666666666666666666667 = 0,0.01.01.01.01.01.01.01.01.01.01.01 = (1/3)/2
1/7 = 0,142857.142857.142857.142857.142857.14 = 0,001.001.001.001.001.001.001.001
2/7 = 0,285714.285714.285714.285714.285714.29 = 0,010.010.010.010.010.010.010.010
3/7 = 0,428571.428571.428571.428571.428571.43 = 0,011.011.011.011.011.011.011.011
4/7 = 0,571428.571428.571428.571428.571428.57 = 0,100.100.100.100.100.100.100.100
5/7 = 0,714285.714285.714285.714285.714285.71 = 0,101.101.101.101.101.101.101.101
6/7 = 0,85714285714285714285714285714286 = 0,110.110.110.110.110.110.110.110
1/15 = 0,0666666666666666666666666666666667 = 0,0001.0001.0001.0001.0001.0001.0001
2/15 = 0,133333333333333333333333333333333 = 0,0010.0010.0010.0010.0010.0010.0010.0010
1/5 = 0,2 = 0,0011.0011.0011.0011.0011.0011.0011 = 3/15
4/15 = 0,2666666666666666666666666666666667 = 0,0100.0100.0100.0100.0100.0100.0100
7/31 = 0,225806451612903.225806451612903.23 = 0,00111.00111.00111.00111.00111.00111
P.S. There is a "comma" is used as a separator between the integer and the fractional part in Russia, not a "point" like in the USA

daydreamer

Quote from: Mikl__ on November 24, 2019, 05:16:17 PM
Preamble
It needed to translate 1/6 to IEEE 754 single precision. Usually I force the compiler, but this time I had to translate manually using the Windows calculator from the standard set of programs
  • 1/6 = 0.1666666666666666666666666666666667
    0.1666666666666666666666666666666667> 0 therefore sign = 0
  • calculate the exponent:
    log2(0,1666666666666666666666666666666667) = -2.584962501
    round off -2.584962501 downward -3
    exponent shifted by a bias 127-3 = 124 = 1111100
    customize for 8 bits 01111100
  • calculate the fraction:
    23 bits - (-3) (exponent) = 26
    (0.1666666666666666666666666666666667-2-3) × 226 = 0.04166666666666666666666666666667 × 67108864 = 2796202.6666666666666666666666669 ≈ 2796203 = 10101010101010101010101011
    customize for 23 bits 0101010101010101010101011
  • sign.exponent.fraction = 0.01111100.010101010101010101010101011 = 0011.1110.0010.1010.1010.1010.1010.1011 = 0x3E2AAAAB
And here I drew attention to the repeating chain 01. And here's a binary representation of fractions of the form M/(2N-1) without calculating on a calculator, maybe someone will come in handy
1/3 = 0,3333333333333333333333333333333333 = 0,01.01.01.01.01.01.01.01.01.01.01.01
2/3 = 0,6666666666666666666666666666666667 = 0,10.10.10.10.10.10.10.10.10.10.10.10
1/6 = 0,1666666666666666666666666666666667 = 0,0.01.01.01.01.01.01.01.01.01.01.01 = (1/3)/2
1/7 = 0,142857.142857.142857.142857.142857.14 = 0,001.001.001.001.001.001.001.001
2/7 = 0,285714.285714.285714.285714.285714.29 = 0,010.010.010.010.010.010.010.010
3/7 = 0,428571.428571.428571.428571.428571.43 = 0,011.011.011.011.011.011.011.011
4/7 = 0,571428.571428.571428.571428.571428.57 = 0,100.100.100.100.100.100.100.100
5/7 = 0,714285.714285.714285.714285.714285.71 = 0,101.101.101.101.101.101.101.101
6/7 = 0,85714285714285714285714285714286 = 0,110.110.110.110.110.110.110.110
1/15 = 0,0666666666666666666666666666666667 = 0,0001.0001.0001.0001.0001.0001.0001
2/15 = 0,133333333333333333333333333333333 = 0,0010.0010.0010.0010.0010.0010.0010.0010
1/5 = 0,2 = 0,0011.0011.0011.0011.0011.0011.0011 = 3/15
4/15 = 0,2666666666666666666666666666666667 = 0,0100.0100.0100.0100.0100.0100.0100
7/31 = 0,225806451612903.225806451612903.23 = 0,00111.00111.00111.00111.00111.00111
P.S. There is a "comma" is used as a separator between the integer and the fractional part in Russia, not a "point" like in the USA
thanks,is also "," here in my country,wonder if it isnt originally from french meter standarsation and maybe most europeans use that
I dont know what IDE or editor you use,but mine helps me when I type in lots of decimal numbers and warns me if I for example type one too many 6 that can fit into float/double,or other type,so this might be a nice alternative to just type 0,0001 and let program copy that byte/word to the rest bits in real4,real8,real10 or custom variable type with loads of decimals for scientific computing
thats normal for nonprogrammers has to stick with type in long numbers,with the human error of accidently type in wrong number especially with 7/31,while programmers can eliminate human error and automatize the process
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

Mikl__

Quote from: daydreamerI dont know what IDE or editor you use
hi, daydreamer!
I use an editor that wrote myself and a universal bat-file. And which editor or IDE do you use?

jack

well done Mikl, for the lazy like me use a Javascript/Html calculator https://babbage.cs.qc.cuny.edu/IEEE-754/
you can save the web page to your HD and run it offline.

Mikl__

Hi, jack!
Thank you very much!