Page 1 of 1
[SOLVED] Getting the remainder of float division
Posted: Tue Feb 19, 2013 3:14 am
by HugeCode
Hi all. In last days I'm trying to work a bit with floating-point numbers in x86 assembly. I have processed to state when I need to get remainder of floating point division. I know that FPU does not offer anything like this but I still need it. Is there any simple way how to do it? I was thinking of decreasing or increasing (depending on if it's signed or not) number by divisor until it reaches or passes zero and then getting difference or sum (again depending on sign) between last result and divisor. Here's example of my idea:
Code: Select all
; dividing 5.48 by 0.3
5.18, 4.88, 4.58 ... 1.28, 0.98, 0.68, 0.38, 0.8, -0.22
; result 0.3 + (-0.22) = 0.8
; 0.8 is the result
Will this work? Is there any faster solution? Please help.
Re: Getting the remainder of float division
Posted: Tue Feb 19, 2013 4:24 am
by Combuster
Compare the speed of performing 1/0.000001 with your method and the speed of executing 1/0.000001 in C code. Wouldn't you have a better place to start if you use the faster method to get yourself an approximation first?
Re: Getting the remainder of float division
Posted: Tue Feb 19, 2013 4:52 am
by iansjack
What about the FPREM instruction?
Re: Getting the remainder of float division
Posted: Tue Feb 19, 2013 6:06 am
by chaoscode
maybe 5,48/0,3 = 18,266...
0,3*(18,266-18)
0,3*0,266 = 0,0798 ~ 0.8
So
Code: Select all
double divident = 5,48;
double divisor = 0,03;
double quotient = divident/divisor;
double remainder = (quotient-((double)((int)quotient)))*divisor;
Re: Getting the remainder of float division
Posted: Tue Feb 19, 2013 8:58 am
by HugeCode
iansjack: as you've written here, I googled it but it seems like it calculates just partial remainder and I have no idea how can I work with it also. MSVC++ is the only place I get information about how do the floating point numbers work, but C++ does not support this operation.
chaoscode: I can see your point but (int)doublevar seems to be not valid in C and also I have no idea about how to write it in assembly. I think you meant something like round when writing explicit conversion.
Back to FPREM, does anybody have any idea how to use it?
Re: Getting the remainder of float division
Posted: Tue Feb 19, 2013 9:20 am
by Combuster
Re: Getting the remainder of float division
Posted: Tue Feb 19, 2013 11:49 am
by HugeCode
What does it mean "partial remainder"? Can anybody please provide some example?
Re: Getting the remainder of float division
Posted: Tue Feb 19, 2013 11:58 am
by iansjack
Re: Getting the remainder of float division
Posted: Sat Feb 23, 2013 5:26 am
by HugeCode
Ok, so can this code be?: (100.4 % 25.3 = 3 rem 24.5)
Code: Select all
fld dword [_divide] ;load 25.3
fld dword [_tobedivided] ;load 100.4
.again:
fprem ;do
fstsw ax ;move status word to ax
fwait
bt ax, 10 ;test C2 bit
jc .again ;if it's 1, do it again
Re: Getting the remainder of float division
Posted: Sat Feb 23, 2013 7:17 am
by iansjack
The easy way to find the answer to that question is to try it and see. It's not something that I need to do, so I won't be testing your code.
Re: Getting the remainder of float division
Posted: Sat Feb 23, 2013 8:29 am
by HugeCode
Sorry, I didn't mean that you should test it. I was just requesting some one-eye check.
// edit:
thank you for the FPREM reference, it works. tested in VC++.