One of our agents has a problem. For years, floating-point math has been too complicated. Remember the good old days? You used to be able to type var=INT(var+0.5) without black-box functions getting in the way. These glory days are long past.

In fact, the reason the IEEE was created was to make floating-point math miserable, especially when it comes to rounding. It is in this new world that Smith works, attempting to round to an arbitrary number of digits. However, when he puts 39.995 into his function, it returns 39.99 instead of 40.00. Other inputs produce the expected output, e.g. 11.995 produces 12.00. Can you solve the mystery?

It would probably help if I showed you the code. (It's Visual Basic --- an important fact --- so jot that down.)

  Public Function roundTo( number as Double, digits as Integer ) As Double
      Dim temp As Double
      Dim factor As Double

      factor = 10 ^ digits
      temp = number * factor + 0.5
      roundTo = Int(temp) / factor
  End Function

 

In the end, Smith ended up changing the code to use a formatting function but he's still wondering what went wrong before. The challenge is for you to figure out what's going on here and explain it to the rest of us. For completeness, here's the new code but that's the last itoa from me.

  Public Function roundTo( number as Double, digits as Integer ) As Double
      roundTo = roCDbl( Format(number, "0." & String(digits,"0")) )
  End Function

 

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!