I'm working on a formula to reduce fractions in Excel. The numerator and denominator are in separate cells. The numerator is in cell I38 and has a value of 1015. The denominator is in cell I40 and has a value of 100000.
I'm trying to use the GCD function to determine when I can reduce the fraction.
However, the GCD function is returning crazy results:

Am I misunderstanding the GCD function and how it works or is there a bug in Excel?
MS Excel 2016 on Windows 10, if that helps.
EDIT: When cell I38 has a number in it, the formula works fine. When it has a formula in it, the wrong answer is returned. Here is a screenshot of the formula:
Here is the formula:
=G38*10^IF(C39=INT(C39),0,LEN(MID(C39-INT(C39),FIND(".",C39,1),LEN(C39)-FIND(".",C39,1))))