Recovering from Divide By 0 and Coming Back From Infinity with IsNaN and IsInfinite

While doing a series of regression tests, I noticed a few tests displayed errors due to a returned result of NaN. When I checked the test itself, it was for a margin calculation, and in some cases, one of the values was zero. This caused a divide by zero error, which returns NaN as a result. If this happens on the site, a 0.0% is displayed, but my test didn’t handle it correctly.

There would be two ways to approach this. One is the detect zeros before doing a divide and set the result to 0. This forces the result to agree with the site.

Another method is to use the isNaN function of Groovy. The calculation code stays the same, but the result is checked to see if it’s a number. For my situation, if the results is NaN, I set the answer to 0.0 to match the site result.

My formula is the following:

float calculatedMargin=(((Float.valueOf(GP) / Float.valueOf(sales)) * 100).round(1))

While it wouldn’t be hard to check if the value of GP or Sales is 0, I prefer to check the result at the end of the calculation using the following:

//If the result is NaN, or Infinity, set the calculatedMargin value to 0.0

if (calculatedMargin.isNaN()==true){
   calculatedMargin=0.0
}
if (calculatedMargin.isInfinite()==true){
   calculatedMargin=0.0
}

You’ll notice there is an isInfinite check. After correcting the NaN issue, I noticed the calculation could also return “Infinity” as a result. This is basically the other side of the coin when doing calculations with zero. If detected, this check sets the calculatedMargin to zero.

Both of these division errors are now handled by built in functions. Again, I could try and prevent the error in the first place by checking for zero values before doing any calculations. But, I prefer the second approach and just check the result. I could turn this validation into a Keyword, pass the calculated result, and use the validation in multiple places. That might be more efficient way of detecting and controlling the error.

I saw many solutions to this problem, but Groovy has these built in functions that handle the situation nicely and my false positive errors have been handled.

Other articles of interest:

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments
  • Create a Dynamic Object at Runtime (1)
    • Jeremy Brien: I appreciate this! I saw this post on LinkedIn this morning and was able to find a use case for it! I found that defining my xpath with an iterable variable allows me to loop through and capture text from tables created...
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (2)
    • Don Pedro: That would be things like: log.logWarning(‘The import date listed on the site is: ‘ + importDate) log.logWarning(‘The value from the site is: ‘ + salesFigure) log.logWarning(‘Filter Results for ‘ +...
    • Prashant Pednekar: Thanks for the informative article but can we also put some variable values to make it more exciting.
  • Setting up a repeatable Search Method in Katalon Studio (1)
    • RJ: Thanks a lot! This is very helpful 🙂
  • Filling forms with random numbers in Katalon Studio (3)
    • Patrick Clough: We created a handy random string generator custom keyword. The method takes in a string for the type of string you want, and an integer for the length. Looks like this: static String RANDOM_ALPHANUM =...
    • Don Pedro: randomNumber is the variable that holds the result of generating a new random number. Depending on how things work on your site, you might need to convert the number to a String before it’s recognized correctly....
    • Seth: When you are setting up randomNumber is this a new keyword or a new variable? I am trying to randomly generate unique socials and can’t seem to connect my generator to my Set Text.
  • Working with Dates and Date Formatting in Katalon Studio (5)
    • Ann: THANK YOU ! That worked perfectly !
    • Don Pedro: import groovy.time.TimeCategory currentDate = new Date() println currentDate use( TimeCategory ) { after30Mins = currentDate + 30.minutes } println after30Mins https://stackoverflow.com/ques tions/21166927/incrementing...
    • ricky julianto nugroho: hay can u gave me an example for plus a hour in the time ? example : now is 15 pm i want to print 16 in the text thank you
  • Boostnote for Code Snippets (1)
    • Manual: Hi Don, Regarding the code snippets and examples that you have created. Is there any GitHub repo or something that can be shared? Thanks
  • Using TypeIt4Me with Katalon Studio (3)
    • Manual: That’s great thanks for sharing.
    • Don Pedro: Added a screenshot to show some examples of how I have shortcuts configured for Groovy code. TypeIt4Me supports groups, so you can create shortcuts related to Groovy, Selenium, and other topics to keep them organized. You can...
    • Manual: Hi Don, I like the idea of using the TypeIt4Me for scripting test code. Could you please share how I can create. I also like the idea of creating code templates to use with code editors. Thanks, Manual
  • Another success with Katalon Studio (2)
    • Don Pedro: Since Katalon is free, a very practical demonstration of what it can would be to create the code needed to fill in forms. That was one of my first tasks as Katalon could fill in a lengthy form in seconds versus the minute or...