Mod Q

Hap

Well-known Member
Joined
Jul 20, 2005
Messages
647
I would like to use a function like the "Mod" operator. My problem with the Mod operator is that it rounds floating point numbers to integer before returning a value. I am hoping there is another function that I can use that will return the remainder of the division of two floating point numbers.

Any guidance would be appreciated.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,279
Could you give an example please?

There shouldn't really be a remainder when you divide 2 floating point numbers.

eg

1.2/0.5=2.4

What would you say the remainder was there?

Do you mean the decimal part?
 

Oaktree

MrExcel MVP
Joined
Jun 20, 2002
Messages
7,937
Can you give an example of where the MOD function doesn't return what you'd like?
 

Hap

Well-known Member
Joined
Jul 20, 2005
Messages
647
The mod function returns the remainder of the division of two integers. If you equate a result to Integer Mod 1 the result is 0 because the all integers are divisible by one but if you equate a result to 19 Mod 2 you will get a result of 1.

I would like to be able to equate 3.14159 Mod 3 and get .14159 or 3.14159 Mod .1 and get .04159.

Hopefully that is a little clearer than mud!

Thanks
 

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,119
Hap said:
The mod function returns the remainder of the division of two integers. If you equate a result to Integer Mod 1 the result is 0 because the all integers are divisible by one but if you equate a result to 19 Mod 2 you will get a result of 1.

I would like to be able to equate 3.14159 Mod 3 and get .14159 or 3.14159 Mod .1 and get .04159.

Hopefully that is a little clearer than mud!

Thanks
Still mud I guess...

Maybe you're looking for:

=A2-TRUNC(A2)
 

Hap

Well-known Member
Joined
Jul 20, 2005
Messages
647
That is the right idea. I am trying to code it.

Example

If 1.2345 / .25 = 4.938

Then .25 goes into 1.2345 only 4 times so the remainder
will be .938 * .25

I hope the puddle is getting clearer.
 

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,119
Hap said:
That is the right idea. I am trying to code it.

Example

If 1.2345 / .25 = 4.938

Then .25 goes into 1.2345 only 4 times so the remainder
will be .938 * .25

I hope the puddle is getting clearer.
What is the desired result: 0.938 or 0.938*0.25?
 

Hap

Well-known Member
Joined
Jul 20, 2005
Messages
647
I just finished my own Mod Function to do what I want. Maybe there is already something like this out there.



Public Function Mod2(X As Double, Y As Double)

Dim Z As Double
Dim Z2 As Double

Z = X / Y
Z2 = WorksheetFunction.Floor(Z, 1)
Z = Z - Z2
Z = Z * Y

Mod2 = Z

End Function
 

Forum statistics

Threads
1,078,252
Messages
5,339,100
Members
399,278
Latest member
randomNumberGenerator2211

Some videos you may like

This Week's Hot Topics

Top