Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

? Floating Point Modulus - How to ? 1

Status
Not open for further replies.

astrogirl77

Programmer
Aug 14, 2008
45
CA


Hi,

I'm trying to figure out how to create a simple floating point modulus function for VB.

Im using an OLD VB version, version 4.0. Yikes, yes I know I should upgrade and will... but am working currently on some old code and not ready yet to move up.

I can't use the standard default Mod operator for what I'd like to do since it rounds all the values.

I'd like to be able to perform operations on numbers as large as the max ceiling of ;

"Currency (scaled integer) 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807."

Or as large as possible, but I'd like to get a mod value back thats a floating point and accurate past the decimal point (no rounding and little to no truncating) or at least as close, accurate as possible.

I've scoured the internet for solutions, (and no this is NOT a schoolo or class assingment - I'm working on a purely personal project) and so far have come up blank. I have found some solutions that look workable in C, but I can't program in C currently. I also found the basic math equations for the INT variants... but these won't work as Im hoping in code... at least I haven't been able to get it to.

If anyone could provide some feedback, suggestions, support, references, code snippets.... I'd really appreciate it.

Thanks and best to all!

AstroGurl

 
I'd be tempted to write a C++ DLL that wraps math.h's fmod function ...
 
However, here's a VB implementation of the algorithm used in VB.NET's Mod function. Note that it is limited by VB's maximum long value (and there's no error checking).
Code:
[blue]' Still limited by VB's maximum long value
Public Function fmod(a As Variant, b As Variant) As Variant
   fmod = a - Int(a / b) * b + CLng(Sgn(a) <> Sgn(b)) * b
End Function[/blue]
 
hooray! just tired this a it works great!

One thing Im wondering though, can we use the Currency data type instead of Clng and Int ?
 
I know it works but the "is not equal to" Sgn(a) <> Sgn(b) in the middle of the left hand side of an equation mystifies me!


How does this work and what does it do? Is it something like an IIF ?

If I put (a<>b) on one side of an equation I get true or false depending on whether the two variables are not equal or not.
 
It tells you if they are both positive, both negative, or whether one is positive and the other is negative; it doesn't care whether the absolute values of the two variables is the same or not, i.e we are comparing the sign of the variables

Basically it ensures that the result is correct no matter which side of 0 it falls
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top