# Thread: Mathematical Operation

1. ## Mathematical Operation

hi All,

In my script, I need to perform a "Modulo (%)" operation, which states that 13%5 = 3 ( remainder).

But the problem with this operator is that it converts both the numbers (inputs) into integers and then does the operation.
But in my script, i need to do the same between 2 float numbers and hence cannot use the Modulo operator.

Can you please suggest if there is any other inbuilt function which can do the same operation or any work ard for this.

Thanks!!
Sam

2. ## Re: Mathematical Operation

my scripting language is JScript

3. ## Re: Mathematical Operation

I'm not sure what you're trying to accomplish by modulo on a floating. That is technically not defined.

I'm guessing you want to say..

5.5 % 2.2 = 1.1

So what you want to do is multiply both numbers by a common value that'll make them into whole numbers, then divide the result.

((5.5 * 10) % (2.2 * 10))/10 = 1.1

4. ## Re: Mathematical Operation

After rethinking it a bit, I came up with:

// find the remainder from a float division
function FloatMod(dividend, divisor)
{
var result = dividend / divisor;
result -= Math.floor(result);
return (result * divisor);
}

So, if you have 8.25 / 2.5 the return would be 0.75. But, as David mentioned above, mod is really only defined by integer operations.

5. ## Re: Mathematical Operation

Hi Sam,

The string "0.1" will be posted to the test log if you execute the following code:

<font class="small">Code:</font><hr /><pre>// JScript
a = 2.2;
b = 0.7;
var res = a % b;
Log.Message(res);
</pre><hr />

If this is not what you need, please clarify your request.

6. ## Re: Mathematical Operation

Thanks David, Agromatic and Alex for your ideas.
I like david's idea the best as i deal with really small float numbers like 0.00015 etc.
HI Alex,
in the scenario,

a = 0.03855;
b = 0.00015;
log.Message(a%b);

The actual output seen in TestComplete is:
4.30970363562988E-18.
while actually "a" is divisible by "b".
you get me?

i also face similar problems if I try Agromatic's idea as even if remainder is 0, the output comes as 4.30970363562988E-18 (which really is a very small number).

The work ard that I am using now is to convert this into a string and Check for occurance for "E" in string.
I dont like it, but i cannot think of anything else.
I hope my explanation is clear!!

Thanks!

7. ## Re: Mathematical Operation

Hi Sam,

I recommend that you specify a threshold value that will be considered as the least possible remainder. You can use the following code:

<font class="small">Code:</font><hr /><pre>
function Main()
{
a = 0.03855;
b = 0.00015;
Log.Message(mod(a, b));
}

function mod(a, b)
{
var threshold = 0.00001
var res = a % b;
if (res &lt; threshold)
{
return 0;
}
else
{
return res;
}
}
</pre><hr />

[ QUOTE ]

The actual output seen in TestComplete is:
4.30970363562988E-18.
while actually "a" is divisible by "b".

[/ QUOTE ]
TestComplete uses the Microsoft JScript scripting engine. I guess this behavior is caused by the way arithmetic operations are implemented there.

8. ## Re: Mathematical Operation

okay Jared.
Thanks!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 11:11 PM.