News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

how can I create floating point?

Started by mineiro, January 13, 2020, 11:29:24 AM

Previous topic - Next topic

mineiro

How can I create a floating point from a binary point of view? I'm talking in intergers point of view.
I have tried a lot, but some numbers needs more bits while others not. Why?
Why I cant represent all fractions into this sense?
I was reading some documents from doctors and academic, but this does not enter into my mind.
To be easy, how much is 1/??
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

HSE

Mineiro:

¿what is a point of view?

You want to represent fraccions with integers. ¿Are You well?  :biggrin:
Equations in Assembly: SmplMath

jimg

Can you write 1/3 exactly as a decimal?   No, it is 1.333333333.....

Similarly, binary cannot represent all fractions.

mineiro

I'm fine sir HSE. I like to reproduce my question.
How much bits I need to represent a reason, division?
As an example: the number 4,5,6,7 in binary have 3 bits only. If i try 1/4,1/5, ... how many bits I need to represent that division? Why is not 3 bits plus one? Why some divisions need more than N bits?
This sounds easy in decimal, but lets take pi=3.1415. If pi ends like that instead of infinite, I know that I just need translate that decimal to binary which I know how to do, but how to reach exactly instead of aproaching number?
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

mineiro

Quote from: jimg on January 13, 2020, 12:19:28 PM
Can you write 1/3 exactly as a decimal?   No, it is 1.333333333.....

Similarly, binary cannot represent all fractions.
Yes, 0.333... something like this. But how many numbers 3 is necessary? I know is infinite, but to precision of 1/3 we dont need too much because number 3 fits in 2 bits.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

HSE

Quote from: mineiro on January 13, 2020, 12:21:46 PM
I'm fine sir HSE.
Fantastic  :thumbsup:

If you are creating a system of representation the rules are what you want.
If you define that those bits means "inverse" 1/7 requiere 3 bits, but you can't represent 1/8.
Equations in Assembly: SmplMath

jj2007

Quote from: mineiro on January 13, 2020, 12:21:46 PMIf i try 1/4,1/5, ... how many bits I need to represent that division? Why is not 3 bits plus one?

You can represent a division like 3/7 with 8 bits:
include \masm32\include\masm32rt.inc
.code
ThreeBySeven db 7*16+3
SevenByThree db 3*16+7
start:
  movzx eax, ThreeBySeven
  mov edx, eax
  and eax, 7
  sar edx, 4
  push eax
  fild dword ptr [esp]
  push edx
  fild dword ptr [esp]
  fdiv
  fstp REAL8 ptr [esp]
  invoke crt_printf, chr$("3/7=%0.18f", 13, 10), REAL8 ptr [esp]

  movzx eax, SevenByThree
  mov edx, eax
  and eax, 7
  sar edx, 4
  push eax
  fild dword ptr [esp]
  push edx
  fild dword ptr [esp]
  fdiv
  fstp REAL8 ptr [esp]
  invoke crt_printf, chr$("7/3=%0.18f", 13, 10), REAL8 ptr [esp]
  inkey
  exit
end start


The possible range is 1/7 ... 7/1. If you allow 16 bits, you can even use negative numbers, as in -66/77 - see attached source:
3/7=    0.428571283684245130
7/3=    7.000000955304130900
-66/77= -0.86842095837193090

mineiro

The number 4 and 5 in binary is 100 and 101, so they have a size of 3 bits.

1/4 = 0.25
1/5 = 0.2

transforming that decimal floating point to binary floating point.
1/4 = 0.25   ==>   0.5   1.0   ==>0.01
1/5 = 0.2   ==>   0.4   0.8   1.6   1.2   0.4   ...   ==>0.00110011...

1/4 need 2 binary digits in fractional part to be represented and result reach remainder zero, or end.
1/5 need 4 binary digits in fractional part to be represented until it continues repeating and result never reach remainder zero.

The question is: Why? Why 1/4 give as result 2 bits and 1/5 give as result 4 bits.
How can I know how many binary digits is necessary to represent a fraction until result is zero or a periodic tithe?
Why 1/4 fits in itself and 1/5 do not fit in itself?

So, if I have in decimal the fraction 100/123, how many binary digits is necessary to represent that fraction?

------while I was writing you posted sir jj2007. But without using floating point instructions, only interger instructions, well, interger math.
Quote from: jj2007 on January 13, 2020, 04:58:02 PM
You can represent a division like 3/7 with 8 bits:
Yes, but why you know that the result fit in 8 bits? Whats the minimum size?
Thanks.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

jj2007

Quote from: mineiro on January 13, 2020, 06:09:06 PM
Whats the minimum size?

It's different for any number. Try using this in my example, and put an int 3 between fdiv and fstp. Then check the number in ST(0).

HundredBy123   dw 100+123*256

mineiro

Thank you for your patience but I continue without understanding.

How much bits I need to represent 1/N, where N is a interger random number with sizeof 3 bits?
So can be 1/1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7.

Minimum size is 1 bit, because 1/1 = 1. So, how many bits is necessary to hold the result of that division where N is random?
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

jj2007

See below. What is the practical application of your question?

1 1/1
2 1/2
2 1/3
3 1/4
3 1/5
3 1/6
3 1/7
4 1/8
4 1/9
4 1/10
4 1/11
4 1/12
4 1/13
4 1/14
4 1/15

mineiro

I'm not able to represent that as 1/4 as an example, I need represent that as binary floating point number. That's why I talked about pi.
Not all numbers that I'm dealing are just 1/N, they can be X/Y (really huge numbers), so using pi as example, I can have 3 as interger part and that infinite as remainder.
Well, ok, inverting scenario, whats 1/pi? Assume that pi = 3,1415. How many binary digits is necessary to represent this arithmetic?

The pratical application of this is to me conquer the world.
Again, thank you.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

jj2007

1/PI=0.31830988618379067153776752674503... and you need an infinite number of bits to represent that number.

JonasS

PI is not a good example, it is not an algebraic number, can not be put in fractional form.

jj2007

Right, Jonas. But 3/7 is a good example, and it can be exactly "represented" with 8 bits=1 byte, as shown above. The problem is what to do with the result... using the FPU with max 19 digits of precision?