Mod/Modulo/Remainder Flow Service

Hi All,

Every now and again, I have had a requirement for a Mod service - A service which takes two numbers as input and determines if the smaller number divides the larger number completely and if it doesnt, whats the remainder left behind.

Such an operator does not exist in wM (or so I believe). Lot of people write Java code for this. I wrote a simple flow service that does this and wanted to share it here. Hope this helps someone, somewhere, sometime.

Comments, suggestions, corrections and scoldings are all welcome.

Penguin
modX.zip (2.46 KB)

Thats a good service… :slight_smile:

Thanks hussainjaffer. I thought it is a handy little service.

Penguin,

Most people would write Java code for this because it’s so simple plus it is more efficient. Just try it yourself. Here’s code for a Java service that returns basically what your service returns:

IDataCursor cursor = pipeline.getCursor();
int divident = Integer.parseInt(IDataUtil.getString(cursor, "Divident"));
int divisor = Integer.parseInt(IDataUtil.getString(cursor, "Divisor"));
int rem = divident % divisor;
IDataUtil.put(cursor, "isDivisible", (rem==0)?"true":"false");
IDataUtil.putInt(cursor, "Remainder", rem);
cursor.destroy();

Just try creating a Java service with this code and run a few tests to compare the speed of this service versus the flow version. You should see the difference.

  • Percio

Hi Percio,

I had myself written a java service for this requirement previously. I just wanted to write it in flow.

I believe if it can be written in flow then it should be.

I did not say this was the best way to do it, but if you wanted to do it in flow, this would be the way to go.

This service is so small and simple that execution time or efficiency would not take a hit whether you use flow or java. Having said that, I can think of scenarios where processing time improvements, of even nano seconds can greatly boost overall performance.

Thanks for your response.

Now people have two services to choose from for the same functionality. :slight_smile:

Hey Percio,

Well it is a debate whether to use flow services when you can have java services. Actuaklly a lot of people do agree that whatever you can do in a FLOW you should not do it in Java. and they say it is a rule of thumb.

You can check the following url for the above mentioned post.
http://www.wmusers.com/forum/showthread.php?t=7965

Hussain

Interesting that my “don’t write it in Java” post is referred to here.

For this particular case, I’d write it in Java (and actually have). It is such a focused service there is little point in reinventing the wheel here. Leveraging Java in this case makes more sense than writing it FLOW.

But it is cool to see that this particular function could be written in FLOW (sort of–using Java services to do the real work).

How accurate is this? The use of divideFloats may be an issue for accuracy in some cases.

The largest number I have ever tried it with is 999999999999999. Any number bigger than that and it starts crapping out. Until 999999999999999 it works great. Which is large enough number I think.

WhutDaYaThink?

Can you be more specific on what you mean by “crapping out?”

Well for numbers larger than that, the ‘divideFloats’ service loses accuracy.

For example with 99999999999999999 as divident and 4 as divisor, divideFloats returns 25000000000000000.0 as quotient. On the other hand if you use your Windows calculator app, the quotient would be 24999999999999999.75.

This results in negative remainders. For example with that divisor and and divident you would get a remainder of -1 and an isDivisible value of No.

On the other hand, with the same divident and 9 as the divisor, which must clearly return isDivisible = Yes and return nothing for a remainder, the results become unexpected. One would get isDivisible = “No” and remainder as -9.

I think the ‘divideFloats’ BIS rounds of the significant digits far “quicker” than your average calculator.

I do think that for most practical applications, the biggest number I have successfully tested should be good enough.

In my free time I also created a floatRound service, which rounds off the significant digits in a floating point number depending on how many significant digits the user inputs. Would any one like me to share that as well?

Thanks
pEnGuIn

As mentioned earlier, the use of divideFloats can be a problem whenever accuracy is desired. It isn’t an issue of how large the number is but the value of the number.

For example, the decimal value 0.1 is a repeater fraction in binary (like 1/3 is a repeating value in decimal–0.33333333…). The example you provided where you see an inaccurate result is because of the use of binary floating point. When accuracy is required (all calculations involving money, for example) the use of float/double should be avoided.

While a rounding service is certainly useful in many respects, its use is often a response to accuracy errors introduced by floating point arithmetic. Instead of rounding an inaccruate result, it would be better in many cases to avoid the inaccuracy altogether. For example, using a class like BigDecimal or equivalent.

It’s very cool that you were able to create these services using FLOW. And such services for floating point data can be useful. I just want make folks aware that “floating point” and “accuracy” do not go together. And one should be aware of the limitations before using floating point for calculations. Most business integrations should avoid floating point.

This just in…

IS_7-1-1_Flow_Fix1

      [B]Article ID:  [/b]1614326735

Quick Access URL: http://advantage.webmethods.com/article/?id=1614326735
Last Updated: 24-Nov-2008

Which just goes to show how widespread is the misunderstanding of binary floating point. They “fixed” the wrong thing. Floating point cannot accurately represent all values. Just like decimal cannot accurately represent 1/3. By changing multiplyFloats to use BigDecimal, they are no longer doing “multiplyFloats”. They are now doing “multiplyDecimal”.

They should change it back and add BigDecimal-based services along-side the floating point services.

Note too that this is not specific to Java and Intel. This applies to all binary representations–all languages and all platforms.

Well speaking of floating point numbers and rounding off, heres another service that I find very nifty. Once again, all FLOW.

Lemme know what you think.

pEnGuIn
floatRoundX.zip (7.33 KB)

In my IS it is showing no services in Package

Hi Krishna,

which package, which IS version?

Regards,
Holger

version is 9.7.0.0.361 and packages are modX.zip,floatRoundX.zip

Hi Krishna,

are there any error messages in the server.log when reloading the packages or restarting the IS?

You should check for the PSUtilities package which contain the following service amongst others:
ps.util.math:modDivideInts

The package should be available from the Download Section (Code or Samples) of the TechCommunity.

Regards,
Holger

no errors

Hi Krushna,

might be an issue with ACLs or with the manifest.v3 file in the packages.

For the second part there should be error messages in the server.log regarding package loading.

Can you see the services when browsing the packages in IS Admin UI?
Then you should be able to see the configured ACLs for these.

Regards,
Holger