Calculating greatest common denominator

adammitchell23

Registered User.
Local time
Today, 20:35
Joined
Oct 30, 2017
Messages
24
Hi all,

I'm trying to calculate the lowest common denominator between the values of two textboxes on a form, [malerats] and [femalerats].

I'm using a function called Denominator, below:

Code:
Public Function Denominator(MaleRats As Long, FemaleRats As Long) As Long
While MaleRats <> FemaleRats
  If MaleRats > FemaleRats Then
  MaleRats = MaleRats - FemaleRats
  Else: FemaleRats = FemaleRats - MaleRats
  End If
Wend
RatRatio = MaleRats


End Function

Can anyone tell me why this isn't working?

Apologies if this is a question which had been asked before; whenever I try to use search on this forum, Chrome has a temper tantrum. Weird.

Cheers,

Ad
 
Endless loop?

Remove the While/Wend lines. (Did you step through the code to see the problem?)

Then the variable RatRatio will contain the difference in genders. To return the value to the calling code you need to add

Denominator=RatRatio
 
Are you actually looking for the denominator for a ratio between the two?
Your code will tell you how many more of one you have than the other. This can be easily done as
Code:
ABS(MaleRats - FemaleRats)
This most likely won't be helpful for a "ratio" though.

Say you have 10 male rats and 9 female rats. Your code would return 1, same as if there are 9 male rats and 10 female rats. Your code won't return a ratio though.

Please let us know what your end goal is so we can better assist.
 
it's not working because you are using RatRatio and not the function name:
Code:
Public Function Denominator(MaleRats As Long, FemaleRats As Long) As Long
While MaleRats <> FemaleRats
  If MaleRats > FemaleRats Then
  MaleRats = MaleRats - FemaleRats
  Else: FemaleRats = FemaleRats - MaleRats
  End If
Wend
[COLOR=blue]RatRatio[/COLOR] = MaleRats


End Function

should be:
Code:
Public Function Denominator(MaleRats As Long, FemaleRats As Long) As Long
While MaleRats <> FemaleRats
  If MaleRats > FemaleRats Then
  MaleRats = MaleRats - FemaleRats
  Else: FemaleRats = FemaleRats - MaleRats
  End If
Wend
[COLOR=Blue]Denominator[/COLOR] = MaleRats


End Function
 
Hi all,

Yes, I was looking for a denominator in order to eventually go on to do a ratio; I know that repeated subtractions like this is probably a really clunky way to do things, but it's the best I could find with Google... (Excel has the GCD function, but I can't find an equivalent).

I tried removing the While/Wend lines and replacing
Code:
RatRatio = MaleRats
with
Code:
Denominator = MaleRats

No joy...
 
the While... End is fine, re-instate it.
 
Adam,

You have shown us something that doesn't work as you want/need.

You call a function Denominator. You seem to have an endless loop.
And you appear to ant a ratio, but are not returning a value from the function.

What are you trying to achieve in simple, plain English?
It always helps developers/posters and readers if we know and understand the requirement.

Good luck.
 
Endless loop when a number less than 1 is entered.

Denominator(1, 0) -> 1-0 -> 1-0 -> 1-0 -> etc.
Denominator(0, 1) -> 0-1 -> 0-1 -> 0-1 -> etc.
Denominator(1, -1) -> 1 -(-1) -> 2 - (-1) -> 3 - (-1) -> etc.
 
Ok, sorry I wasn't clear. My end goal is to create a ratio using the values from the textboxes [malerats] and [femalerats] and put it in the textbox [ratratio],

So if [malerats]=20 and [femalerats]=4, then [ratratio]=5:1
 
Set the ratratio control to

= nz([malerats]/[femalerats],1) & ":1"
 
What ratratio would you like if there were 23 males and 4 females?
 
Or
..... if there were 4 males and 23 females?

Or
...if there were0 males and 0 females?
 
Yes, this is the other issue - we have to use integers, so I guess we'd have to round any decimals up or down. As for zeroes, how would we go about dealing with a division by zero error?
 
You test if zero division is going to be happen and avoid it.

What do you want to display with zero values and for the first case where the ratio is 4:23?
 
OK, so I checked with people for who I'm setting this up; it's OK if the ratios aren't integers and are just rounded to 1 decimal place, so I've done that with:

Code:
=Round(nz([malerats]/[femalerats],1),1) & " : 1"

...and they assure me that there will never be a case where there will be 0 male or female rats. I asked if they were sure and they said yes.

So... that's problem sorted then, I think...

Thanks everyone!
 
@OP,

Please see attached for calculating the ratio. I have annotated it and it will tell you what it is doing as it does it. You need to start with the largest value and work down to find the least common denominator.

NOTE: Posted after your reply. Didn't see it as I'l been looking prior to your reply.
 

Attachments

Last edited:
While maybe not for what you were intending, I have dozens of little pieces like this I use for reference. This one in particular for Do While loops.
 

Users who are viewing this thread

Back
Top Bottom