[SOLVED] Getting the remainder of float division

Programming, for all ages and all languages.
Post Reply
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

[SOLVED] Getting the remainder of float division

Post 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.
Last edited by HugeCode on Tue Apr 16, 2013 9:56 am, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Getting the remainder of float division

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4686
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Getting the remainder of float division

Post by iansjack »

What about the FPREM instruction?
chaoscode
Posts: 7
Joined: Thu Dec 13, 2012 3:24 am

Re: Getting the remainder of float division

Post 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;
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

Re: Getting the remainder of float division

Post 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?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Getting the remainder of float division

Post by Combuster »

"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

Re: Getting the remainder of float division

Post by HugeCode »

What does it mean "partial remainder"? Can anybody please provide some example?
User avatar
iansjack
Member
Member
Posts: 4686
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Getting the remainder of float division

Post by iansjack »

You really need to brush up on your Googling skills.

http://www.website.masmforum.com/tutori ... chap12.htm
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

Re: Getting the remainder of float division

Post 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
User avatar
iansjack
Member
Member
Posts: 4686
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Getting the remainder of float division

Post 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.
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

Re: Getting the remainder of float division

Post 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++.
Post Reply